how to boxplot on the same figure?

조회 수: 18 (최근 30일)
Hydro
Hydro 2017년 12월 29일
댓글: Hydro 2017년 12월 29일
Hello, I am trying to boxplot four data types on the same graph such that A and B remains on the left side, while C & D on the right side of the graph. I want to compare A to B and C to D. Elow is my code but i lose AB plot when i execute plotting for CD. Will highly appreciate if someone can help.
X1= [1.2 2.2 3.2 4.2 5.2];
X2=[1.4 2.4 3.4 4.4 5.4];
X3=[6.2 7.2 8.2 9.2 10.2];
X4=[6.4 7.4 8.4 9.4 10.4];
A=rand(100,10); % Data
B=rand(100,10)+5; % Data
f=figure;
hold on
BoxA = boxplot(A(:,1:5),'positions',X1, 'width',0.18,'notch','on','color','r');
set(gca,'XTickLabel',{' '})
BoxB = boxplot(A(:,6:10),'positions',X2, 'width',0.18,'notch','on','color','b');
set(gca,'XTickLabel',{' '})
BoxC = boxplot(B(:,1:5),'positions',X3, 'width',0.18,'notch','on','color','r');
set(gca,'XTickLabel',{' '})
BoxD = boxplot(B(:,6:10),'positions',X4, 'width',0.18,'notch','on','color','b');
set(gca,'XTickLabel',{' '})

답변 (1개)

Sean de Wolski
Sean de Wolski 2017년 12월 29일
Your xlimits are only showing one of the box plots. I'd also recommend using yyaxis here or perhaps subplots.
X1= [1.2 2.2 3.2 4.2 5.2];
X2=[1.4 2.4 3.4 4.4 5.4];
X3=[6.2 7.2 8.2 9.2 10.2];
X4=[6.4 7.4 8.4 9.4 10.4];
A=rand(100,10); % Data
B=rand(100,10)+5; % Data
f=figure;
yyaxis left
hold on
BoxA = boxplot(A(:,1:5),'positions',X1, 'width',0.18,'notch','on','color','r');
set(gca,'XTickLabel',{' '})
BoxB = boxplot(A(:,6:10),'positions',X2, 'width',0.18,'notch','on','color','b');
set(gca,'XTickLabel',{' '})
yyaxis right
BoxC = boxplot(B(:,1:5),'positions',X3, 'width',0.18,'notch','on','color','r');
set(gca,'XTickLabel',{' '})
BoxD = boxplot(B(:,6:10),'positions',X4, 'width',0.18,'notch','on','color','b');
set(gca,'XTickLabel',{' '})
xlim([0 12])
  댓글 수: 1
Hydro
Hydro 2017년 12월 29일
Thank you Sean, Is there a way to match the color the outliers as like their boxes. right now it is showing al red. Also, I see tickmark on x-axis on the right graph.

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

Community Treasure Hunt

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

Start Hunting!

Translated by