필터 지우기
필터 지우기

how to create grouped Box plot

조회 수: 4 (최근 30일)
baran
baran 2014년 9월 14일
댓글: dasilvarosa 2017년 10월 14일
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
baran
baran 2014년 9월 14일
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 2017년 10월 14일
Did you solve this error? I'm encountering the same problem.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2014년 9월 14일
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
baran
baran 2014년 9월 14일
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 2014년 9월 14일
편집: Star Strider 2014년 9월 14일
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’.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by