I'm trying to generate some nested tiledlayouts and have a colorbar associated with all of them.
There is a way to put a colorbar on the outside of a single tiledlayout (here):

However, this fails for nested tiledlayouts.
Here is an example:
figure;
tl = tiledlayout(4, 1);
for ii = 1:4
tl2 = tiledlayout(tl, 1, ii);
tl2.Layout.Tile = ii;
for jj = 1:ii
nexttile(tl2,jj);
imagesc; axis tight;
end
end
cb = colorbar();
cb.Layout.Tile = 'east';
(This is of course a minimal example. In the example I could nest the tiledlayouts in columns instead of rows - but this is not suitable for my actual code.)
What I would like is for the colorbar to appear on the side of all the axes. Similar to the example presented above, but for the nested tiledlayouts. How would this be done?
Unsuccessful attempts so far:
tl) is not allowed at the time of instantiation i.e. using cb = colorbar('Parent', tl); results in an error.tl is also not allowed after it is generated (set(cb,'Parent',tl);) as the colorbar has to have the same parent as its axes.set(gca, 'Parent', tl); set(cb, 'Parent', tl); at the end).If nested tiles are really necessary, you can do something like:
figure;
t1=tiledlayout(1, 2);
t2=tiledlayout(t1,4,4);
for ii = 1:4
nexttile(t2,(ii-1)*4+ii,[1,5-ii]);
imagesc;
end
ax=nexttile(t1,2);
cbh=colorbar(ax);
ax.Visible="off";
cbh.Position(1)=ax.Position(1);
cbh.AxisLocation='in';
You can also start with a gridded tile layout and exploit the nexttile() function with the span option.
figure;
tiledlayout(4, 5);
for ii = 1:4
nexttile((ii-1)*5+ii,[1,5-ii]);
imagesc;
end
ax=nexttile(5,[4,1]);
cbh=colorbar(ax);
ax.Visible="off";
cbh.Position(1)=ax.Position(1);
cbh.AxisLocation='in';
The colorbar will need some manual adjusts in both cases, the results are similar:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With