Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using colorbar and nested tiledlayout

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:

  • Changing the parent to of the colorbar to the outer tiledlayout (tl) is not allowed at the time of instantiation i.e. using cb = colorbar('Parent', tl); results in an error.
  • Changing the parent of the colorbar to 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.
  • Changing the parent of both the axes and the colorbar changes the Layout of the axes (i.e. adding set(gca, 'Parent', tl); set(cb, 'Parent', tl); at the end).
like image 501
magnesium Avatar asked Jun 14 '26 18:06

magnesium


1 Answers

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:

enter image description here

like image 86
X Zhang Avatar answered Jun 16 '26 14:06

X Zhang