필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
% 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.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by