Size of subplots in figure is not the same
103 次查看(过去 30 天)
显示 更早的评论
I have attached a figure in which I want to plot 13 subplots.
I have used
subplot(5,3,n)
to do so.
For some reason, matlab makes plot n°8 smaller than all the other plots.
Does anyone know why this happens or what would the most straightforward way to fix this?
Thanks!
6 个评论
DGM
2022-2-9
编辑:DGM
2022-2-9
Something must be different. The other plots don't have ylabels, and there's no title.
Without knowing what exactly happened, all I can say is to use the geometry of the neighboring axes to fix the errant one(s).
open BOXPLOTS.fig
hax = get(gcf,'children');
P0 = vertcat(hax.Position);
for k = 1:numel(hax)
hax(k).Position(3:4) = P0(5,3:4);
end
P1 = vertcat(hax.Position);
for k = 1:numel(hax)
hax(k).Position(1) = P0(k,1)-(P1(k,3)-P0(k,3));
end
That will just ignore the cause and simply resize all the axes to be the same size.
采纳的回答
Adam Danz
2022-2-9
The cause of the problem is difficult to troubleshoot without having a reproducible example. Here are two workarounds.
Set PositionConstraint
I think this should fix the problem but since we don't have a reproducible example, I can't be sure. Set the PositionConstraint of the axes to innerposition before applying axis labels etc. As explained in the documentation, this will keep the inner axis position constant when adding labels etc.
figure()
ax = subplot(3,5,1); % <--------------------------use axis handles!
boxplot(ax, rand(5,6))
set(ax,'PositionConstraint','innerposition') % <---set PositionConstraint
hold(ax,'on') % <----------------------------------hold on
yt = get(ax, 'YTick');
axis([xlim 0 ceil(max(yt)*1.2)])
xt = get(ax, 'XTick');
% plot(ax, xt([1 2]), [1 1]*max(yt)*1.1, '-k', mean(xt([1 2])), max(yt)*1.15, '*k')
% plot(ax, xt([3 4]), [1 1]*max(yt)*1.1, '-k', mean(xt([3 4])), max(yt)*1.15, '*k')
hold(ax,'off')
xticklabels(ax,{'DCP RF','TD RF','DCP RGV','TD RGV','DCP RS','TD RS'});
% a = get(ax,'XTickLabel'); % <-----------------Redundant to the line above
% set(gca,'XTickLabel',a,'fontsize',8)
set(ax,'fontsize',8)
ylabel(ax,'Standard deviation (°)','FontSize',11);
title(ax,'Elevation plane');
Used TiledLayout
figure()
tlo = tiledlayout(3,5);
ax = nexttile(tlo);
boxplot(ax, rand(5,6))
% (...)
ax2 = nexttile(tlo);
boxplot(ax2, rand(5,6))
% (...) etc....
2 个评论
Adam Danz
2022-2-9
If you're using a Matlab release prior to R2020a, then use ActivePositionProperty rather than PositionConstraint. Otherwise, if you share the error and would like to fix it, I can help. But it sounds like TiledLayout worked for you (in also controls the innerposition property).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!