shared colorbar for specific plots in tiledlayout

조회 수: 12 (최근 30일)
Sobhan
Sobhan 2025년 2월 17일
댓글: Sobhan 2025년 2월 17일
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

채택된 답변

Catalytic
Catalytic 2025년 2월 17일
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
Sobhan
Sobhan 2025년 2월 17일
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

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

추가 답변 (1개)

Matt J
Matt J 2025년 2월 17일
편집: Matt J 2025년 2월 17일
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

카테고리

Help CenterFile Exchange에서 Vector Volume Data에 대해 자세히 알아보기

태그

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by