Producing the same-sized box plots for subfigures

15 次查看(过去 30 天)
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 中查找有关 Axis Labels 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by