【问题标题】:Plot the max of two plots绘制两个图的最大值
【发布时间】:2013-11-04 02:14:16
【问题描述】:

我有一个数据文件,其中包含一些看起来像这样的点(请注意缺少一些值):

x   A   1-A
0   1   0
0.25    0   1
0.5
0.75    0   1
1   1   0
1.25    0   1
1.5
1.75    0   1
2   1   0
2.25    0   1
2.5
2.75    0   1
3   1   0
3.25    0   1
3.5
3.75    0   1
4   1   0
4.25    0   1
5

我想将此数据绘制成如下图所示(请注意,粉色线始终是其他两条线的最大值):

为了做到这一点,我有以下 gnuplot 代码,它适用于除粉线之外的所有代码:

gnuplot> max(x,y) = (x>y) ? x : y
gnuplot> plot "dataset1" using 1:2 title "A" with lines lc rgbcolor "black" lw 4, \
>"dataset1" using 1:3 title "1-A" with lines lc rgbcolor "blue" lw 4, \
>"dataset1" using 1:(max($2,$3)) title "Fuzzy(A)" with lines lc rgbcolor "purple" lw 4

但是,这会产生下图(请注意,紫色线的作用与上图中的粉色线不同):

我怎样才能生成一个看起来像第一张图片的图表,而不是我所拥有的?

【问题讨论】:

    标签: max gnuplot


    【解决方案1】:

    这有两个原因:

    1. 只要您不在using 语句中进行计算,空的“字段”就会被视为缺失数据。如果你有两个点,它们之间有一个“缺失”点,它们仍然用一条线连接。如果两者之间的点未定义(如计算时那样),则其他两点未连接。最简单的例子是:

      set yrange[-0.1:1.1]
      set multiplot layout 2,1
      plot 'dataset1' using 1:2 with lines
      plot 'dataset1' using 1:($2) with lines
      unset multiplot
      

      因此,您必须使用外部工具过滤数据才能正确绘制数据(另请参阅In gnuplot, with “set datafile missing”, how to ignore both “nan” and “-nan”?):

    2. 您需要计算两条曲线之间的交点才能得到“紫色”线。

    这是一个使用awk 的变体,它同时进行过滤(仅使用具有三个字段的行(if (NF == 3) 并跳过描述的第一行),并计算交点并将它们添加到输出中:

    max(x,y) = (x>y) ? x : y
    plot "dataset1" using 1:2 title "A" with lines lc rgbcolor "black" lt -1 lw 4, \
         "dataset1" using 1:3 title "1-A" with lines lc rgbcolor "blue" lt -1 lw 4, \
         "< awk 'BEGIN { prevX = prevA = prevN = currX = currA = currN = -1 } \
            { if (NF == 3 && NR > 1) { \
                if (currA != -1) { prevA = currA; prevN = currN; prevX = currX } \
                currX = $1; currA = $2; currN = $3; \
                if ((prevA != -1) && (prevA != currA)) { \
                   print 0.5*(currX + prevX), 0.5*(currA+prevA), 0.5*(currN+prevN); \
                }\
                print \
              }\
            }' dataset1" \
          using 1:(max($2,$3)) title "Fuzzy(A)" with lines lc rgbcolor "purple" lt 2 lw 4
    

    还有一些其他的小设置

    set termoption dashed
    set key above right
    set autoscale fix
    set offset 0,0,0.1,0
    

    和 4.6.4 我得到以下结果:

    【讨论】:

    • 非常感谢您。但我不得不问(因为我根本不知道任何awk - 我很喜欢awkward):我不禁注意到A1-A 的情节有互为倒数的斜率。您的解决方案是否仅适用于此类对称图,还是也适用于不太对称的图?
    • @inspectorG4dget 是的,这仅适用于 A1-A 的图,但概括它应该不是问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    相关资源
    最近更新 更多