shared colorbar for specific plots in tiledlayout

10 visualizzazioni (ultimi 30 giorni)
I am trying to give the first two plots one shared colorbar and the third one its own but the colorbar for the first two plots is added on the outside of all three plots. I need this to work for all cases of Boolean1 and Boolean2.
Z1 = peaks; Z2 = membrane;
Boolean1 = true; Boolean2 = true;
t = tiledlayout('vertical');
t.Padding = 'compact';
t.TileSpacing = 'compact';
ax1 = nexttile;
contourf(Z1);
colormap(ax1, flipud(gray(256)));
title('Plot 1');
if Boolean1
ax2 = nexttile;
contourf(Z2);
colormap(ax2, flipud(gray(256)));
title('Plot 2');
end
cb1 = colorbar;
cb1.Layout.Tile = 'east';
if Boolean2
ax3 = nexttile;
contourf(Z2);
colormap(ax3, 'jet');
title('Plot 3');
cb2 = colorbar; cb2.Location = 'eastoutside';
end

Risposta accettata

Catalytic
Catalytic il 17 Feb 2025
main(1,1)
function main(Boolean1,Boolean2)
B=[Boolean1,Boolean2];
Z1 = peaks; Z2 = membrane;
if B==[0,0] %#ok<*BDSCA,*BDSCI>
Plot1(gca);colorbar
elseif B==[1,0]
tl();
Plot1(nexttile); Plot2(nexttile);
cb = colorbar; cb.Layout.Tile = 'east';
elseif B==[0,1]
tl();
Plot1(nexttile); colorbar
Plot3(nexttile); colorbar
elseif B==[1,1]
OUT=tl(); IN=tl(OUT); IN.Layout.TileSpan=[2,1];
Plot1(nexttile(IN)); Plot2(nexttile(IN));
cb = colorbar; cb.Layout.Tile = 'east';
Plot3(nexttile(OUT)); colorbar
end
function t=tl(varargin)
t = tiledlayout(varargin{:},'vertical');
t.Padding = 'compact';
t.TileSpacing = 'compact';
end
function Plot1(ax)
axes(ax)
contourf(Z1);
colormap(ax,flipud(gray(256)));
title('Plot 1');
end
function Plot2(ax)
axes(ax)
contourf(Z2);
colormap(ax,flipud(gray(256)));
title('Plot 2');
end
function Plot3(ax)
axes(ax)
contourf(Z2);
colormap(ax,'jet');
title('Plot 3');
end
end
  1 Commento
Sobhan
Sobhan il 17 Feb 2025
Thanks, this worked for me. I condensed it a bit to exclude some elseif statements.
Z1 = peaks; Z2 = membrane;
Boolean1 = true; Boolean2 = true
Boolean2 = logical
1
t = tiledlayout('vertical', 'Padding', 'compact', 'TileSpacing', 'compact');
s = tiledlayout(t, 'vertical', 'Padding', 'compact', 'TileSpacing', 'compact');
ax1 = nexttile(s);
contourf(Z1);
colormap(ax1,flipud(gray(256)));
title('Plot 1');
if Boolean1
s.Layout.TileSpan = [2,1];
ax2 = nexttile(s);
contourf(Z2);
colormap(ax2,flipud(gray(256)));
title('Plot 2');
end
cb = colorbar;
cb.Layout.Tile = 'east';
if Boolean2
ax3 = nexttile(t);
contourf(Z2);
colormap(ax3, 'jet');
title('Plot 3');
colorbar;
end

Accedi per commentare.

Più risposte (1)

Matt J
Matt J il 17 Feb 2025
Modificato: Matt J il 17 Feb 2025
Using nestedLayouts from the File Exchange,
Z1 = peaks; Z2 = membrane;
Boolean1 = true; Boolean2 = true;
if Boolean2
[ax,t,T]=nestedLayouts([2,1],[2,1]);
else
[ax,t,T]=nestedLayouts([1,1],[2,1]);
end
[t.Padding] = deal('compact');
[t.TileSpacing] = deal('compact');
axes(ax(1))
contourf(Z1);
colormap(ax(1), flipud(gray(256)));
title('Plot 1');
cb1 = colorbar;
cb1.Layout.Tile = 'east';
%%Additional plots, conditional on Booleans
if Boolean1
axes(ax(2));
contourf(Z2);
colormap(ax(2), flipud(gray(256)));
title('Plot 2');
else
ax(1).Layout.TileSpan=[2,1]; delete(ax(2));
end
if Boolean2
ax(3).Layout.TileSpan=[2,1]; delete(ax(4));
axes(ax(3))
contourf(Z2);
colormap(ax(3), 'jet');
title('Plot 3');
cb2 = colorbar; cb2.Location = 'eastoutside';
end

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by