Reputation: 1037
This question is related to gnuplot histogram: How to put values on top of bars.
I have a datafile file.dat
:
x y1 y2
1 2 3
2 3 4
3 4 5
and the 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 bars. The 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";
puts the labels in the correct place, but the calculated sum is wrong. That is, in ($0-1):($2+$3+0.2):($2+$3)
, the second $2
appears to evaluate to zero.
What's going wrong here and how do I fix it?
Upvotes: 5
Views: 664
Reputation: 48440
You must give an explicit string as 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 other improvement, I would use the offset
option which allows you to give an displacement in character units, which doesn't depend on the yrange.
(Side note: if a value from a column is used, then one can skip the explicit formatting of the label, like using 1:2:2 with labels
, but in general one should use sprintf
to format the label)
Upvotes: 4