Clear Filters
Clear Filters

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

4 views (last 30 days)
% 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.

Accepted Answer

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

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!