shared colorbar for specific plots in tiledlayout
50 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Catalytic
2025 年 2 月 17 日 2:11
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 件)
Matt J
2025 年 2 月 17 日 1:16
編集済み: Matt J
2025 年 2 月 17 日 1:30
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Blue についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!