Add values to the bars of a barplot
3 views (last 30 days)
Show older comments
I would like to add some additional values to a barplot. for example I have two vectors lets say
b = [0.14 0.18 0.25 0.13
a = [80 15 30 20]
now I want to create a barplot of b where inside the bins the values of a are show and at the top of each bin I would like to have the values of b. I attach an example in order to understand what I mean.
Thank you in advance, Elric
0 Comments
Accepted Answer
Star Strider
on 1 Aug 2016
This will do what you want:
b = [0.14 0.18 0.25 0.13];
a = [80 15 30 20];
figure(1)
hb = bar(b);
set(hb, 'FaceColor',[0.8 0.8 0.8])
a_txt = regexp(sprintf('%.0f\n',a), '\n','split');
b_txt = regexp(sprintf('%.2f\n',b), '\n','split');
text(1:4, ones(1,4)*0.01, a_txt(1:end-1), 'HorizontalAlignment','center')
text(1:4, b-0.01, b_txt(1:end-1), 'HorizontalAlignment','center')
You will have to experiment with your actual data to get the result you want, but this should get your started. The ‘a_txt’ and ‘b_txt’ assignments create the numeric labels. The last value is always the empty string, so it is necessary to eliminate it with the subscript addressing in the text calls. See the documentation for the bar and text functions for their details.
0 Comments
More Answers (1)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!