フィルターのクリア

Bar Plot of 2-D Array - How to label each individual bar with the value on top?

2 ビュー (過去 30 日間)
Robert
Robert 2013 年 10 月 28 日
編集済み: Image Analyst 2013 年 10 月 29 日
So I've managed to get the value to show on the plot, but it is only over the middle plot. I would like to get the values to show up over their respective bars. I think I need to get the x-coordinates for each bar but am not quite sure about how to do that. Any help would be much appreciated. Here's my code:
x=[1:3]';
y=round(rand(3,5)*10);
bar(x,y)
[size_x,size_y]=size(y);
for i1=1:size_x
for j1=1:size_y
text(x(i1),y(i1,j1),num2str(y(i1,j1),'%0.2f'),...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
end
end

採用された回答

Robert
Robert 2013 年 10 月 29 日
For anyone who might be looking to do the same thing, here's my solution. You just have to adjust the value of 'b' based on what your x-values are.
x=[1:3]';
y=round(rand(3,5)*10);
bar(x,y)
[size_x,size_y]=size(y);
a=0;
b=0;
for i1=1:size_x
for j1=1:size_y
a=a+1;
if a==1
b=-0.3;
end
if a==2
b=-0.15;
end
if a==3
b=0;
end
if a==4
b=0.15;
end
if a==5
b=0.3;
end
text((x(i1)+b),y(i1,j1),num2str(y(i1,j1),'%0.1f'),...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
if a==5
a=0;
end
end
end

その他の回答 (2 件)

sixwwwwww
sixwwwwww 2013 年 10 月 28 日
Dear Robert, replace first two lines of your code as follows and you will get your desired bar plot:
x=[1:15]';
y=round(rand(15,1)*10);
I hope it helps. Good luck!
  2 件のコメント
Robert
Robert 2013 年 10 月 28 日
Hey, thanks for your help. So yes that would solve the issue of getting the values for each bar, but I need the bars grouped together. That is why I have the code set to
x=[1:3]';
y=round(rand(3,5)*10);
Is there any way to get the values on top and still have bars grouped together?
sixwwwwww
sixwwwwww 2013 年 10 月 28 日
I don't think so but maybe there could be a work around for this problem. You can also have a look at
hist
Maybe that is useful to you

サインインしてコメントする。


Image Analyst
Image Analyst 2013 年 10 月 29 日
See my demo that does this, attached below.
  2 件のコメント
Robert
Robert 2013 年 10 月 29 日
編集済み: Robert 2013 年 10 月 29 日
Hey so that's not quite what I was trying to do, but I used a portion of your code to come up with a solution. Thanks for your input though!
Image Analyst
Image Analyst 2013 年 10 月 29 日
編集済み: Image Analyst 2013 年 10 月 29 日
My intent was not to do exactly what you want to do but to show you, in a demo I already had on hand, how you could use text() to do what you wanted to do. You were supposed to modify my code or your code to use text() to do more exactly what you wanted, which it looks like you've done in the Answer you Accepted.

サインインしてコメントする。

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by