【问题标题】:How to generate unaligned subplots using tiledlayout?如何使用 tiledlayout 生成未对齐的子图?
【发布时间】:2021-12-13 18:45:25
【问题描述】:

我计划使用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来实现这个目标?

【问题讨论】:

    标签: matlab subplot


    【解决方案1】:

    方法一:继续使用subplot()

    如果您希望继续使用subplot() 函数,您可以在这种情况下使用 3 和 4 的最低公倍数 (LCM)。在这种情况下,使用具有 2 行和 12 列的子图网格就足够了。这种方法的关键是让子图跨越多个位置subplot() 函数中的第三个参数将指示每个子图从头到尾跨越的位置。我发现 subplot 和 span 概念适用于许多语言,这就是我倾向于在 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));
    

    【讨论】:

    • 不错的解决方案!不知道用 subplot 可以做到这一点。
    猜你喜欢
    • 2021-05-19
    • 2014-12-10
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 2016-02-26
    相关资源
    最近更新 更多