How to plot grouped stacked bar plot in matlab

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 日

0 投票

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 件のコメント

vignesh mohan
vignesh mohan 2022 年 2 月 7 日
hai Simon chan.
Thanks for your comment
i got this error. I am using matlab 2016a
G = groupsummary(data,"Season","sum");
Undefined function or variable 'groupsummary'.
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 件)

カテゴリ

ヘルプ センター および File ExchangeGraphics Object Properties についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by