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?

 Akzeptierte Antwort

Voss
Voss am 24 Apr. 2022

0 Stimmen

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 Kommentare

ok thanks! It goes well!
In the UNION part I had written this code:
boxplot([A; B], group)
instead of this:
boxplot([A; B], group, 'Whisker', inf)
Alberto Acri
Alberto Acri am 26 Apr. 2022
Bearbeitet: Alberto Acri am 26 Apr. 2022
May I ask if you know how to set the background color for each boxplot?
What I want to achieve is similar to this code:
data = rand(100, 4);
x = 1:4;
colors = rand(4, 3);
boxplot(data, x);
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),colors(j,:),'FaceAlpha',.5);
end
But manually setting the color type.
That code should work ok. I modified it to use the Tags of the boxplot lines instead of findobj to find the blue box outlines. There are different things you can do, depending on exactly how it should look.
% 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]);
% 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]);
% UNION
A = data_1';
B = data_2';
group = [ones(size(A)); 2 * ones(size(B))];
b_union = boxplot([A; B], group, 'Whisker', inf);
set(gca,'XTickLabel',{'group A','group B'});
% get the lines that outline the boxes, using the lines' Tags:
h = b_union(strcmp(get(b_union,'Tag'),'Box'));
% specify the colors you want:
colors = [ ...
0.6 0 0.2; ...
0 0.6 0.2; ...
]
colors = 2×3
0.6000 0 0.2000 0 0.6000 0.2000
% make patches for the box backgrounds:
% (must be semi-transparent so as not to
% obscure the red line inside the box)
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),colors(j,:),'FaceAlpha',.5);
end
Alberto Acri
Alberto Acri am 26 Apr. 2022
perfect! thank you so much!
Voss
Voss am 26 Apr. 2022
You're welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2021b

Gefragt:

am 24 Apr. 2022

Kommentiert:

am 26 Apr. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by