通过数据分组的特定gnuplot

时间:2015-05-05 14:06:23

标签: gnuplot std mean

我是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

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

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

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

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

#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 -

1 个答案:

答案 0 :(得分:1)

通常,gnuplot不适用于此类数据处理任务。最好用外部脚本完成,它执行处理并写入stdout,然后可以直接输入到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

enter image description here

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

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)
}