MATLAB Answers

How to remove labels and grey space between subplots so that the subplots fill-up the entire figure

259 views (last 30 days)
I have 2x2 plots. How do I to turn off the labeling, and just fill the subplot region such that the subplots fill-up the entire figure window?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 29 Aug 2019
For MATLAB releases before R2019b, the following example code creates 2x2 plots, using the 'subplot' function and modifies the axes properties such that the subplots fill-up the entire figure window:
subplot(2,2,1);
[X,Y,Z] = peaks(20);
imagesc(Z);
ax = gca;
set(ax,'XTick',[], 'YTick', []);
ax.Position = [0 0.5 0.5 0.5];
subplot(2,2,2);
contour(X,Y,Z)
ax = gca;
set(ax,'XTick',[], 'YTick', []);
ax.Position = [0.5 0.5 0.5 0.5];
subplot(2,2,3);
imagesc(Z);
ax = gca;
set(ax,'XTick',[], 'YTick', []);
ax.Position = [0 0 0.5 0.5];
subplot(2,2,4);
contour(X,Y,Z);
ax = gca;
set(ax,'XTick',[], 'YTick', []);
ax.Position = [0.5 0 0.5 0.5];
There is a slightly elegant way of doing this in R2019b using the 'tiledlayout' function instead of 'subplot':
t = tiledlayout(2,2);
[X,Y,Z] = peaks(20);
% Tile 1
nexttile
imagesc(Z)
ax = gca;
set(ax,'XTick',[], 'YTick', []);
% Tile 2
nexttile
contour(X,Y,Z)
ax = gca;
set(ax,'XTick',[], 'YTick', []);
% Tile 3
nexttile
imagesc(Z)
ax = gca;
set(ax,'XTick',[], 'YTick', []);
% Tile 4
nexttile
contour(X,Y,Z)
ax = gca;
set(ax,'XTick',[], 'YTick', []);
t.Padding = 'none';
t.TileSpacing = 'none';
Note that the last two commands get rid of all the space between the tiled plots.

More Answers (1)

Mendi
Mendi on 5 Dec 2019
Edited: Mendi on 6 Dec 2019
A cleaner version:
tlo = tiledlayout(2,2,'TileSpacing','none','Padding','none');
[X,Y,Z] = peaks(20);
nexttile, imagesc(Z) % Tile 1
nexttile, contour(X,Y,Z) % Tile 2
nexttile, imagesc(Z) % Tile 3
nexttile, contour(X,Y,Z) % Tile 4
set(tlo.Children,'XTick',[], 'YTick', []); % all in one

Products


Release

No release entered yet.

Community Treasure Hunt

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

Start Hunting!