How to plot grouped stacked bar plot in matlab
86 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
vignesh mohan
am 7 Feb. 2022
Kommentiert: vignesh mohan
am 8 Feb. 2022
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/886555/image.png)
0 Kommentare
Akzeptierte Antwort
Simon Chan
am 7 Feb. 2022
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 Kommentare
Simon Chan
am 8 Feb. 2022
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')
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Bar Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!