How to put a colorbar between the columns in tiledlayout?

조회 수: 7 (최근 30일)
Konstantin
Konstantin 2024년 10월 23일
댓글: Voss 2024년 10월 24일
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!

채택된 답변

Voss
Voss 2024년 10월 23일
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
Konstantin
Konstantin 2024년 10월 24일
Thank you! I did not consider before the nested layouts and from now on will use them when needed.
Voss
Voss 2024년 10월 24일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Orange에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by