Bug in boxplot?

25 views (last 30 days)
Francisco
Francisco on 6 Jan 2016
Commented: Alan on 20 Nov 2020
I get what it seems a weird behavior from boxplot. If I have an array with a single column, and specify groups as the scalar 1, AND specify ANY additional parameters, I get an error (see example below). The error is about parameter 'Orientation' which is not the one specified. However, if I don't specify additional parameters OR if the scalar is anything else (say 2, or a cell) there is no error. Is this a bug? Any suggestions about it?. I understand the syntax may seem weird but I need it for a function to plot nicer boxplots.
Example:
y= normrnd(0,1,100,1);
boxplot(y)
no problem
boxplot(y,1)
no problem
boxplot(y,1,'symbol','.')
Error using boxplot>parseArgs (line 591)
'.' is not a valid value for the 'Orientation' argument. The value must be 'horizontal' or 'vertical'.
Error in boxplot (line 251)
[ax,x,g,notch,symbol,orientation,whisker,labels,labelverbosity, ...
--
boxplot(y,2,'symbol','.')
no problem
boxplot(y,{1},'symbol','.')
no problem
???

Accepted Answer

Sudhanshu Bhatt
Sudhanshu Bhatt on 8 Jan 2016
Hi Francisco,
I understand the issue you are facing. The BOXPLOT documentation states that: boxplot(X,G) specifies one or more grouping variables G, producing a separate box for each set of X values sharing the same G value or values. Grouping variables must have one row per element of X, or one row per column of X.
There are two possible scenarios for the arguments that BOXPLOT takes:
Scenario 1) First Argument to BOXPLOT function is a vector:
In this case, the second argument also has to be a vector of the same size. So for example:
>> y = randn(100,1); boxplot(y,ones(size(y)),'symbol','.')
Scenario 2) First argument to the BOXPLOT function is a matrix:
It is also intended that BOXPLOT function works if second argument is a matrix, and in that case you can give a value for each column:
>> y = randn(100,2); boxplot(y,[1 4],'symbol','.')
However, it is an edge case where y is a single-column matrix. (i.e. X = 100x1 matrix )It qualifies as a vector, so the function expects the first syntax. The reason for the failure is that there is an older syntax for BOXPLOT function which is still working:
BOXPLOT(X,NOTCH,SYM,VERT,WHIS)
When the function sees a second input that looks more like a valid NOTCH value rather than either one row per vector element or one value per matrix column and since it does not recognize that a valid value, it returns an error message.
Please note, that it requires MathWorks Account to access the link.
So, the intended current and correct syntax is to provide one grouping value per element when X is a vector.
In a situation where code may receive an X that is intended to be treated as a matrix but may just happen to have one column, you can use the syntax having the grouping variables in a cell array, for example:
>> y = randn(100,1); boxplot(y,{1},'symbol','.')
>> y = randn(100,2); boxplot(y,{1:2},'symbol','.')
Hope this helps.
Thanks
Sudhanshu Bhatt

More Answers (1)

Alan
Alan on 13 Nov 2020
I attach a file which explains the serious issues with boxplot for R2019b (and maybe other versions). can you help please. See attached pdf for full details
Alan
  2 Comments
Alan
Alan on 20 Nov 2020
yes I had been planning to. Thanks for the tip, I get this output
C:\Users\Alan Pickering\Google Drive\matlab code\work in EP docs Matlab\mcmc\boxplot.m
C:\Program Files\MATLAB\R2019b\toolbox\stats\stats\boxplot.m % Shadowed
I can see that I have an (old) mcmc toolbox on my machine which has a command called boxplot in it.
removing that and all works fine.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!