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.

 Accepted Answer

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 Comments

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...!

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!