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');
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!