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