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

T
tanisha97 · Mar 16, 2020 · 1.9K views
Question
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 PhD Expert
Answered Aug 24, 2026
>> 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. >>

 
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!