如何使用 tiledlayout 生成未对齐的子图?
How to generate unaligned subplots using tiledlayout?
我计划使用 tiledlayout
生成一个图,第一行有 3 个子图,第二行有 4 个子图。由于 4 不是 3 的倍数,我很难正确处理它。不过,我看到的所有示例都是对齐的子图(图块)。
通常我会这样做:
subplot(2,3,1); plot(...);
subplot(2,3,2); plot(...);
subplot(2,3,3); plot(...);
subplot(2,4,5); plot(...);
subplot(2,4,6); plot(...);
subplot(2,4,7); plot(...);
是否可以通过使用tiledlayout
来实现这个目标?
方法一:继续使用subplot()
如果您想继续使用 subplot()
函数,您可以在本例中使用 3 和 4 的最低公倍数 (LCM)。在这种情况下,使用具有 2 行和 12 列的子图网格就足够了。这种方法的关键是让子图跨越多个位置。 subplot()
函数中的第三个参数将指示每个子图从头到尾跨越的位置。我发现次要情节和跨度概念适用于多种语言,这也是我倾向于在 tiledlayout()
.
上使用它的原因
clf;
subplot(2,12,1:4); plot(rand(5,1));
subplot(2,12,5:8); plot(rand(5,1));
subplot(2,12,9:12); plot(rand(5,1));
subplot(2,12,13:15); plot(rand(5,1));
subplot(2,12,16:18); plot(rand(5,1));
subplot(2,12,19:21); plot(rand(5,1));
subplot(2,12,22:24); plot(rand(5,1));
clf;
subplot(2,12,2:4); plot(rand(5,1));
subplot(2,12,5:7); plot(rand(5,1));
subplot(2,12,8:10); plot(rand(5,1));
subplot(2,12,13:15); plot(rand(5,1));
subplot(2,12,16:18); plot(rand(5,1));
subplot(2,12,19:21); plot(rand(5,1));
subplot(2,12,22:24); plot(rand(5,1));
方法二:使用tiledlayout()
Grid_Height = 2; Grid_Width = 12;
tiledlayout(Grid_Height,Grid_Width);
Position = 1; Height = 1; Width = 4;
nexttile(Position,[Height,Width]);
plot(rand(5,1));
Position = 5; Height = 1; Width = 4;
nexttile(Position,[Height,Width]);
plot(rand(5,1));
Position = 9; Height = 1; Width = 4;
nexttile(Position,[Height,Width]);
plot(rand(5,1));
Position = 13; Height = 1; Width = 3;
nexttile(Position,[Height,Width]);
plot(rand(5,1));
Position = 16; Height = 1; Width = 3;
nexttile(Position,[Height,Width]);
plot(rand(5,1));
Position = 19; Height = 1; Width = 3;
nexttile(Position,[Height,Width]);
plot(rand(5,1));
Position = 22; Height = 1; Width = 3;
nexttile(Position,[Height,Width]);
plot(rand(5,1));
我计划使用 tiledlayout
生成一个图,第一行有 3 个子图,第二行有 4 个子图。由于 4 不是 3 的倍数,我很难正确处理它。不过,我看到的所有示例都是对齐的子图(图块)。
通常我会这样做:
subplot(2,3,1); plot(...);
subplot(2,3,2); plot(...);
subplot(2,3,3); plot(...);
subplot(2,4,5); plot(...);
subplot(2,4,6); plot(...);
subplot(2,4,7); plot(...);
是否可以通过使用tiledlayout
来实现这个目标?
方法一:继续使用subplot()
如果您想继续使用 subplot()
函数,您可以在本例中使用 3 和 4 的最低公倍数 (LCM)。在这种情况下,使用具有 2 行和 12 列的子图网格就足够了。这种方法的关键是让子图跨越多个位置。 subplot()
函数中的第三个参数将指示每个子图从头到尾跨越的位置。我发现次要情节和跨度概念适用于多种语言,这也是我倾向于在 tiledlayout()
.
clf;
subplot(2,12,1:4); plot(rand(5,1));
subplot(2,12,5:8); plot(rand(5,1));
subplot(2,12,9:12); plot(rand(5,1));
subplot(2,12,13:15); plot(rand(5,1));
subplot(2,12,16:18); plot(rand(5,1));
subplot(2,12,19:21); plot(rand(5,1));
subplot(2,12,22:24); plot(rand(5,1));
clf;
subplot(2,12,2:4); plot(rand(5,1));
subplot(2,12,5:7); plot(rand(5,1));
subplot(2,12,8:10); plot(rand(5,1));
subplot(2,12,13:15); plot(rand(5,1));
subplot(2,12,16:18); plot(rand(5,1));
subplot(2,12,19:21); plot(rand(5,1));
subplot(2,12,22:24); plot(rand(5,1));
方法二:使用tiledlayout()
Grid_Height = 2; Grid_Width = 12;
tiledlayout(Grid_Height,Grid_Width);
Position = 1; Height = 1; Width = 4;
nexttile(Position,[Height,Width]);
plot(rand(5,1));
Position = 5; Height = 1; Width = 4;
nexttile(Position,[Height,Width]);
plot(rand(5,1));
Position = 9; Height = 1; Width = 4;
nexttile(Position,[Height,Width]);
plot(rand(5,1));
Position = 13; Height = 1; Width = 3;
nexttile(Position,[Height,Width]);
plot(rand(5,1));
Position = 16; Height = 1; Width = 3;
nexttile(Position,[Height,Width]);
plot(rand(5,1));
Position = 19; Height = 1; Width = 3;
nexttile(Position,[Height,Width]);
plot(rand(5,1));
Position = 22; Height = 1; Width = 3;
nexttile(Position,[Height,Width]);
plot(rand(5,1));