Borrar filtros
Borrar filtros

Stacked Bar chart using structure, displaying putting values on each bar

4 visualizaciones (últimos 30 días)
% d7 = [0.1 0.2 0.15 0.2]
% d1= [0.3 0.2 0.15 0.15]
% d2= [0.2 0.15 0.15 0.15]
% d3= [0.1 0.15 0.1 0.15]
% d4= [0.1 0.1 0.2 0.15]
% d5= [0.1 0.1 0.05 0.15]
% d6= [0.1 0.1 0.2 0.05]
aor= [22.469 21.973 19.7 16.278]
%diameter = [0.3 0.2 0.15 0.15, 0.2 0.15 0.15 0.15,0.1 0.15 0.1 0.15, 0.1 0.1 0.2 0.15, 0.1 0.1 0.05 0.15, 0.1 0.1 0.2 0.05, 0.1 0.2 0.15 0.2];
diameter = [0.3 0.2 0.1 0.1 0.1 0.1 0.1; 0.2 0.15 0.15 0.1 0.1 0.1 0.2; 0.15 0.15 0.1 0.2 0.05 0.2 0.15; 0.15 0.15 0.15 0.15 0.15 0.05 0.2]
b = bar(aor,diameter, 'stacked')
xtickformat('%.2f')
grid on
newaor=aor.'
newarray = repmat(newaor,1,7)
text(newarray, cumsum(diameter),compose('%0.2f', cumsum(diameter)),'HorizontalAlignment','center','VerticalAlignment','top')
axis([14 24 0 2])
Hi,
I want to show the cumulative value of y in the stacked bar using the given data value provide. Could anyone please give me a hint for this? Thank you.

Respuesta aceptada

Simon Chan
Simon Chan el 21 de Feb. de 2022
The positions of the text should be in a row or column vector. Hence reshape it and will work.
While for the y-position of the text, you may adjust the margin to put the text in the required position. I use 0.06 in the following example.
Of course, you may also modify the fontsize or font color if needed.
d7 = [0.1 0.2 0.15 0.2];
d1= [0.3 0.2 0.15 0.15];
d2= [0.2 0.15 0.15 0.15];
d3= [0.1 0.15 0.1 0.15];
d4= [0.1 0.1 0.2 0.15];
d5= [0.1 0.1 0.05 0.15];
d6= [0.1 0.1 0.2 0.05];
aor= [22.469 21.973 19.7 16.278];
diameter = [0.3 0.2 0.1 0.1 0.1 0.1 0.1; 0.2 0.15 0.15 0.1 0.1 0.1 0.2; 0.15 0.15 0.1 0.2 0.05 0.2 0.15; 0.15 0.15 0.15 0.15 0.15 0.05 0.2];
b = bar(aor,diameter, 'stacked');
xtickformat('%.2f')
grid on
xpos=reshape(repmat(aor,size(diameter,2),1),[],1); % Make it to a column vector
ypos=reshape(cumsum(diameter'),[],1); % Make it to a column vector
text(xpos, ypos+0.06,arrayfun(@(x) sprintf('%0.2f',x),ypos,'uni',0),'HorizontalAlignment','center','VerticalAlignment','top','FontWeight','bold','FontSize',12)
axis([14 24 0 1.2]);
  1 comentario
Yeasir Mohammad Akib
Yeasir Mohammad Akib el 21 de Feb. de 2022
Thank you for the solution, I followed the same path (resolving it into row vector) to solve the problem later on.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Bar Plots en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by