Clear Filters
Clear Filters

Insert two boxplots into one graph

15 views (last 30 days)
Alberto Acri
Alberto Acri on 24 Apr 2022
Commented: Voss on 26 Apr 2022
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?

Accepted Answer

Voss
Voss on 24 Apr 2022
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 Comments
Alberto Acri
Alberto Acri on 26 Apr 2022
perfect! thank you so much!

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!