Azzera filtri
Azzera filtri

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

4 visualizzazioni (ultimi 30 giorni)
% 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.

Risposta accettata

Simon Chan
Simon Chan il 21 Feb 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 Commento
Yeasir Mohammad Akib
Yeasir Mohammad Akib il 21 Feb 2022
Thank you for the solution, I followed the same path (resolving it into row vector) to solve the problem later on.

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by