How to align position of titles in subplots?

조회 수: 36 (최근 30일)
Tomaszzz
Tomaszzz 2025년 1월 30일
이동: Walter Roberson 2025년 2월 9일
Hi all,
I have the following problem as can be seen in the image below, where position of titles in the subplots is misaligned. I want them to be aligned and do not cover x labels of some of the plots above. This do not occur if I make a figure with just one row, the more rows rthe more misaligned it is.
The code to create this is as follows (just first three subplots) is below the figure. Can you help please? if possible, i would prefer not to align titles manually.
hfig = figure ('color','w');
subplot(531);
h(1) = plot(bf(:,7),'LineWidth',1.2); hold on; grid on
Unrecognized function or variable 'bf'.
patch([x; flip(x)], [bf_high; flip(bf_low)], 'b', 'FaceAlpha',0.2, 'EdgeColor','none');
title('A. Hip Y');
ax = gca; ax.TitleHorizontalAlignment = 'left';
ylabel('(°)'); box off
subplot (532); t31i.plot(); grid on; ylim([-5 5]);
title('ML vs BF'); ax = gca; ax.TitleHorizontalAlignment = 'left';
subplot (533); t32i.plot(); grid on; ylim([-5 5]);
title('ML vs CV'); ax = gca; ax.TitleHorizontalAlignment = 'left'; spmi.plot_p_values();
%remaining subplots as above
picturewidth = 10.5;
hw_ratio = 1.3;
set(findall(hfig, '-property', 'FontSize'),'FontSize', 8 );
fontname(hfig, 'Arial');
set(findall(hfig, '-property', 'Box'),'Box', 'off' );
set(hfig, 'Units', 'centimeters', 'Position',[ 3 3 picturewidth hw_ratio*picturewidth]);
pos = get(hfig, 'Position');
set(hfig, 'PaperPositionMode', 'Auto', 'PaperUnits', 'centimeters', 'PaperSize', [pos(3), pos(4)]);
align_Ylabels(figure(1))
  댓글 수: 2
Matt J
Matt J 2025년 1월 30일
Your code doesn't run, see error messages above.
dpb
dpb 2025년 1월 30일
편집: dpb 2025년 1월 30일
for i=1:15
hAx(i)=subplot(5,3,i);
plot(rand(10,1))
title(num2str(i,'Figure %d'))
hAx(i).TitleHorizontalAlignment='left';
end
It appears it has to do with ticks/tick labels; Originally, the above code looked to be fine but the couple of cases such as 1, 12, 14 didn't occur. While your code seems to always set the ylim, I'd look into that alspect. It may be you've uncovered a "feature".
Let's try the same exercise withouth the "Left" positioning...since I didn't save the opening can't reproduce the above RNV sequence so we'll make another response...

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

채택된 답변

dpb
dpb 2025년 1월 30일
이동: Walter Roberson 2025년 2월 9일
for i=1:15
hAx(i)=subplot(5,3,i);
plot(rand(10,1))
title(num2str(i,'Figure %d'))
%hAx(i).TitleHorizontalAlignment='left';
end
Well, that's interesting! It appears the spacing of the title above the axis is much more compact and remains at the same location with the default centered position regardless of whether the axis ticks match the limits or not -- see 3 above.
I think you have discovered a "quality of implementation" wart.
You might try the new replacement of tiledlayout and see if the symptoms go away.
  댓글 수: 3
Adam Danz
Adam Danz 2025년 1월 30일
이동: Walter Roberson 2025년 2월 9일
Here's a reproducible version that sets figure size and rng (R2024b).
The title placement is adjusted when the upper y-tick extends beyond the plot box.
figure('Position',[100 100 1024 562])
rng default
t=tiledlayout(5,3,'TileSpacing','tight','Padding','tight');
for i=1:15
hAx=nexttile(t);
plot(rand(10,1))
title(num2str(i,'Figure %d'))
hAx.TitleHorizontalAlignment='left';
end
The title position would also be affected by adding a secondary label to the y-axis.
figure()
t = tiledlayout(1,2,'TileSpacing','tight','Padding','tight');
plot(nexttile(), rand(10,1))
title('Axes 1');
set(gca,TitleHorizontalAlignment='left');
plot(nexttile(), rand(10,1))
title('Axes 2');
set(gca,TitleHorizontalAlignment='left');
ysecondarylabel('x10')
Generally tiledlayout offers better handling of position than subplot. That should address the title/tick overlap. To address title position differences caused by y-ticks, ensure that your axes all have ticks at the upper limit (or lower limit if YDir is reversed). That can be achieved by setting ylim or ytick.
Tomaszzz
Tomaszzz 2025년 2월 5일
이동: Walter Roberson 2025년 2월 9일
Thanks, tiledlayout does help, happy to accept the answer

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by