version 1.0.0.0 (13.9 KB) by
Jonathan C. Lansey

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

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.

Jonathan C. Lansey (2021). Box and whiskers plot (without statistics toolbox) (https://www.mathworks.com/matlabcentral/fileexchange/42470-box-and-whiskers-plot-without-statistics-toolbox), MATLAB Central File Exchange. Retrieved .

Created with
R2010b

Compatible with any release

**Inspired by:**
simple box whiskers plot (for those who don't have the statistics toolbox)

**Inspired:**
Boxplot: boxplotx without toolboxes

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

JoeThis works great. Different sized populations can be used as long as you pad the empty spaces with NaN using Nan()

Asher MayIt works fantastic, just see Jose Matute's comment. Thanks very much!

Branislav Dzepinaworks like a charm. legend. thank you

Olivia Booth-HoweNevermind lol!

Jonathan C. LanseyThanks for the comment. Actually I've personally moved over to Python since I don't have a license for Matlab anymore. Hence haven't updated any of these functions in a while :)

Bernard LemayI can't believe Mathworks has put this simple function behind a paywall. When everyone is using Python it will be too late for them to be sorry.

Jose MatuteIt works... just substitute 'Annotation' by 'default' in the line 329

theblueeyeswhitedragonI am getting the following error

Error using get

There is no Annotation property on the Rectangle class.

I have 41x12 matrix as input to the function bplot.

Hanieh BMEwhat if in the X matrix number of elements in column one and two are not the same?

Don KraskyDidn't work for me. I gave 2016b the command bplot(rand(20)) and got a plot with 2 bars, along with an error message. Looks like I'm not the only one with this problem

Error using get

There is no Annotation property on the Rectangle class.

Error in bplot (line 329)

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

Error in bplot (line 97)

bplot(x(:,ii),ii,'nolegend');

Altaf Taqiit worked

Pascal KnappeNikola MarkovichelloSuppose I have a matrix D which is 3 by 4. So that means I will have four box plots. How can I label x-axis for each box plot?

For example I would like to label A for first box plot, B for second and so on.

AdamIf you comment out lines 320-331 the function works fine. Something must have changed with the properties of rectangles or legends.

Abdelilah ErrajiErik ReinertsenI am unsure if Jonathan Lansey still checks this page; the last update was Sep 2015.

I have nhist.m in one of my public repos at https://github.com/erikrtn/dataviz/. GitHub is a better tool for collaborating on and sharing code vs. File Exchange.

Perhaps we can work out of there. Send pull requests and let's keep improving this code!

Subsequent versions of this code should attribute Jonathan for his great work, and link to this File Exchange page.

Jonathan, if you check this and are unpleased and would prefer to host this function in one of your own GitHub repos, we should do that. Let us know. Thanks.

betagamizadoIt did not work for me. I used a simple matrix:

A = [1 2 3 4 5;

2 3 4 5 6;

1 2 3 4 5;

4 2 4 5 2;

3 5 6 7 7]

bplot(A)

%but I get the following error message:

Error using get

There is no Annotation property on the Rectangle class.

Error in bplot (line 329)

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

Error in bplot (line 97)

bplot(x(:,ii),ii,'nolegend');

% also, this yields only two boxplots, but it was supposed to yield five, since the input has five columns. I don't know how to fix that.

I'm using matlab 2016a

Michael ZintVery useful tool!

However using Matlab 2016b it seems that user defined xlabels are not supported any longer. xlabels always defined from 1:n. Entiers for x seem to be ignored. In older versions the xlabels are plotted as specified in vector x.

Does anyone have an idea on how to fix this.

Siwei LOUAndrew SciottiAfter 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.

Erik ReinertsenWhen 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

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

HugoThere 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'.

Hugoperhaps 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.

TimidontknowMaiderI 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

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

DKMI'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?

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

BrianWhen 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.

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

GavinEasy to produce plots that communicate results well

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

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