How to fix unallignment of box plot in subplot when using sgtitle?
7 次查看(过去 30 天)
显示 更早的评论
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1005375/image.png)
Thank you
0 个评论
采纳的回答
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
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:
- What happens if you put sgtitle at the end of this code?
- 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.
- If "boxplot" is the issue, you could try the newer "boxchart" (introduced in R2020a)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!