【问题标题】:Specific gnuplot by data grouping按数据分组的特定 gnuplot
【发布时间】:2015-07-15 07:46:32
【问题描述】:

我是 gnuplot 的新手,很抱歉我的问题表述可能不精确,但我不知道如何找到解决问题所需的工具/命令。我想将绘图代码集成到我的 bash 文件中。

我有一个像这样的数据集:

285 1 50 7.35092
265 1 50 7.35092
259 1 50 7.35092
258 1 50 7.35092
264 1 50 7.35092

491 5 50 33.97
488 5 50 33.97
495 5 50 33.97
492 5 50 25.1649
495 5 50 33.0725
500 5 50 13.6176
507 5 50 32.2502
489 5 50 33.0725
494 5 50 33.97
491 5 50 33.97

746 10 50 34.6007
746 10 50 34.6007
767 10 50 30.858
745 10 50 34.8789
746 10 50 34.6007
747 10 50 34.6007
758 10 50 34.6007
772 10 50 34.60

我已经通过在块之间输入新行来对数据进行分组。我想为每个块计算第四列的平均值和标准差。

然后我想在Y 轴上绘制带有置信区间(标准差)的平均值,在X 轴上绘制第二列的值。

每个数据块在第 2 列中都有唯一的编号。

解决方案:到目前为止,我从第一个块中获得了一个点的值,但是当我尝试绘图时出现错误:

#myBash code for plotting.sh
FILEIN=simulationR.txt
rm plotTestR.png

gnuplot << EOF

reset
set terminal png
set output 'plotTestR.png'
set ylabel 'reward'
set xlabel 'Nr of simualtion'
set title 'Simualtio duration'
set grid

stats "$FILEIN" using 4 every :::0::0 nooutput
mean1 = sprintf('%.3f', STATS_mean)
std1 = sprintf('%.3f', STATS_stddev)
stats "$FILEIN" using 2 every :::0::0 nooutput
x1 = sprintf('%.3f', STATS_max)

plot '-' w yerrorbars title std1
x1 mean1 std1 

exit
EOF

和错误:

gnuplot> plot '-' w yerrorbars title std1
              ^
line 1: Bad data on line 1 of file -

【问题讨论】:

    标签: gnuplot std mean


    【解决方案1】:

    通常,gnuplot 不适用于此类数据处理任务。最好使用外部脚本来完成,该脚本进行处理并写入标准输出,然后可以直接将其提供给 gnuplot,如

    plot '< python myscript.py simulationR.txt'
    

    在您的示例中,您只能在plot '-' 部分之后有固定数据,这里没有进行变量替换。

    但是,gnuplot 版本 5 引入了一种新的内联数据结构,您可以将计算值写入其中 (set print $data)。

    注意,以下是一个普通的 gnuplot 脚本,如果你想将它包装在一个 bash 脚本中(这不是必需的,因为你可以通过命令行将变量传递给 gnuplot 脚本),那么你必须转义$ 个字符。

    FILEIN="simulationR.txt"
    system('rm -f plotTestR.png')
    
    reset
    set terminal pngcairo
    set output 'plotTestR.png'
    set ylabel 'reward'
    set xlabel 'Nr of simulation'
    set title 'Simulation duration'
    set grid
    
    set print $data
    do for [i=0:2] {
       stats FILEIN using 2:4 every :::i::i nooutput
       print sprintf("%e %e %e", STATS_max_x, STATS_mean_y, STATS_stddev_y)
    }
    set autoscale xfix
    set offsets 1,1,0,0
    
    plot $data using 1:2:3 w yerrorbars
    

    进一步的改进可能是用两个空行分隔两个块,在这种情况下你可以使用

    stats 'simulationR.txt' using 0 nooutput
    

    在变量STATS_blocks中有块的数量,你可以将循环重写为

    do for [i=0:STATS_blocks-1] {
       stats FILEIN using 2:4 index i nooutput
       print sprintf("%e %e %e", STATS_max_x, STATS_mean_y, STATS_stddev_y)
    }
    

    【讨论】:

    • 你是英雄,非常感谢。这正是我一直在寻找的。你有什么建议,比如教程之类的,所以我可以增加我在这个主题上缺少的知识吗?再次感谢。
    • 小提示:在 bash 脚本中,我必须删除 $ 但保留引用变量,例如:"data"
    • 在 bash 中你应该像 \$ 一样转义 $
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2019-11-08
    • 1970-01-01
    • 2017-06-29
    相关资源
    最近更新 更多