View License

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

» Watch video

Highlights from
Box and whiskers plot (without statistics toolbox)

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

4.6
4.6 | 11 ratings Rate this file 117 Downloads (last 30 days) File Size: 13.9 KB File ID: #42470 Version: 1.0
image thumbnail

Box and whiskers plot (without statistics toolbox)

by

 

03 Jul 2013 (Updated )

Creates nice boxplots from data. You don't need a toolbox. Simple yet fully featured.

| Watch this File

File Information
Description

bplot(X)
This function will create a nice boxplot from a set of data. You don't
need a toolbox.
    bplot(D) will create a boxplot of data D, no fuss.
T = bplot(D) If X is a matrix, there is one box per column; if X is a
              vector, there is just one box. On each box, the central
              mark is the median, the edges of the box are the 25th and
              75th percentiles
              array 'T' for a legend. You can add the legend as legend(T)

T = bplot(D,x) will plot the boxplot of data D above the 'x' value x

T = bplot(D,y,'horiz') will plot a horizontal boxplot at the 'y' value of y

T = bplot(...,'Property', . . . )
T = bplot(...,'PropertyName',PropertyValue, . . . )

% Jitter feature
The boxplot has a cool jitter feature which will help you view each
outlier separately even if two have identical values. It jitters the
points around the other axis so that you can see exactly how many are
there.

% Examples:
bplot(randn(30,3),'outliers')
bplot(randn(30,3),'color','black');
----
X = round(randn(30,4)*5)/5; % random, with some duplicates
T = bplot(X,'points');
legend(T,'location','eastoutside');

% This function was created for inclusion in my larger nhist function.

Acknowledgements

Simple Box Whiskers Plot (For Those Who Don't Have The Statistics Toolbox) inspired this file.

This file inspired Boxplot: Boxplotx Without Toolboxes.

Required Products MATLAB
MATLAB release MATLAB 7.11 (R2010b)
MATLAB Search Path
/
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (17)
18 Jul 2016 Andrew Sciotti

After looking at documentation on box and whisker plots, I am not certain where you got that the wisEdge variable (essentially accounting for outliers or not) is 9% on either side. Currently line 254 looks like:
wisEdge = prctile(x,[percentileNum2 100-percentileNum2])

I would change it to:
wisEdge = [boxEdge(1)-IQR*1.5 boxEdge(2)+IQR*1.5];

as is conventional with many learning resources.

13 Jun 2016 Erik Reinertsen

When trying to use 'box' option for multiple distributions on the same figure, I get this error:

===

Error using get
There is no Annotation property on the Rectangle class.

Error in nhist>bplot (line 2073)
set(get(get(hReg2(ii),'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
% Exclude line from legend

Error in nhist (line 1169)
bplot(logFunc(cellValues{k}),tempY,'color',lineStyleOrder{k},'barwidth',boxplotWidth,'linewidth',linewidth,'horizontal',pointsOrNo,'specialwidth','histmode');

===

I solved it by simply commenting out lines 2070 to 2075:

% % remove all remenants of legends
% if forceNoLegend
% for ii=1:length(hReg2)
% set(get(get(hReg2(ii),'Annotation'),'LegendInformation'),'IconDisplayStyle','off'); % Exclude line from legend
% end
% end

Comment only
06 Apr 2016 Seb Biass

Update on Maider's comment, replace lines 322-331 with Maider's suggestion

Comment only
11 Jan 2016 Hugo

Hugo (view profile)

There is a minor bug in the code, where feeding an all-nan array raises an error instead of being treated as an empty array. The fix is quite simple, move the lines in the section 'check that there is at least some data' after the section 'serialize and remove NANs'.

Comment only
10 Jan 2016 Hugo

Hugo (view profile)

perhaps the documentation could make more clear all NaN's are actually filtered out, this helps for plotting potentialy unequal sized columns next to each other.

24 Dec 2015 Tim

Tim (view profile)

 
22 Dec 2015 idontknow  
19 Aug 2015 Maider

Maider (view profile)

I found the same error than Brian and DKM I found a way to solve it by changing lines 304-313 to

for ii=1:length(hReg)
set(hReg(ii),'HandleVisibility','off')
end

% % remove all remenants of legends
if forceNoLegend
for ii=1:length(hReg2)
set(hReg2(ii),'HandleVisibility','off')
end
end

Comment only
18 Aug 2015 Maider

Maider (view profile)

Thank you very much for submitting this code. Works well, easy to customize and save me ton of time :D

22 Jul 2015 DKM

DKM (view profile)

I've got the same issue as Brian, "no Annotation property on the Rectangle class" error. Worked fine until I upgraded to 2015a. I wonder what changed?

04 Jun 2015 chsharrison

Nice contribution, thank you. Really like that I can overplot box plots on top of other plots, Matlab's boxplot() will not do this.

29 Apr 2015 Brian

Brian (view profile)

When I try to plot a matrix D, larger than a vector, in MATLAB R2015a, I get the following error: (I don't get the error in R2014b)

"Error using get
There is no Annotation property on the Rectangle class.

Error in bplot (line 311)
set(get(get(hReg2(ii),'Annotation'),'LegendInformation'),'IconDisplayStyle','off'); % Exclude line from legend

Error in bplot (line 99)
bplot(x(:,ii),ii,'nolegend');"

When plotting a vector such as: "bplot(z(:,1));" It plots fine, but as soon as we go to a higher dimension (such as: "bplot(z(:,1:2));") I receive the above error.

Comment only
21 Apr 2015 F Z

F Z (view profile)

It's very useful. Is there any way to display values on every plot please?

Comment only
09 Jan 2015 Gavin

Gavin (view profile)

Easy to produce plots that communicate results well

09 Jan 2015 Gavin

Gavin (view profile)

 
15 Oct 2014 Tim

Tim (view profile)

Exactly what I've been looking for. Love the simplicity of the features!

09 Jul 2014 Udit Gupta

It's quite useful. Wish there was a way to perform the analysis when the number of observations for each experiment are not the same.

Updates
16 Sep 2015 1.0

Minor update

Contact us