File Exchange

image thumbnail

Generalized Bar Charts

version 1.6 (4.31 KB) by

Stacked, Grouped, Clustered, Floating; and MIXED of any combination

7 Downloads

Updated

View License

Generalization of bar.m function.

Capable of producing:

1. Bar Chart
2. Floating Bar Chart
3. Clustered Bar Chart
4. Floating Clustered Bar Chart
5. Stacked Bar Chart
6. Stacked Clustered Bar Chart
7. (Multiple data sets)

As well as MIXED charts containing any combination of them on the same chart

Can also handle x-coordinate spacing and labeling, missing data, and legends.

EXAMPLES:

SIMPLE BAR CHART
data=rand(1,4);
bar2(data)

BAR CHART with spaced data and labels:
data=rand(1,4);
bar2([1 2 7 8],data,'BASE','XLABEL',{'A','B','C','D'})

FLOATING BAR CHART:
data=rand(2,4);
bar2([1 2 7 8],data,'XLABEL',{'A','B','C','D'})

BAR CHART WITH LOG AXIS AND USER SPECIFIED BASE:
data=rand(1,4);
bar2([1 2 7 8],data,'BASE',1e-3,'XLABEL',{'A','B','C','D'});
set(gca,'Yscale', 'log'), grid on;

CLUSTERED BAR CHART:
data=rand(3,4);
bar2([1 2 7 8],data,'BASE','XLABEL',{'A','B','C','D'})

FLOATING CLUSTERED BAR CHART:
data=rand(6,4); %Six rows representing three vertical [low high]
bar2(data)
legend({'A','B','C'},'Location','BestOutside')

FLOATING CLUSTERED BAR CHART: (with NaNs)
data=rand(6,4); data([3,4],[2 3])=NaN;
bar2(data)
legend({'A','B','C'},'Location','BestOutside')

STACKED BAR CHART:
data=rand(1,4,3); %Note: One row for one vertical column per category
bar2(data,'STACKED')

STACKED CLUSTERED BAR CHART:
data=rand(2,4,3); %Two clusters, both stacked
bar2([1 2 5 6],data,'STACKED','XLABEL',{2001,2002,2005,2006})
legend({'A','B','C','D','E','F'},'Location','BestOutside')

Multiple data sets on same X-coordinate:
data=rand(6,4,2);
data(:,:,2)=data(:,:,2)+max(data(:)); %for illustration
bar2(data,'XLABEL',{'A','B','C','D'})

MIXED CHARTS
(Bar chart + Clustered Bar Chart + Stacked Bar Chart + Floating)
alldata{1}={[1 2 6 10],rand(1,4),'BASE','XLABEL',{'B1','B2','B3','B4'}};
alldata{2}={[3 7 8 12],rand(3,4),'BASE','XLABEL',{'CB1','CB2','CB3','CB4'}};
alldata{3}={[4 5 11 13],rand(1,4,3),'STACKED','XLABEL',{'S1','S2','S3','S4'}};
alldata{4}={9,rand(2,1),'XLABEL',{'FLOAT'}};
bar2(alldata)
legend({'Bar','CB_1','CB_2','CB_3','V_1','V_2','V_3','Float'},'Location','BestOutside')

Comments and Ratings (3)

Mariana

Great code! Can we change color on the bars?

Really great to see a script that supports floating bar charts! I can't seem to change the face or edge color of the bars, though. Is this supported?

Updates

1.6

Updated help section

1.5

Updated H1 line, and description

1.4

14-Dec-2010 Fixed bug: bar2(data) produces simple bar chart

1.3

10-Dec-2010 Modified code to handle mixed charts

1.1

Modified:
6-Dec-2010 Non-zero VAL for BASE (Credit to J.R.G)

MATLAB Release
MATLAB 7.9 (R2009b)
Acknowledgements

Inspired: superbar

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

» Watch video