View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Plot Groups of Stacked Bars

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

4.875
4.9 | 16 ratings Rate this file 100 Downloads (last 30 days) File Size: 1.69 KB File ID: #32884 Version: 1.0
image thumbnail

Plot Groups of Stacked Bars

by

Evan (view profile)

 

Generate a bar plot with multiple stacks grouped together.

| Watch this File

File Information
Description

Plot a set of stacked bars, but group them according to labels provided.

Params:
     stackData is a 3D matrix (i.e., stackData(i, j, k) => (Group, Stack, StackElement))
     groupLabels is a CELL type (i.e., { 'a', 1 , 20, 'foo' };)

Acknowledgements

This file inspired Machine Learning With Matlab.

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (20)
22 Aug 2016 Suehee Huh

Thanks!!!!

12 Jul 2016 Beckah Rieck

Thanks Evan!

Emma- if you are talking about x-labels for what each bar means, just add a legend:
b = plotBarStackGroups(stackData, Month);
legend([b(1,1), b(2,1), b(2,2)], 'Total Added', 'Closed- Non Quick Hits', 'Closed- Quick Hits');

If you mean Y data labels on the bars, use text:
text(b(1,1).XData', zeros(size(b(1,1).XData')), labelsAddedtoQ);

(that places them at the base).

06 Jul 2016 Ashrar Matin  
07 Jun 2016 Emma Jonasson

Thanks for the script. I would also like to add x-labels for each bar within the groups, anyone who can help me achieve that? Explaining from the example: above the label 'Test', I would like to add labels on the bars, e.g. 'a' 'b' 'c', and so on.

Comment only
07 Jun 2016 Emma Jonasson  
04 May 2016 Samy

Samy (view profile)

thanks for the script, if you don't like the border of the plot being messed up just remove the 'hold on;' in line 20 and add 'if i == 1, hold on; end' after 'h(i,:) = bar(Y, 'stacked');'!

11 Mar 2016 tan yu

tan yu (view profile)

 
14 Jul 2015 Elizabeth  
20 Apr 2015 Oleg Komarov

Oleg Komarov (view profile)

If you have a 3D array A, then reshaping your array and interleaving groups with a row of NaNs will produce the same result:

sz = size(A);
B = [A, NaN(sz(1),1,sz(3))];
B = reshape(permute(B, [2,1,3]),sz(1)*(sz(2)+1),sz(3));
bar(C(1:end-1,:),'stacked')

Comment only
17 Apr 2015 ADITHYAN

Works very well. Thanks a lot evan.

20 Feb 2015 Daniel Paiva

Very good.

18 Jul 2014 Jenny

Jenny (view profile)

 
01 Apr 2014 Vera

Vera (view profile)

 
13 Oct 2013 Ricardo

Very useful function!, thank you!

25 Aug 2013 Tim

Tim (view profile)

Berk, I had the same desire and found it was pretty simple to customize.

After this...
h(i,:) = bar(Y, 'stacked');

I added this...
set(h(i,1),'facecolor','y','edgecolor','k');
set(h(i,2),'facecolor','r','edgecolor','k');
set(h(i,3),'facecolor','c','edgecolor','k');
set(h(i,4),'facecolor','g','edgecolor','k');

...so that the base of each bar is always yellow, etc.

Doubt you'll need this 1 year later, but for the next rookie like myself, could be useful.

Thanks for the elegant and useful function, Evan.

12 Mar 2013 Ben

Ben (view profile)

 
21 Aug 2012 Berk

Berk (view profile)

Thanks, very handy function!

Could you please tell whether we can assign different colors to different stacks?
Currently, all the stack elements have the same color/type. However, I am wonering whether it would be possible to differentiate the different stacks?

Comment only
21 Aug 2012 Berk

Berk (view profile)

 
21 Oct 2011 grega

grega (view profile)

Nice one! Works! Example makes it clear! Thank you!

14 Sep 2011 Evan

Evan (view profile)

Example usage:

NumStacksPerGroup = 3;
NumGroupsPerAxis = 6;
NumStackElements = 4;

% labels to use on tick marks for groups
groupLabels = { 'Test'; 2; 4; 6; 8; -1; };
stackData = rand(NumGroupsPerAxis,NumStacksPerGroup,NumStackElements);

plotBarStackGroups(stackData, groupLabels);
set(gca,'FontSize',18)
set(gcf,'Position',[100 100 720 650])
grid on
set(gca,'Layer','top') % put grid lines on top of stacks

Comment only

Contact us