# How do I decrease the margins around the subplots in my figure in MATLAB?

1,693 次查看（过去 30 天）
MathWorks Support Team 2009-6-27
I would like to decrease the white space around my plots because when I copy the figure to a Word document, the margins around the axes reduces the size of the plot and I often have to crop this space out in order to increase the plot's size.

### 采纳的回答

From R2019b, you can minimize the space around the plots by using tiledlayout function.
For details, please see the following documentation.
- Saving and Copying Plots with Minimal White Space
If you use R2019a and earlier releases, please refer the following information:
The ability to set margins for subplots in a figure is not currently available in MATLAB. However, you may refer to the user-contributed functions "subaxis" or "subtightplot" at MATLAB Central that provides this functionality:
MATLAB Central is a common location for MATLAB users provided by MathWorks where they can share their MATLAB code and ideas.
Note that MathWorks does not guarantee or warrant the use or content of these submissions. Any questions, issues, or complaints should be directed to the contributing author.
##### 2 个评论显示隐藏 1更早的评论
Eric Sargent 2020-12-9
Starting in R2019b, you can create a tiledlayout and set padding and spacing properties. This example creates a layout with no padding around the perimeter and minimal spacing between plots.
for i=1:4
nexttile
plot(rand(1,10));
end
Also, starting in R2020a, you can use the copygraphics to copy a tightly cropped version of your graphics and then paste it in Word.
copygraphics(gcf)

### 更多回答（5 个）

Jesús Lucio 2015-2-12
Hi,
I know the question is very old, but it's still very interesting.
A simple way of doing what you want (change margins of subplots) is this:
pos = get(gca, 'Position');
pos(1) = 0.055;
pos(3) = 0.9;
set(gca, 'Position', pos)
to write just after you create each (sub)plot.
This way each plot is resized. 'Position' is the axis property (a 1 x 4 vector) with these fields:
[x y width height]
Obviously, you can change any of the four values to your best choice.
Jesus.
##### 2 个评论显示隐藏 1更早的评论
RP 2019-11-16

Thank you very much Jesus. It worked awesome to solve my Problem. But, I have some queries on your code that
1. what pos(1) and pos(3) indicates?
2. What x and y indicates?
3. How can I change the values of x, y, height, and width as per my requirement?
4. Is this code works for any order of grid other than 1*4 grid as suggested by you. Let suppose, I have 5*6 order of grid. So my subplot will be subplot(5,6,i). In this case, how can I adjust the height, width of the subplots?
Thanks.

Konstantinos 2015-11-20

Since there is no standard for what margin size is the best this is what I would do
1. Once I create the plot I would enable editing from tools menu.
2. Resize plots to what I think is best configuration.
3. File>Generate Code to have an automated solution
##### 2 个评论显示隐藏 1更早的评论
Francisco Alderete 2022-9-16
This was the most helpful and easy solution

Robby van Delden 2015-11-23
Although ugly, for me it seems to help to multiply the distribution of the subplots for instance by a factor 2:
ax1 = subplot(1,8,[1,2]); .. ax2 = subplot(1,8,[3,4,5,6]); .. %probably not related: ylim([ymin ymax]); .. ax3 = subplot(1,8,[7,8]); linkaxes([ax2,ax1,ax3],'y');
instead of: ax1 = subplot(1,4,1) ax2 = subplot(1,4,[2,3]) ax3 = subplot(1,4,4)
##### 0 个评论显示隐藏 -1更早的评论

Miquel 2020-1-17
You can use the following code:
ax = gca;
outerpos = ax.OuterPosition;
ti = 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);
ax.Position = [left bottom ax_width ax_height];
I found it here: https://uk.mathworks.com/help/matlab/creating_plots/save-figure-with-minimal-white-space.html. I think it should only work for MATLAB R2019b or later versions, but it works for me with version R2018b.
##### 1 个评论显示隐藏 无
Geoffrey BAYARD 2021-3-25
Thanks a lot Miquel. Your code is running on R2018A too.

KAE 2021-8-20
You could try tiledlayout.

### 类别

Find more on Graphics Object Programming 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!

Translated by