首页 > 解决方案 > 如何将数据标签添加到 Gnuplot 直方图(平滑频率)?

问题描述

我的文件第 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 的计数的数据标签?我似乎找不到办法做到这一点。

标签: gnuplot

解决方案


我会先将直方图数据绘制到表格中,然后使用该表格绘制直方图本身和标签。检查以下示例。如果您有一个文件,例如'myData.dat',跳过随机数据生成行,而是添加该行FILE = 'myData.dat'并将所有内容替换$DataFILE. 正如评论中提到的@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

结果:

在此处输入图像描述


推荐阅读