【问题标题】:Gnuplot: How to use multiple values from one data column?Gnuplot:如何使用一个数据列中的多个值?
【发布时间】:2012-11-19 15:14:54
【问题描述】:

我有一个多列数据文件。假设我只需要从第 13 行绘制第 4 个数字。这适用于以下代码:

plot 'datafile' u (some fix x-value):($0==13? $4 :1/0) with points

现在我想从第 13 行和第 11 行绘制这些数字的平均值。 像这样的:

plot 'datafile' u (some fix x-value):( ($4(line11)+$4(line13.))/2 ) with points

据我所知,在 gnuplot 中没有办法解决这两行,对吧? 我可以在 gnuplot 中使用 awk 或 sed 吗?也许将第 11 行的值存储在可用于第 13 行函数的变量中?

非常感谢您的热心帮助!

最好的祝愿, 托布

【问题讨论】:

  • 我已经很长时间没有使用 gnuplot 了,但是对于复杂的数据处理来说它总是一个糟糕的选择(参见他们的 faq )。我使用了简单的脚本(在这种情况下是 python 或 octave,有时是 grep/awk)并且有一个原始的 datafile 和几个派生文件,例如。 datafile_average。 Make是你的朋友;)

标签: function awk gnuplot lines


【解决方案1】:

我已经很长时间没有使用 gnuplot 了,我真正记得的只是我过去非常依赖 awk 来解析数据文件。要从 awk 的第 13 行获取第 4 个“数字”(我假设您的意思是字段/列),只需:

awk 'NR==13{print $4}' datafile

要从第 11 行和第 13 行的第 4 个字段中获取值的平均值,应为:

awk 'NR~/^(11|13)$/{sum+=$4} END{print sum/2}' datafile

现在如果有人能告诉你如何在 gnuplot 中使用 awk 的输出(抱歉,我不记得了)那么你就在做生意了。

编辑:一个 gnuplot 和 awk 的快速谷歌显示了这个:

gnuplot> plot "<awk '{x=x+$2; print $1,x}' file1.dat" with boxes

来自http://security.riit.tsinghua.edu.cn/~bhyang/ref/gnuplot/datafile3-e.html 以及其他几个示例。 HTH。

【讨论】:

    【解决方案2】:

    当然,你也可以只用 gnuplot 做一些事情。

    选项 1: 通过statsevery 提取感兴趣的值。

    • stats 从一列或两列计算一些统计值 min、max、mean 等(检查 help stats
    • every 将范围限制为某些行号或块号,此处:限制为单行,即感兴趣的行(检查 help every
    • 因此,在这种有限的情况下,最小值、最大值和平均值都将相同并等于感兴趣的值

    选项 2: 在三元运算符和串行评估的帮助下记住实际绘图命令中的值(检查help ternaryhelp operators binary

    • 将值v1v2初始化为NaN
    • 如果您找到感兴趣的行($0==row1-1$0==row2-1),请将值分配给 v1v2(检查 help pseudocolumns
    • 仅当v1v2 都与NaN 不同时,才进行计算并绘制此值。检查gnuplot: How to compare to NaN?

    在以下示例中,应从第 4 列绘制第 11 行(此处为:11.4)和第 13 行(此处为:13.4)的平均值,即12.4

    请记住,gnuplot 中的行/行编号从 0 开始。

    这个脚本可以很容易地适应其他变体。

    数据: SO13456636.dat

     1.1    1.2    1.3    1.4
     2.1    2.2    2.3    2.4
     3.1    3.2    3.3    3.4
     4.1    4.2    4.3    4.4
     5.1    5.2    5.3    5.4
     6.1    6.2    6.3    6.4
     7.1    7.2    7.3    7.4
     8.1    8.2    8.3    8.4
     9.1    9.2    9.3    9.4
    10.1   10.2   10.3   10.4
    11.1   11.2   11.3   11.4
    12.1   12.2   12.3   12.4
    13.1   13.2   13.3   13.4
    14.1   14.2   14.3   14.4
    

    脚本:(适用于 gnuplot >=4.6.0,2012 年 3 月)

    ### do some math with two values from the same row
    reset
    
    FILE = "SO13456636.dat"
    
    myCol  = 4
    myRow1 = 11
    myRow2 = 13
    
    set grid x,y
    set key noautotitle
    
    set multiplot layout 2,1
    
        # Option 1
        stats FILE u myCol every ::myRow1-1::myRow1-1 name "v1" nooutput
        stats FILE u myCol every ::myRow2-1::myRow2-1 name "v2" nooutput
        plot  FILE u (1):((v1_min+v2_min)*0.5) w p pt 7 lc rgb "red"
    
        # Option 2
        plot v1=v2=NaN FILE u (1):($0==myRow1-1 ? v1=column(myCol) : 0, \
                                   $0==myRow2-1 ? v2=column(myCol) : 0, \
                              (v1==v1 && v2==v2 ? (v1+v2)*0.5 : NaN)) w p pt 7 lc rgb "blue"
    unset multiplot
    ### end of script
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      • 2020-06-28
      • 2020-09-28
      相关资源
      最近更新 更多