Hi everyone
How can I compaine two boxplots in the same figure for this code?
figure
sgtitle('main graph')
subplot(1,2,1);
boxplot(X,'first')
ylabel('timer')
grid on
subplot(1,2,2);
boxplot(Y,'Second')
ylabel('timer')
grid on
I've tried used solution in below link but still I have problem:
I've tried:
figure
hold on
x=[X;Y];
boxplot(x(1,:),'first')
boxplot(x(2,:),'secondl')
hold off
ylabel('timer')
title('main graph')
also I've tried this solution but still there are error
= [16 20 15 17 22 19 17]';
B = [22 15 16 16 16 18]';
C = [23 9 15 18 13 27 17 14 16 15 21 19 17]';
group = [ ones(size(A));
2 * ones(size(B));
3 * ones(size(C))];
figure
boxplot([A; B; C],group)
set(gca,'XTickLabel',{'A','B','C'})

댓글 수: 4

Adam Danz
Adam Danz 2022년 2월 11일
Welcome to the forum @NOUF ALHARBI. I've formatted your code using the the rich text editor but I haven't changed any of it. You'll notice some errors in red which are probably copy-paste errors.
Anyway, it's unclear how you would like to combine the boxplots and how many are in each group. An image would be nice, or at least a description.
Using a grouping variable is one option. Another option is using the "positions" property.
NOUF ALHARBI
NOUF ALHARBI 2022년 2월 11일
Hi Adam thank you so much,
I've two array I can put them in subplot as boxplot but I need to put it them in the same figue.
Adam Danz
Adam Danz 2022년 2월 11일
Thanks, but it's still not clear. Let's say subplot A has 3 boxs and subplot B has 3 boxs. Do you want
[AB AB AB] or [ABABAB]
or
[AAA BBB] or [AAABBB]?
Adam Danz
Adam Danz 2022년 2월 11일
NOUF ALHARBI's answer moved here as a comment
----------------------------------------------------------------------
Subplot A has 1 box and subplot B has 1 box so just I want [A B]
but apeared to me like this:

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

 채택된 답변

Adam Danz
Adam Danz 2022년 2월 11일

0 개 추천

Method 1: set position along x-axis
Use the positions property in boxplot to set the box x-coordinates.
A = rand(10,1);
B = rand(8,1);
hold on
boxplot(A, 'Positions', 1)
boxplot(B, 'Positions', 2)
% Set desired xtick, xticklabels, and xlim
set(gca,'XTickMode','auto','XTickLabelMode','auto','XLimMode','auto')
Method 2: Combine columns of data
Vectors must have the same length. Pad shorter vectors with NaN values to equate lengths, if necessary.
A = rand(10,1);
B = rand(10,1);
data = [A(:),B(:)];
figure
boxplot(data)

댓글 수: 4

NOUF ALHARBI
NOUF ALHARBI 2022년 2월 11일
Many thanks Adam Danz, but how can I change names in x-coordinates? I want right the name of each .
I used this
A = rand(10,1);
B = rand(8,1);
hold on
boxplot(A, 'Positions', 1)
boxplot(B, 'Positions', 2)
% Set desired xtick, xticklabels, and xlim
set(gca,'XTickMode','auto','XTickLabelMode','auto','XLimMode','auto')
Adam Danz
Adam Danz 2022년 2월 11일
For method 1
set(gca,'XTick',1:2,'XTickLabel',{'Group 1', 'Group 2'},'XLim', [0.5,2.5])
For method 2
boxplot(data,'labels',{'Group1','Group2'})
NOUF ALHARBI
NOUF ALHARBI 2022년 2월 11일
Many thanks Adam Danz
Adam Danz
Adam Danz 2022년 2월 11일
Happy to help!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

질문:

2022년 2월 11일

댓글:

2022년 2월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by