I'm plotting 5 x 3 plots using subplot command, but there are massive margins around each subplot.
How do I control the margin size around them?
figure;
for c=1:15
subplot(5,3,c);
imagesc(开发者_开发技巧reshape(image(:,c), 360,480));
colormap gray;
axis image;
end
The problem is that Matlab assigns the position
property of each axis such that there is space around each plot. You can either adjust the position
property, or you can get subaxis from the File Exchange and set up the subplots the way you like.
Take a look at the axes's LooseInset and OuterPosition properties: http://undocumentedmatlab.com/blog/axes-looseinset-property/
Since MATLAB R2019b you can use tiledlayout function to control the spacing of the subplots.
Here's an example which shows how to obtain subplots without tile spacing:
figure
example_image = imread('cameraman.tif');
t = tiledlayout(5,3);
nexttile
for c= 1:15
imagesc(example_image(:,c))
if c < 15
nexttile
end
end
t.TileSpacing = 'None';
In addition to the other answers, you could try Chad Greene's smplot from the FileExchange. This will produce a 'small multiple' plot and automatically deal with some of the hassle of Matlab's position
property.
Example below showing default subplot
behaviour, smplot
with axis off and smplot
with axis on, respectively:
image = randn(360*480,15);
% default subplot
figure;
for c=1:15
subplot(5,3,c);
imagesc(reshape(image(:,c), 360,480));
colormap gray;
axis image;
end
% smplot axis off
figure;
for c=1:15
smplot(5,3,c);
imagesc(reshape(image(:,c), 360,480));
colormap gray;
axis off;
end
% smplot axis on
figure;
for c=1:15
smplot(5,3,c,'axis','on');
imagesc(reshape(image(:,c), 360,480));
colormap gray;
axis tight;
end
To minimize the white space surrounding each subplot, run: [1]
for c=1:15
h_ax = subplot(5,3,c);
% [...]
outerpos = get(h_ax,'OuterPosition');
ti = get(h_ax,'TightInset');
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
set(h_ax,'Position',[left bottom ax_width ax_height]);
end
This implementation automates the principle outlined in Jonas's answer.
[1] https://www.mathworks.com/help/releases/R2015b/matlab/creating_plots/save-figure-with-minimal-white-space.html
精彩评论