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).

 Respuesta aceptada

Adam Danz
Adam Danz el 30 de Jul. de 2020
Editada: Adam Danz el 30 de Jul. de 2020

0 votos

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');
For bar plots that are stacked and grouped, see this answer.

6 comentarios

Adam Danz
Adam Danz el 30 de Jul. de 2020
"I require the some help in correting the circle in pic. I need a little gap between the bars and number. How can i do it?"
Here are two options.
1) Set the vertical alignment to bottom
Note that I've made some other changes to your text params. I've updated my answer to include this recommendation.
th = text(x,y,string(y'),'HorizontalAlignment','Center',...
'VerticalAlignment','bottom')
2) Add additional space between top of bars and text.
This example adds 1% of the y-axis range between bar and text.
offset = range(ylim)*0.01; % <-- 1% of y axis range
th = text(x,y+offset,string(y'),'HorizontalAlignment','Center',...
'VerticalAlignment','bottom')
Milosha Britto Nordbø
Milosha Britto Nordbø el 30 de Jul. de 2020
Thank you. I forgot to use the offset.
Now it works perfectly fine.
Adam Danz
Adam Danz el 30 de Jul. de 2020
Note that if any bar extends to the top of the axes or near the top of the axes, you'll need to extend ylim a bit if you want the text label to fit within the axes.
Milosha Britto Nordbø
Milosha Britto Nordbø el 30 de Jul. de 2020
Noted. Thank you!
Prasad Joshi
Prasad Joshi el 13 de Abr. de 2022
Can you answer this question Adam thanks in advance
Adam Danz
Adam Danz el 13 de Abr. de 2022
Hello @Prasad Joshi, which question?

Iniciar sesión para comentar.

Más respuestas (1)

Milosha Britto Nordbø
Milosha Britto Nordbø el 30 de Jul. de 2020
Editada: Adam Danz el 30 de Jul. de 2020

0 votos

Thank you for tips. I used the below codes and got the follwing results:
bh=bar(Year,ValCha,'stacked');
y=sum(reshape(cell2mat(get(bh','YData')),size(bh,2),[]),1)
x=unique(cell2mat(get(bh','XData')),'stable')
th = text(x,y,string(y'),'HorizontalAlignment','left',...
'VerticalAlignment','middle','rotation',0)
I require the some help in correting the circle in pic. I need a little gap between the bars and number.
How can i do it?

Categorías

Etiquetas

Preguntada:

el 30 de Jul. de 2020

Comentada:

el 13 de Abr. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by