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

6 vues (au cours des 30 derniers jours)
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

Réponse acceptée

Cris LaPierre
Cris LaPierre le 20 Mai 2022
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

Plus de réponses (1)

Monica Roberts
Monica Roberts le 20 Mai 2022
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)

Catégories

En savoir plus sur Historical Contests dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by