How to generate unaligned subplots using tiledlayout?

5 views (last 30 days)
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?

Accepted Answer

Dave B
Dave B on 28 Oct 2021
Edited: Dave B on 28 Oct 2021
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)

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!