【问题标题】:gnuplot rowstacked histogram: how to put sum above barsgnuplot rowstacked histogram:如何将总和放在条形上方
【发布时间】:2015-06-16 16:00:11
【问题描述】:

这个问题与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 列的 sums 放在条形上方。显而易见的解决方案

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 的计算结果似乎为零。

这里出了什么问题,我该如何解决?

【问题讨论】:

    标签: label gnuplot histogram


    【解决方案1】:

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

     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 来格式化标签)

    【讨论】:

    • 请注意,对于同时具有正负和的数据文件,正确的偏移量不再是恒定的,因此很遗憾您不能使用offset(它需要一个固定的位移),但必须使用yrange-dependent 方法。
    • 是的,你是对的。有一个非常非常难看的技巧:当值为负数时插入一些新行:sprintf("%s%.1f", ($2+$3) < 0 ? "\n\n" : "", $2+$3)。免责声明:不要在家里尝试这个;)
    猜你喜欢
    • 2015-09-11
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多