Ploted graphs in tiled layout extends beyond figure

4 次查看(过去 30 天)
Hi all, the problem I have is pretty self-explanatory. I am trying to used tiledlayout in order to produce 5 plots as shown by the code below.
The problem is however, that the last three plots are extending outside the figure boundary for some reason.
clear ; clc
x = linspace(0,30);
y = sin(x/2);
[X,Y,Z] = peaks(20);
t1 = tiledlayout(3, 1, 'TileSpacing', 'Compact') ;
t2 = tiledlayout(t1, 1, 2, 'TileSpacing', 'Compact') ;
t3 = tiledlayout(t1, 3, 1, 'TileSpacing', 'Compact') ;
t3.Layout.Tile = 2 ;
t3.Layout.TileSpan = [2 3] ;
% Plot top two figures
nexttile(t2)
contour(X, Y, Z)
nexttile(t2)
contour(X, Y, Z)
title(t2, 'Common Title')
xlabel(t2, 'Common xlabel')
ylabel(t2, 'Common ylabel')
% Plot bottom three figures
nexttile(t3)
plot(x, y)
nexttile(t3)
plot(x, y)
nexttile(t3)
plot(x, y)
title(t3, 'Common title')
xlabel(t3, 'Common xlabel')
ylabel(t3, 'Common ylabel')
The reason I am particularly keen on using tiledlayout is because of the common legends and titles functionality.
Thanks for your help in advance.

采纳的回答

Adam Danz
Adam Danz 2023-2-10
The problem is that t1 has a layout of 3x1 but t3 which is nested in t1 is set to a span of 2x3.
t3.Layout.TileSpan = [2 3] ; % <----- problem
Change the span to [2,1]
x = linspace(0,30);
y = sin(x/2);
[X,Y,Z] = peaks(20);
t1 = tiledlayout(3, 1, 'TileSpacing', 'Compact') ;
t2 = tiledlayout(t1, 1, 2, 'TileSpacing', 'Compact') ;
t3 = tiledlayout(t1, 3, 1, 'TileSpacing', 'Compact') ;
t3.Layout.Tile = 2 ;
t3.Layout.TileSpan = [2 1] ;
% Plot top two figures
nexttile(t2)
contour(X, Y, Z)
nexttile(t2)
contour(X, Y, Z)
title(t2, 'Common Title')
xlabel(t2, 'Common xlabel')
ylabel(t2, 'Common ylabel')
% Plot bottom three figures
nexttile(t3)
plot(x, y)
nexttile(t3)
plot(x, y)
nexttile(t3)
plot(x, y)
title(t3, 'Common title')
xlabel(t3, 'Common xlabel')
ylabel(t3, 'Common ylabel')

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Labels and Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by