按数据分组的特定 gnuplot

Specific gnuplot by data grouping

我是 gnuplot 的新手,很抱歉我的问题表述可能不精确,但我不知道如何找到解决我的问题所需的 tools/commnds。我想将绘图代码集成到我的 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

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

然后我想在 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 不是为此类数据处理任务而设计的。最好使用外部脚本来完成,该脚本进行处理并写入标准输出,然后可以像

一样直接提供给 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)
}