Gnuplot rowstacked histogram: how to put sum above bars

This question is related to gnuplot histogram: how to set values ​​at the top of columns .

I have a data file file.dat

:

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

      

and gnuplot:

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

      

Now I want to place the sums of columns 2 and 3 above the columns. An obvious solution

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";

      

places the labels in the correct place, but the calculated amount is wrong. That is, in ($0-1):($2+$3+0.2):($2+$3)

, the second $2

seems to be priced to zero.

What's going on here and how do I fix it?

+3


source to share


1 answer


You must specify an explicit string as a label:

 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"

      



As another improvement, I would use an option offset

that allows you to force an offset in units of characters that is independent of yrange.

(Side note: if using a value from a column, you can skip explicit label formatting, for example using 1:2:2 with labels

, but in general should be used sprintf

to format a label)

+2


source







All Articles