Hi
In the following code:
How can I show each bar its value
clc, clear
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y)
for i=1:1:length(y)
text(i:i,y(i)',num2str(y(i)' ,'%0.2f')),'HorizontalAlignment','center','VerticalAlignment','bottom';
box off ;
end
I will note that I have the 2019 version that does not support the above solution
TNX :)
xtips1 = b(1).XEndPoints;
ytips1 = b(1).YEndPoints;
labels1 = string(b(1).YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')

 採用された回答

Adam Danz
Adam Danz 2021 年 1 月 8 日
編集済み: Adam Danz 2021 年 12 月 10 日

0 投票

How to label bar heights for non-stacked bar plots
This demo adds labels above each bar.
To add the labels at the top but within the bars, change VerticalAlignment to Top.
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
h = bar(y);
% Get bar centers (tested in 19a)
xCnt = get(h(1),'XData')' + cell2mat(get(h,'XOffset'))'; % XOffset is undocumented!
% For matlab R2019b or later,
% xCnt = vertcat(h.XEndPoints)';
% Apply labels
text(xCnt(:),y(:),compose('%d',y(:)),'HorizontalAlignment','center','VerticalAlignment','bottom')
% Adjust height if tallest label is outside of axes
ylim([min(ylim),min(ylim)+range(ylim)*1.05])
How to label bar heights for stacked bar plots

4 件のコメント

Shahar ben ezra
Shahar ben ezra 2021 年 1 月 8 日
Amazing thanks!
Shahar ben ezra
Shahar ben ezra 2021 年 1 月 10 日
Hi
I noticed that I have a problem with the categorical function
Do you know why it does not work with her?
clc, clear
x = categorical({'Small','Medium','Large','Extra Large'});
x = reordercats(x,{'Small','Medium','Large','Extra Large'});
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
h = bar(x,y);
% Get bar centers (rested in 19a)
xCnt = get(h(1),'XData')' + cell2mat(get(h,'XOffset'))'; % XOffset is undocumented!
% Apply labels
text(xCnt(:),y(:),compose('%d',y(:)),'HorizontalAlignment','center','VerticalAlignment','bottom')
% Adjust height if tallest label is outside of axes
ylim([min(ylim),max(ylim)*1.05])
Error message:
Undefined function 'plus' for input arguments of type 'categorical'.
Error in bar plot (line 11)
xCnt = get(h(1),'XData')' + cell2mat(get(h,'XOffset'))'; % XOffset is undocumented!
TNX again
Adam Danz
Adam Danz 2021 年 1 月 11 日
It's much more difficult with categorical variables.
The error is telling you that you can't just apply math to categories. For example, what's small+2?
I suggest you don't use categories and instead, just XTickLabels.
xTickLabels = {'Small','Medium','Large','Extra Large'};
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
h = bar(y);
set(gca,'XTick',1:size(y,1),'XTickLabel',xTickLabels)
% Get bar centers (rested in 19a)
xCnt = get(h(1),'XData')' + cell2mat(get(h,'XOffset'))'; % XOffset is undocumented!
% Apply labels
text(xCnt(:),y(:),compose('%d',y(:)),'HorizontalAlignment','center','VerticalAlignment','bottom')
% Adjust height if tallest label is outside of axes
ylim([min(ylim),max(ylim)*1.05])
Shahar ben ezra
Shahar ben ezra 2021 年 1 月 13 日
This is the result I wanted
For me XTickLabelit's is great
thank you very much!

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2019a

質問済み:

2021 年 1 月 8 日

編集済み:

2021 年 12 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by