필터 지우기
필터 지우기

How to plot grouped stacked bar plot in matlab

조회 수: 86 (최근 30일)
vignesh mohan
vignesh mohan 2022년 2월 7일
댓글: vignesh mohan 2022년 2월 8일
Hai everyone,
I am new to the matlab. I would like to plot stacked bar plot in matlab but i couldn't find the solution to resolve my problem. I hope anyone help me to resolve this issues. I had attached my data sheet with this. In my x-axis season should come like winter, spring, summer, monsoon, autumn, my y axis the values will come in percentage and in that winter bar should contines A, B, C, D all my column will be there like stacked. This below three codes are i tried. Some sample graphs also i added with this.
bar(data.season,data.A,data.season,data.B,data.season,data.C,data.season,data.D,'stacked');
bar(1:4,'stacked');
bar(data,'stacked')
Error using bar (line 103)
Input arguments must be numeric.

채택된 답변

Simon Chan
Simon Chan 2022년 2월 7일
Try this:
data = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/886550/data.xlsx');
G = groupsummary(data,"Season","sum"); % Group them into 5 categories
Percentage = 100*G{:,3:6}./sum(G{:,3:6},2); % Calculate average percentage
b = bar(categorical(G.Season),Percentage,'stacked'); % Stacked bar-chart
ax = gca;
ax.YLim = [1 110];
ypos = transpose(cat(1,b.YEndPoints)-[b(1).YEndPoints/2;diff(cat(1,b.YEndPoints))/2]); % Calculate text positions
text(cat(2,b.XEndPoints),ypos(:),arrayfun(@(x) sprintf('%.1f',x),(cat(2,b.YData)),'uni',0),...
'HorizontalAlignment','center','VerticalAlignment','middle')
text(b(end).XEndPoints,b(end).YEndPoints,arrayfun(@(x) sprintf('N=%d',x),G.GroupCount,'uni',0),...
'HorizontalAlignment','center','VerticalAlignment','bottom');
legend(ax,data.Properties.VariableNames(1:4),'location','eastoutside')
  댓글 수: 3
Simon Chan
Simon Chan 2022년 2월 8일
Then use function findgroups and splitapply
data = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/886550/data.xlsx');
[G,ID] = findgroups(data.Season); % Find how many groups
Percentage = splitapply(@mean,data{:,1:4},G); % Calculate the mean for A,B,C & D
b = bar(categorical(ID),Percentage,'stacked'); % Stacked Bar Chart
ax = gca;
ax.YLim = [1 110];
cumulative = cumsum(Percentage,2); % Calculate cumulative
ypos = cumulative-Percentage/2; % Determine offset to display the values
xpos = transpose(repmat(1:max(G),4,1));
text(xpos(:),ypos(:),arrayfun(@(x) sprintf('%.1f',x),Percentage(:),'uni',0),...
'HorizontalAlignment','center','VerticalAlignment','middle');
Nz = arrayfun(@(x) numel(find(G==x)),1:max(G)); % Number of occurance for each group
text(1:max(G),cumulative(:,end),arrayfun(@(x) sprintf('N=%d',x),Nz,'uni',0),...
'HorizontalAlignment','center','VerticalAlignment','bottom');
legend(ax,data.Properties.VariableNames(1:4),'location','eastoutside')
vignesh mohan
vignesh mohan 2022년 2월 8일
Thank you so much simon Chan
It works good...!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by