gnuplot rowstacked 直方图:如何将总和放在条形上方

Ale*_*uer 5 label gnuplot histogram

这个问题与 gnuplot 直方图相关:How to put values on top of bar

我有一个数据文件file.dat

x y1 y2
1 2 3
2 3 4
3 4 5
Run Code Online (Sandbox Code Playgroud)

和 gnuplot:

set style data histogram;
set style histogram rowstacked;
plot newhistogram 'foo', 'file.dat' u 2:xtic(1) t col, '' u 3 t col;
Run Code Online (Sandbox Code Playgroud)

现在我想将第 2 列和第 3 列的总和放在条形上方。显而易见的解决方案

plot newhistogram 'foo', 'file.dat' u 2:xtic(1) t col, '' u 3 t col, \
'' u ($0-1):($2+$3+0.2):($2+$3) notitle w labels font "Arial,8";
Run Code Online (Sandbox Code Playgroud)

将标签放在正确的位置,但计算出的总和是错误的。也就是说,在 中($0-1):($2+$3+0.2):($2+$3),第二个$2似乎为零。

这里出了什么问题以及如何修复它?

Chr*_*oph 4

您必须给出一个明确的字符串作为标签:

 plot newhistogram 'foo', 'file.dat' u 2:xtic(1) t col, '' u 3 t col, \
'' u ($0-1):($2+$3):(sprintf('%.1f', $2+$3)) notitle w labels offset 0,1 font "Arial,8"
Run Code Online (Sandbox Code Playgroud)

作为其他改进,我将使用offset允许您以字符单位给出位移的选项,这不依赖于yrange。

(附注:如果使用列中的值,则可以跳过标签的显式格式,例如using 1:2:2 with labels,但通常应该使用sprintf来格式化标签)