How to plot stacked bar in Matlab with showing each limit ?

Illustration
tanisha97 - 2020-03-16T10:20:13+00:00
Question: How to plot stacked bar in Matlab with showing each limit ?

I am looking a code in Matlab to plot the stacked bar graph (like in the picture). This stacked bar graph is also labelled (inside the graph) with the limits. I try to solve same problem in excel but the limits labelling is problem? Please if any one knows the code please share it.

Expert Answer

Profile picture of Prashant Kumar Prashant Kumar answered . 2025-11-20

>> Y = [5 1 2; 8 3 7; 9 6 8; 5 5 5; 4 2 3];  % sample data from bar example...
>> Z=cumsum(Y,2);                            % bar cumulative heights for positioning
>> X=repmat([1:5].',1,3);                    % x locations for each Y
>> bar(Y,'stack')                            % base stacked bar
>> colormap(copper)                          % change color map
>> arrayfun(@(x,y,z) text(x,y,num2str(z), ...
                         'horizontalalign','center', ...
                         'verticalalign','top', ...
                         'color','w'), ...
                          X,Z,Y)   % write the labels for each subset

Salt to suit colors, locations. Use average of the bar height plus lower to center vertically, for example. >>

 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!