How to put values above bars in barchart in Matlab?
Note, that there can be several bars per position of unknown width, so you can't just do text
y1 = [1; 2; 3; 4; 5];
y2 = [5; 4; 3; 2; 1];
bar([y1, y2]);
text(0.75:1:(length(y1)-0.25), y1, num2str(y1,'%.0f'), 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
text(1.0:1:(length(y2)+0.0), y2, num2str(y2,'%.0f'), 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
I can try some position guessing, but would like to avoid it.
You can know the locations of the bars with
b=bar([y1, y2]);
b.XData
gives you the location, b.BarWidth
its width, b.BarLayout
will tell you if there are more than one in the same location (to adjust for width) and so on. Read more about the available properties .
With this information you should be able to just put text
wherever you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.