如何绘制gnuplot中多行的均值和标准差

时间:2015-07-10 03:24:27

标签: gnuplot

我想绘制以下三列的平均值和标准差 名为std.txt的数据文件是上面的

1       5       4       2
2       2       1       0
3       8       4       1
4       4       3       0
5       1       0       0
6       8       2       1
7       4       3       3
8       7       5       4

我使用以下内容绘制三行

plot 'std.txt' using 1:2 with lines, \
     'std.txt' using 1:3 with lines, \
     'std.txt' using 1:4 with lines

现在我想绘制[均值+标准差,平均标准差]之间区域的均值和阴影区域。

1 个答案:

答案 0 :(得分:3)

我们假设您已经计算了每列的平均值和标准差 您可以使用类似下面的脚本

# Computed values of means (Avg) and standard deviations (SD)
Avg2=4.875000;    SD2=2.471715
Avg3=2.750000;    SD3=1.561249
Avg4=1.375000;    SD4=1.408678

set termoption enhanced
set key below
set style fill transparent solid 0.40 noborder  # <------ Note transparent
set title "{/=14 Plot for Kayan}"               # Bigger
set xlabel "{/=12 X of Kayan data [a.u]}"       # Big  
set ylabel "{/=12 Y of Kayan data [a.u]}"       # Big

plot Avg2+ SD2 with filledcurve  y1=Avg2-SD2  t "Mean #2"  lc rgb "#00B000" \
   , Avg2                                     notitle lw 2 lc rgb "#00B000" \
   , 'std.txt' using 1:2 with lines           t "Data #2"  lc rgb "#008000" \
   , Avg3+ SD3 with filledcurve  y1=Avg3-SD3  t "Mean #3"  lc rgb "#00B0F0" \
   , Avg3                                     notitle lw 2 lc rgb "#00B0F0" \
   , 'std.txt' using 1:3 with lines           t "Data #3"  lc rgb "#00B0B0" \
   , Avg4+ SD4 with filledcurve  y1=Avg4-SD4  t "Mean #3"  lc rgb "#0000B0" \
   , Avg4                                     notitle lw 2 lc rgb "#0000B0" \
   , 'std.txt' using 1:4 with lines           t "Data #4"  lc rgb "#000080" \
#

并获得与此类似的结果:

Plot of means and data

Big Bonus: awk代码,用于生成均值和标准偏差。

awk 'BEGIN{for (i=2; i<5; i++ ){n[i]=0; s[i]=0;    sq[i]=0;       } } \
          {for (i=2; i<5; i++ ){n[i]++; s[i]+= $i; sq[i]+=$i*$i ; } } \
       END{for (i=2; i<5; i++ ){printf("Avg%s=%f; SD%s=%f\n",i,s[i]/n[i], \    
                                i,sqrt(sq[i]/n[i]-s[i]^2/n[i]^2)) } }' std.txt