how to boxplot on the same figure?

18 views (last 30 days)
Hydro
Hydro on 29 Dec 2017
Commented: Hydro on 29 Dec 2017
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',{' '})

Answers (1)

Sean de Wolski
Sean de Wolski on 29 Dec 2017
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 Comment
Hydro
Hydro on 29 Dec 2017
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!