How to print values of a bar plot...

4 ビュー (過去 30 日間)
jesica_anguiano Anguiano
jesica_anguiano Anguiano 2014 年 9 月 4 日
編集済み: Star Strider 2014 年 9 月 4 日
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
jesica_anguiano Anguiano 2014 年 9 月 4 日
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 Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by