Producing the same-sized box plots for subfigures

조회 수: 12 (최근 30일)
yp78
yp78 2021년 10월 26일
댓글: yp78 2021년 10월 26일
I want to produce one figure consisting of 4 by 3 =12 subfigures in the same size, in which the figures in the first column only display the tile and y-axes labels. However, my current code produces a figure where the size of the figures in the first colum is different from the rest of columns.
How can I fix this?
% Just generating a random data to plot the box plots
% I referred to this link: https://ch.mathworks.com/help/stats/boxplot.html
x1 = rand(5,1);
x2 = rand(10,1);
x3 = rand(15,1);
x = [x1; x2; x3];
g1 = repmat({'One'},5,1);
g2 = repmat({'Two'},10,1);
g3 = repmat({'Three'},15,1);
g = [g1; g2; g3];
%%
for i=1:12
subplot(4,3,i)
boxplot(x,g)
if i < 4
title(label{j})
end
if mod(i,3)==1
ylabel('Data');
end
end
The output looks like this.

채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 10월 26일
One approach is just to use a dummy y-axis label for the plots in the 2nd and 3rd columns:
if mod(i,3)==1
ylabel('Data');
else
ylabel(' '); % dummy y-axis label
end

추가 답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 10월 26일
Since your y-axis labels are the same for all plots, consider using a tiled layout. I also use the newer boxchart function, which has slightly different syntax.
% Just generating a random data to plot the box plots
% I referred to this link: https://ch.mathworks.com/help/stats/boxplot.html
x1 = rand(5,1);
x2 = rand(10,1);
x3 = rand(15,1);
x = [x1; x2; x3];
g1 = repmat({'One'},5,1);
g2 = repmat({'Two'},10,1);
g3 = repmat({'Three'},15,1);
g = [g1; g2; g3];
%%
t = tiledlayout(4,3);
for i=1:12
nexttile
boxchart(categorical(g),x)
end
ylabel(t,'Data')
title(t,'360d')
  댓글 수: 1
yp78
yp78 2021년 10월 26일
Thank you for the nice suggestions!
I oversimplified my question; I actually needed to label y-axes differently for each row. I will follow your suggestion in other occasions :)

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

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by