shared colorbar for specific plots in tiledlayout
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sobhan
am 17 Feb. 2025
Kommentiert: Sobhan
am 17 Feb. 2025
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
0 Kommentare
Akzeptierte Antwort
Catalytic
am 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 Kommentar
Weitere Antworten (1)
Matt J
am 17 Feb. 2025
Bearbeitet: Matt J
am 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
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



