How to put a colorbar between the columns in tiledlayout?

6 views (last 30 days)
Hello,
I cannot figure out how to put a colorbar between the columns in tiledlayout. I searched for possible answers but unfortunately could not solve my problem. Please give me a link if there is an answer already.
Here is the chunk of code I use:
figSlices = figure('Position', [0, 0, 2 * 300, 3 * 250], 'Units', 'pixels', ...
'Name', ['nFreqs ' num2str(length(Geometry.freqsVec))]);
tlo = tiledlayout(3, 2, "TileSpacing", "compact", "Padding", "compact", ...
"TileIndexing", "columnmajor");
for i = 1:3
hAbs(i) = nexttile(tlo);
imagesc(0.5*ones(50,50), [0 1]);
end
set(hAbs, 'Colormap', hot, 'CLim', [0 1]);
cbAbs = colorbar(hAbs(end));
cbAbs.Layout.Tile = 'west';
for i = 1:3
hSca(i) = nexttile(tlo);
imagesc(1.2*ones(50, 50), [1 2]);
end
set(hSca, 'Colormap', hot, 'CLim', [1 2]);
cbSca = colorbar(hSca(end));
cbSca.Layout.Tile = 'east';
And I get something like looking below.
I want, however, that the top colorbar is near the left column in the image and stretched over all the 3 tiles in the first column. While the bottom colorbar must go on the right of the second column and also stretched across all rows.
Changing
cbAbs.Layout.Tile = 'east'; to cbAbs.Layout.Tile = 'west';
I get the image like this:
This is close to what I want, but the left colormap must be in between the figure, on the right side of the left column. How to do it?
Thanks!

Accepted Answer

Voss
Voss on 23 Oct 2024
One solution is to use nested tiledlayouts; in this case a 1x2 tiledlayout containing two 3x1 tiledlayouts.
figSlices = figure('Units', 'pixels', 'Position', [0, 0, 2 * 300, 3 * 250]);
outer_tl = tiledlayout(1, 2, "TileSpacing", "compact", "Padding", "compact");
N = 3;
inner_tl = tiledlayout(outer_tl,N,1,"TileSpacing", "compact", "Padding", "compact");
inner_tl.Layout.Tile = 1;
ax = gobjects(N,1);
for ii = 1:N
ax(ii) = nexttile(inner_tl);
imagesc(ax(ii), 0.5*ones(50,50), [0 1]);
end
set(ax, 'Colormap', hot, 'CLim', [0 1]);
cb = colorbar(ax(end));
cb.Layout.Tile = 'east';
N = 3;
inner_tl = tiledlayout(outer_tl,N,1,"TileSpacing", "compact", "Padding", "compact");
inner_tl.Layout.Tile = 2;
ax = gobjects(N,1);
for ii = 1:N
ax(ii) = nexttile(inner_tl);
imagesc(ax(ii), 1.2*ones(50,50), [1 2]);
end
set(ax, 'Colormap', hot, 'CLim', [1 2]);
cb = colorbar(ax(end));
cb.Layout.Tile = 'east';
  2 Comments
Konstantin
Konstantin on 24 Oct 2024
Thank you! I did not consider before the nested layouts and from now on will use them when needed.

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!