how to create grouped Box plot

3 views (last 30 days)
baran
baran on 14 Sep 2014
Commented: dasilvarosa on 14 Oct 2017
hi, i want to create a box plot for my data. my data is in the excel file, with two sheet. each sheet have 10 column. i want to grouped first column in two sheet as box plot parameter 1. and second column in two sheet as parameter 2 and so on . i write this code but it dont work, please guide me . and is there any way that each group have different colors. thanks so much
% code
clear
clc
fontsize=14;
s1 =xlsread('data.xlsx','s1');
s2=xlsread('data.xlsx','s2');
h={s2,s1};
figure,
boxplot(h,'Labels',{'par1','par2','par3','par4','par5','par6','par7','par8','par9','par10'})
  3 Comments
baran
baran on 14 Sep 2014
the error is:
Error using boxplot>straightenX (line 893)
'X' parameter must be a numeric vector or matrix.
Error in boxplot (line 273) [xDat,gDat,origRow,xlen,gexplicit,origInd,origNumXCols] = straightenX(x,g);
Error in Copy_of_boxplott2 (line 8) boxplot(h,'Labels',{'par1','par2','par3','par4','par5','par6','par7','par8','par9','par10'
dasilvarosa
dasilvarosa on 14 Oct 2017
Did you solve this error? I'm encountering the same problem.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 14 Sep 2014
Assuming ‘s1’ and ‘s2’ are column vectors or column-wise data matrices, change ‘h’ to:
h=[s2,s1];
(using square brackets [] to indicate a matrix rather than curly brackets {} indicating a cell array) and see if that improves things. If you are intending a cell array because ‘s1’ and ‘s2’ do not have the same row length, you have to resolve that problem first.
  3 Comments
baran
baran on 14 Sep 2014
can i use this code instead????
% code
clear
clc
fontsize=14;
s1 =xlsread('data.xlsx','s1');
s2=xlsread('data.xlsx','s2');
x=[s1(:,1), s2(:,1), s1(:,2), s2(:,2), ...]
group=[1,1,2,2, ...]
figure,
boxplot(x, group)
thanks
Star Strider
Star Strider on 14 Sep 2014
Edited: Star Strider on 14 Sep 2014
As I mentioned in my Answer, ‘If you are intending a cell array because ‘s1’ and ‘s2’ do not have the same row length, you have to resolve that problem first.’
Fortunately, boxplot (and several Statistics Toolbox functions) ignore ‘NaN’ values, so create a ‘NaN’ matrix and then fill its columns with ‘s1’ and ‘s2’ respectively:
s1 = randi(10, 15, 1);
s2 = randi(12, 20, 1);
s3 = NaN(max([length(s1), length(s2)]), 2); % Create ‘NaN’ Array
s3(1:size(s1,1),1) = s1; % Copy ‘s1’ to first column
s3(:,2) = s2; % Copy ‘s2’ to second column
figure(1)
boxplot(s1)
figure(2)
boxplot(s2)
figure(3)
boxplot(s3)
Compare the three plots to demonstrate that ‘s1’ is the same even if it is padded with ‘NaN’ values. This should work as written with your data. It automatically adjusts to differences in the row length of ‘s1’ and ‘s2’.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!