在gnuplot中添加垂直标记线以获得直方图

时间:2017-06-27 17:12:21

标签: gnuplot histogram

在绘制函数时,有一些指令可以在gnuplot中获取垂直线。就像使用set arrow函数一样。我需要这个功能用于直方图,结果直方图在X轴上具有0.0的不同位置。在我的例子中,X轴标记只是数据文件中的字符串。

当绘制直方图时,如果有平均值,+ -3sigma,可能是X = 0点,这是由阴影色实线的曲线从上到下的垂直线标记的。

我的直方图代码:

set boxwidth 1.0 absolute
set style line 1 lc rgb 'skyblue'
set style fill solid border lt -1
set style data histogram
set style histogram clustered gap 0.0
set xtics in rotate by 90 offset first +0.5,0 right
set xlabel
set ylabel 'Count'
set terminal unknown
plot 'histo.raw' using 3
set title 'data'
set yrange [0:GPVAL_DATA_Y_MAX*1.2]
set term X11
plot 'histo.raw' using 3:xtic(2) ls 1 title columnheader(1)
set arrow 1 from 0.0,0.0 to 0.0,GPVAL_DATA_Y_MAX*1.2 nohead

我的数据:

"data"
     0 "-INF ->  -5.0"      0   0.00
     1 " -5.0 ->  -4.5"      0   0.00
     2 " -4.5 ->  -4.0"      2   0.03
     3 " -4.0 ->  -3.5"      4   0.06
     4 " -3.5 ->  -3.0"      3   0.05
     5 " -3.0 ->  -2.5"      5   0.08
     6 " -2.5 ->  -2.0"     19   0.30
     7 " -2.0 ->  -1.5"     49   0.78
     8 " -1.5 ->  -1.0"    193   3.07
     9 " -1.0 ->  -0.5"    527   8.39
    10 " -0.5 ->  +0.0"   1289  20.53
    11 " +0.0 ->  +0.5"   1878  29.90
    12 " +0.5 ->  +1.0"   1411  22.47
    13 " +1.0 ->  +1.5"    636  10.13
    14 " +1.5 ->  +2.0"    178   2.83
    15 " +2.0 ->  +2.5"     56   0.89
    16 " +2.5 ->  +3.0"     17   0.27
    17 " +3.0 ->  +3.5"      9   0.14
    18 " +3.5 ->  +4.0"      4   0.06
    19 " +4.0 ->  +4.5"      0   0.00
    20 " +4.5 ->  +5.0"      0   0.00
    21 " +5.0 -> +INF"      0   0.00

set arrow函数将该行置于错误的位置。

set arrow 1 from 0.0,0.0 to 0.0,GPVAL_DATA_Y_MAX*1.2 nohead

在此数据中

mean= 0.2743
sigma= 0.7491

感谢您的想法。

格特

1 个答案:

答案 0 :(得分:0)

我发现直方图条定义了从左到右计数的X坐标。由于我有22个直方图条的22个数据行,因此在行位置添加11.0就可以了。

set boxwidth 1.0 absolute
set style line 1 lc rgb 'skyblue'
set style fill solid border lt -1
set style data histogram
set style histogram clustered gap 0.0
set xtics in rotate by 90 offset first +0.5,0 right
set xlabel
set ylabel 'Count'
set terminal unknown
plot 'histo.raw' using 3
set title 'data'
set yrange [0:GPVAL_DATA_Y_MAX*1.2]
set terminal png size 1200,800
set output 'histo.png'
mean= +0.2743
sdev= +0.7491
lboffs= 0.2
set arrow  from 11.0 + mean,0.0 to 11.0 + mean ,GPVAL_DATA_Y_MAX*1.2 nohead lw 2 lc rgb "dark-green"
set arrow  from 11.0 + mean - 3 * sdev,0.0 to 11.0 + mean - 3 * sdev ,GPVAL_DATA_Y_MAX*1.2 nohead lw 2 lc rgb "red"
set arrow  from 11.0 + mean + 3 * sdev,0.0 to 11.0 + mean + 3 * sdev ,GPVAL_DATA_Y_MAX*1.2 nohead lw 2 lc rgb "red"
set arrow  from 11.0,0.0 to 11.0,GPVAL_DATA_Y_MAX*1.2 nohead lw 2 lc rgb "blue"
set label "Mean" at 11.0 + mean + lboffs,GPVAL_DATA_Y_MAX*1.1 tc rgb "dark-green"
set label "+3%"  at 11.0 + mean + 3 * sdev + lboffs,GPVAL_DATA_Y_MAX*1.1 tc rgb "red"
set label "-3%"  at 11.0 + mean - 3 * sdev + lboffs,GPVAL_DATA_Y_MAX*1.1 tc rgb "red"
plot 'histo.raw' using 3:xtic(2) ls 1 title columnheader(1)
set output
set term X11