gnuplot rowstacked histogram:如何将sum加在bar之上

时间:2015-06-16 16:00:11

标签: label gnuplot histogram

此问题与gnuplot histogram: How to put values on top of bars有关。

我有一个数据文件file.dat

x y1 y2
1 2 3
2 3 4
3 4 5

和gnuplot:

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

现在我想将第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";

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

这里出了什么问题以及如何解决?

1 个答案:

答案 0 :(得分:2)

您必须提供一个显式字符串作为标签:

 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"

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

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

相关问题