Size of subplots in figure is not the same

29 ビュー (過去 30 日間)
Inti Vanmechelen
Inti Vanmechelen 2022 年 2 月 9 日
コメント済み: Adam Danz 2022 年 2 月 9 日
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 件のコメント
Inti Vanmechelen
Inti Vanmechelen 2022 年 2 月 9 日
That is the odd thing, it does not... I have used the exact same code for all subplots.
DGM
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
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
If you use TiledLayout instead of subplot, the axes position are better controlled.
figure()
tlo = tiledlayout(3,5);
ax = nexttile(tlo);
boxplot(ax, rand(5,6))
% (...)
ax2 = nexttile(tlo);
boxplot(ax2, rand(5,6))
% (...) etc....
  2 件のコメント
Inti Vanmechelen
Inti Vanmechelen 2022 年 2 月 9 日
PositionConstraint gave me an error but the tiledlayout option did give me want I wanted.
Thanks a lot!
Adam Danz
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 ExchangeAxes Appearance についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by