How to print values of a bar plot...

4 次查看(过去 30 天)
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');

回答(1 个)

Star Strider
Star Strider 2014-9-4
Comment-out the line that sets the labels to ‘lab’ and substitute ‘d’:
% set(gca,'xticklabel',lab.');
set(gca,'xticklabel',d.');
  2 个评论
jesica_anguiano Anguiano
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 2014-9-4
编辑:Star Strider 2014-9-4
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by