How to fix unallignment of box plot in subplot when using sgtitle?

5 ビュー (過去 30 日間)
Magnus Sejer Lind
Magnus Sejer Lind 2022 年 5 月 20 日
コメント済み: Magnus Sejer Lind 2022 年 5 月 20 日
When I use the 'sgtitle' command to make a title for the subplot figure, becomes the first subplot unalligned with the others. Is there a way to fix this?
'G' is a 105 element long row-vector
...
noa = ones(1,5);
nob = repmat(2,1,5);
noc = repmat(4,1,5);
no = cat(2,noa,nob,noc);
figure(5)
sgtitle('Fracture Energy vs Height')
% Titles
tph = ('3Dd1s1 d1s1 d1s2 d1s4 d2s1 d2s2 d2s4');
tph = split(tph);
G_sub = 0;
for i = 1:length(G)
if rem(i,15)==0
G_sub = G(i-14:i);
subplot(2,4,i/15)
boxplot(G_sub,no)
title(tph(i/15))
xlabel('Height [mm]')
grid
end
end
Output:
Thank you

採用された回答

Cris LaPierre
Cris LaPierre 2022 年 5 月 20 日
Interestingly, if I run your code in a live script, it initially renders incorrectly, but then corrects itself. If I run your code here, it renders misaligned.
I think a reliable way to create this figure so that it renders the same everywhere is to update your code to use tiledlayout and boxchart.
G = rand(1,7*15);
noa = ones(1,5);
nob = repmat(2,1,5);
noc = repmat(4,1,5);
no = cat(2,noa,nob,noc);
figure(5)
t = tiledlayout(2,4);
title(t,'Fracture Energy vs Height')
% Titles
tph = ('3Dd1s1 d1s1 d1s2 d1s4 d2s1 d2s2 d2s4');
tph = split(tph);
G_sub = 0;
for i = 1:length(G)
if rem(i,15)==0
G_sub = G(i-14:i);
nexttile
boxchart(no,G_sub)
title(tph(i/15))
xlabel('Height [mm]')
grid
end
end

その他の回答 (1 件)

Monica Roberts
Monica Roberts 2022 年 5 月 20 日
This looks like a bug. You should contact MATLAB support: https://servicerequest.mathworks.com/mysr/cp_case_new1?bug=true
They will want to know what version of MATLAB you are using, and what is the minimum number of lines you can cut back to replicate the issue (i.e. does it still happen without data? Without boxplot?)
In the meantime, some things you can try:
  1. What happens if you put sgtitle at the end of this code?
  2. If you have a more recent version of MATLAB you could try "tiledlayout" instead of "subplot" (introduced in R2019b). Then you can use "title" with the tiledlayout object instead of sgtitle.
  3. If "boxplot" is the issue, you could try the newer "boxchart" (introduced in R2020a)

カテゴリ

Help Center および File ExchangeHistorical Contests についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by