How to generate unaligned subplots using tiledlayout?

21 ビュー (過去 30 日間)
hmhuang
hmhuang 2021 年 10 月 28 日
編集済み: Dave B 2021 年 10 月 28 日
I plan to generate a plot that has 3 subplots on the first row, 4 subplots on the second row using tiledlayout. As 4 is not a multiple of 3, I have difficulties to correctly manage it. All examples I have seen are aligned subplots (tiles) though.
Normally I will do that with something like:
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(...);
Is it possible to achieve this goal by using tiledlayout?

採用された回答

Dave B
Dave B 2021 年 10 月 28 日
編集済み: Dave B 2021 年 10 月 28 日
tiledlayout requires that your grid is fixed, but you can do this in two ways:
First approach: least common multiple with tilespan. This gives you simple looking code but you'll have to think about the math a bit.
figure(1)
tiledlayout(2,12)
nexttile([1 4])
nexttile([1 4])
nexttile([1 4])
nexttile([1 3])
nexttile([1 3])
nexttile([1 3])
Second approach: nested layouts. I like this because it's easy for me to picture a layout with multiple sub-layouts, but it means thinking about the graphics tree a bit when you add new things.
figure(2)
tcl_parent = tiledlayout(2,1);
tcl_top = tiledlayout(tcl_parent,1,3);
nexttile(tcl_top)
nexttile(tcl_top)
nexttile(tcl_top)
tcl_bot = tiledlayout(tcl_parent,1,4);
% note that while nexttile lets you specify a tile, when you parent
% a layout to another layout you have to manually specify the tile
tcl_bot.Layout.Tile = 2;
nexttile(tcl_bot)
nexttile(tcl_bot)
nexttile(tcl_bot)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by