필터 지우기
필터 지우기

How to plot stacked bar in Matlab with showing each limit ?

조회 수: 1 (최근 30일)
AFAQ AHMAD
AFAQ AHMAD 2015년 11월 27일
댓글: dpb 2015년 11월 28일
Hi
I am looking a code in Matlab to plot the stacked bar graph (like in the picture).
This stacked bar graph is also labelled (inside the graph) with the limits.
I try to solve same problem in excel but the limits labelling is problem?
Please if any one knows the code please share it.

채택된 답변

dpb
dpb 2015년 11월 27일
>> Y = [5 1 2; 8 3 7; 9 6 8; 5 5 5; 4 2 3]; % sample data from bar example...
>> Z=cumsum(Y,2); % bar cumulative heights for positioning
>> X=repmat([1:5].',1,3); % x locations for each Y
>> bar(Y,'stack') % base stacked bar
>> colormap(copper) % change color map
>> arrayfun(@(x,y,z) text(x,y,num2str(z), ...
'horizontalalign','center', ...
'verticalalign','top', ...
'color','w'), ...
X,Z,Y) % write the labels for each subset
Salt to suit colors, locations. Use average of the bar height plus lower to center vertically, for example. >>
  댓글 수: 2
AFAQ AHMAD
AFAQ AHMAD 2015년 11월 28일
Thank you dpb Please tell me how to display the ranges(limits) of each portion of the stacked bar. Thank you
dpb
dpb 2015년 11월 28일
Simplest is probably two steps; first use the bottoms which would be
Ybot=[zeros(1,size(Y,2)); Y(1:end-1,:)];
with X,Z as defined above excepting use 'verticalalign','bottom' to write above that height. Then the above to do the tops. NB: you'll have "issues" with short blocks such as the middle of the leftmost bar that doesn't have enough height to display both elements at the same x-axis position. An alternative would be to use the average height for the y location and write both limits as
num2str([ZL,ZU],'%d - %d')
where ZL,ZU are the two lower, upper array locations described above. You'd then have one call to arrayfun but four dummy arguments instead of three.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by