How do i put in a bar group the value of each bar on top?

3 ビュー (過去 30 日間)
Arne1234
Arne1234 2017 年 11 月 20 日
コメント済み: Arne1234 2017 年 11 月 20 日
Hi, I have a grouped bar graph and i want to add text off the value on the top of each bar in each group.
Example :
Y_as = [ 165 160 121 ; 59 84 62 ; 106 76 58 ]
X_as = categorical({'1. apples & pears' , '2. apples', '3. pears'})
HB = bar(X_as , Y_as , 'group')
%Result is in the image
%I know you need to put your numbers to a string bij @num2str but thats it.
%Do i use a for loop?
If somewhone can help me please give me also some comment why you use that code.
Thanks in advance

採用された回答

KL
KL 2017 年 11 月 20 日
編集済み: KL 2017 年 11 月 20 日
Try this,
Y_as = [ 165 160 121 ; 59 84 62 ; 106 76 58 ] ;
X_as = categorical({'1. apples & pears' , '2. apples', '3. pears'});
HB = bar(X_as , Y_as , 'group');
a = (1:size(Y_as,1)).';
x = [a-0.25 a a+0.25];
for k=1:size(Y_as,1)
for m = 1:size(Y_as,2)
text(x(k,m),Y_as(k,m),num2str(Y_as(k,m),'%0.2f'),...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
end
end
  1 件のコメント
Arne1234
Arne1234 2017 年 11 月 20 日
Thanks a lot. It works :-)

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

その他の回答 (1 件)

KSSV
KSSV 2017 年 11 月 20 日
  1 件のコメント
Arne1234
Arne1234 2017 年 11 月 20 日
Doesn't work for Group bar

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

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by