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.

 采纳的回答

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 个评论

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'.
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')
Thank you so much simon Chan
It works good...!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Graphics Object Properties 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by