How to print values of a bar plot...

1 view (last 30 days)
jesica_anguiano Anguiano
jesica_anguiano Anguiano on 4 Sep 2014
Edited: Star Strider on 4 Sep 2014
I have this code that works fine. The problem with it is that it prints the value of the variable 'lab' which contains text, and i want to print the values of 'd' that contains numbers... i try to fix the code but i couldn't. I would appreciate any help. Thanks!
d=[306 286 273 242 238];
lab={'Switzerland' 'Germany' 'Austria' 'Norway' 'Sweden'};
bh=bar(d);
xd=get(bh,'children');
xd=get([xd],'xdata');
xd=cat(2,xd);
xdd=diff(xd);
xd=sort(xd(1,:)+.5*xdd(2,1));
set(gca,'xtick',xd);
set(gca,'xticklabel',lab.');
yl=get(gca,'ylim');
set(gca,'ylim',[yl(1),yl(2)+3]);
yValues = d' + 0.5;
text(xd, yValues(:),lab.','horizontalalignment','center');

Answers (1)

Star Strider
Star Strider on 4 Sep 2014
Comment-out the line that sets the labels to ‘lab’ and substitute ‘d’:
% set(gca,'xticklabel',lab.');
set(gca,'xticklabel',d.');
  2 Comments
jesica_anguiano Anguiano
jesica_anguiano Anguiano on 4 Sep 2014
I've just tried your suggestion, but i keep getting the same plot with the 'lab' text on top of each bar...
Star Strider
Star Strider on 4 Sep 2014
Edited: Star Strider on 4 Sep 2014
I didn’t see that, but then I wasn’t aware you wanted those labels to disappear as well.
Comment-out this line to eliminate the top labels:
% text(xd, yValues(:),lab.','horizontalalignment','center');
What do you want along the x-axis?
Do you want the ‘d’ numbers printed at the top instead?

Sign in to comment.

Categories

Find more on Labels and 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!