shared colorbar for specific plots in tiledlayout
Show older comments
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
Accepted Answer
More Answers (1)
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
Categories
Find more on Vector Volume Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


