Gnuplot - 使用直方图样式时如何将y值置于柱上方?

dev*_*vil 2 gnuplot histogram

我目前正在使用脚本生成直方图,例如:

set style histogram cluster gap 4
plot for [COL=2:10] 'example.dat' u COL:xticlabels(1) title columnheader(COL)
Run Code Online (Sandbox Code Playgroud)

现在我希望在直方图中的条形图上方添加y值(数字),但是添加w labels会给出"此样式的列数不足"错误.

plot for [COL=2:10] 'example.dat' u COL:xticlabels(1) title columnheader(COL), \
    for [COL=2:10] 'example.dat' u COL title '' w labels
Run Code Online (Sandbox Code Playgroud)

是否可以使用直方图样式添加y标签?

注意:我知道有绘图的例子with boxes.如果可能的话,我希望使用直方图样式.

mgi*_*son 9

这是我提出的测试数据文件:

example.dat

 hi world foo bar baz qux
 1   2     3   4   5   6    
 4   5     7   3   6   5    
Run Code Online (Sandbox Code Playgroud)

这是我用来绘制它的脚本:

set yrange [0:*]
GAPSIZE=4
set style histogram cluster gap 4
STARTCOL=2                 #Start plotting data in this column (2 for your example)
ENDCOL=6                   #Last column of data to plot (10 for your example)
NCOL=ENDCOL-STARTCOL+1     #Number of columns we're plotting 
BOXWIDTH=1./(GAPSIZE+NCOL) #Width of each box.
plot for [COL=STARTCOL:ENDCOL] 'example.dat' u COL:xtic(1) w histogram title columnheader(COL), \
    for [COL=STARTCOL:ENDCOL] 'example.dat' u (column(0)-1+BOXWIDTH*(COL-STARTCOL+GAPSIZE/2+1)-0.5):COL:COL notitle w labels
Run Code Online (Sandbox Code Playgroud)

每个直方图簇在x轴上的总宽度为1个单位.我们知道我们需要多少宽度(盒子的数量+4,因为那是gapize).我们可以计算每个盒子的宽度(1/(N+4)).然后我们将直方图绘制为正常.(注意我添加with histogram到plot命令中).

根据内置帮助,标签需要3列数据(x y label).在这种情况下,y位置和标签是相同的,可以直接从列中读取COL.第一个块的x位置以0为中心(总宽度为1).所以,第一个区块将位于x=-0.5+2*BOXWIDTH.这里的2是因为间隙是4个盒子宽度 - 左边两个,右边两个.下一个块将位于-0.5+3*BOXWIDTH,等等.通常,(作为函数COL)我们可以将其写为

-0.5+BOXSIZE*(COL-STARTCOL+1+GAPSIZE/2)
Run Code Online (Sandbox Code Playgroud)

对于我们读取的每个附加块,我们需要将其向右移动1个单位.由于每个块对应于数据文件中的1行,因此我们可以使用伪列0(即column(0)$0),因为它为每个"记录/行"gnuplot读取增加.第0个记录保存标题,第一个记录保存第一个块.因为我们想要一个为第一条记录返回0的函数,所以我们使用column(0)-1.总而言之,我们发现x位置是:

(column(0)-1-0.5+BOXSIZE*(COL-STARTCOL+1+GAPSIZE/2))
Run Code Online (Sandbox Code Playgroud)

这相当于我上面所说的.