如何将数据标签添加到 Gnuplot 直方图(平滑频率)?

How to add data labels to Gnuplot histogram (smooth freq)?

我的文件第 6 列中有蛋白质分子量数据。有问题的列如下所示:

MW [kDa]
16.8214045562515
101.41770820613989
24.332255496943485
43.946599899844436
210.58276787970942
57.987597263605494
27.384315650885558
119.02857910337919
8.962938979036466

我想绘制直方图,我正在使用 Gnuplot 的平滑频率函数进行绘制:

echo n=20 >$gnuplot #number of intervals
echo max=100 >> $gnuplot #max value
echo min=-0 >> $gnuplot #min value
echo width=\(max-min\)\/n >> $gnuplot #interval width
echo hist\(x,width\)=width*floor\(x\/width\)+width\/2.0 >> $gnuplot
echo plot \"$dataFile\" using \(hist\($6,width\)\)\:\(1.0\) smooth freq w boxes lc rgb\"blue\" notitle >> $gnuplot

如何在每个直方图条的顶部添加一个数据标签来表示每个 bin 的计数?我似乎找不到办法。

我会先将直方图数据绘制到 table 中,然后使用此 table 绘制直方图本身和标签。 检查以下示例。如果你有一个文件,例如'myData.dat',跳过随机数据生成行,而是添加行 FILE = 'myData.dat' 并将所有 $Data 替换为 FILE。正如@Eldrad 在评论中提到的,对标签使用绘图样式 with labels。检查 help labelshelp table.

代码:

### histogram with labeled bins
reset session

# create some random test data
set print $Data
    do for [i=1:2000] {
        print sprintf("%g",(invnorm(rand(0))+10)*20)
    }
set print

stats $Data u 1 nooutput
xmin = STATS_min
xmax = STATS_max

N = 20
myWidth = (xmax-xmin)/N
bin(col) = myWidth*floor(column(col)/myWidth)+myWidth/2.

set key noautotitle
set style fill solid 0.3
set boxwidth myWidth
set grid x,y
set offsets graph 0,0,0.05,0    # l,r,t,b

set table $Histo
    plot $Data u (bin(1)) smooth freq 
unset table

plot $Histo u 1:2 w boxes lc rgb "blue", \
         '' u 1:2:2 w labels offset 0,0.7
### end of code

结果: