Insert two boxplots into one graph

조회 수: 11 (최근 30일)
Alberto Acri
Alberto Acri 2022년 4월 24일
댓글: Voss 2022년 4월 26일
Hi, I would like to insert the two boxplots into one graph. How can I do this?
Here is the example code I use:
% first set
Q0_1 = -0.3771; % min
Q1_1 = -0.1007; % 25th percentile
Q2_1 = -0.0634; % 50th percentile (median)
Q3_1 = -0.0284; % 75th percentile
Q4_1 = 0.4958; % max
data_1 = [Q0_1 Q1_1 Q2_1 Q3_1 Q4_1];
data_1 = data_1(:, [1 2 2 3 4 4 5]);
boxplot_1 = boxplot(data_1.', 'Whisker', inf);
% SECOND set
Q0_2 = -0.2771; % min
Q1_2 = -0.1117; % 25th percentile
Q2_2 = -0.0428; % 50th percentile (median)
Q3_2 = -0.0311; % 75th percentile
Q4_2 = 0.3954; % max
data_2 = [Q0_2 Q1_2 Q2_2 Q3_2 Q4_2];
data_2 = data_2(:, [1 2 2 3 4 4 5]);
figure()
boxplot_2 = boxplot(data_2.', 'Whisker', inf);
% UNION
A = data_1';
B = data_2';
group = [ones(size(A)); 2 * ones(size(B))];
figure
boxplot([A; B], group)
set(gca,'XTickLabel',{'group A','group B'})
Trying the last few lines of code I get this graph but it doesn't represent what I want. How come?

채택된 답변

Voss
Voss 2022년 4월 24일
If you use the same parameters for the combined boxplot as you did for the individual ones (i.e., use 'Whisker', inf), does that represent what you want?
% first set
Q0_1 = -0.3771; % min
Q1_1 = -0.1007; % 25th percentile
Q2_1 = -0.0634; % 50th percentile (median)
Q3_1 = -0.0284; % 75th percentile
Q4_1 = 0.4958; % max
data_1 = [Q0_1 Q1_1 Q2_1 Q3_1 Q4_1];
data_1 = data_1(:, [1 2 2 3 4 4 5]);
boxplot_1 = boxplot(data_1.', 'Whisker', inf);
% SECOND set
Q0_2 = -0.2771; % min
Q1_2 = -0.1117; % 25th percentile
Q2_2 = -0.0428; % 50th percentile (median)
Q3_2 = -0.0311; % 75th percentile
Q4_2 = 0.3954; % max
data_2 = [Q0_2 Q1_2 Q2_2 Q3_2 Q4_2];
data_2 = data_2(:, [1 2 2 3 4 4 5]);
figure()
boxplot_2 = boxplot(data_2.', 'Whisker', inf);
% UNION
A = data_1';
B = data_2';
group = [ones(size(A)); 2 * ones(size(B))];
figure
boxplot([A; B], group, 'Whisker', inf)
set(gca,'XTickLabel',{'group A','group B'})
  댓글 수: 5
Alberto Acri
Alberto Acri 2022년 4월 26일
perfect! thank you so much!
Voss
Voss 2022년 4월 26일
You're welcome!

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

추가 답변 (0개)

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by