![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/339241/image.png)
Labels to a stacked bar
41 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Milosha Britto Nordbø
am 30 Jul. 2020
Kommentiert: Adam Danz
am 13 Apr. 2022
I am trying to add labels for a stacked bar and need to help. I managed to add label to each part of the stack but unable to add at the total level (circled in red).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/339232/image.png)
0 Kommentare
Akzeptierte Antwort
Adam Danz
am 30 Jul. 2020
Bearbeitet: Adam Danz
am 30 Jul. 2020
Here's a demo.
% Create stacked bar plot
x = randi(3,20,5);
h = bar(x,'stacked');
% Get the (x,y) coordinates for the top of each bar stack
y = sum(reshape(cell2mat(get(h', 'YData')),size(h,2),[]),1);
x = unique(cell2mat(get(h', 'XData')),'stable')
% Define labels
labels = num2cell(char(((1:size(x,1))+64)'))'; % 'A' 'B' 'C' ...
% Plot the text labels
offset = range(ylim)*.01;
th = text(x,y+offset,labels,'HorizontalAlignment','left',...
'VerticalAlignment','middle','rotation',90)
If you don't want to rotate the text labels,
th = text(x,y,string(y'),'HorizontalAlignment','Center',...
'VerticalAlignment','bottom');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/339241/image.png)
6 Kommentare
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Annotations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!