Code covered by the BSD License  

Highlights from
Get rid of the white margin in saveas output

4.1 | 7 ratings Rate this file 107 Downloads (last 30 days) File Size: 1.81 KB File ID: #34024 Version: 1.6
image thumbnail

Get rid of the white margin in saveas output


E Akbas (view profile)


01 Dec 2011 (Updated )

MATLAB's saveas pads your figure with a white margin. This m-file is prevents that.

| Watch this File

File Information

This m-file is an alternative to built-in saveas command. It saves your figure without the white box/margin around it.


This file inspired Tightfig.

MATLAB release MATLAB 7.11 (R2010b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (15)
22 May 2015 Daniel

Daniel (view profile)

Hi, I found two problems with this function:

#1 : Is doesn't works if the figure is a rectangle.
#2: The colorbar still get forced to be inside the figure.

I tried to use your function with this figure

figure,[C,h] = contourf(peaks(20),10);
axis equal, axis([1 12 1 6])

Thanks a lot.

Comment only
07 Feb 2015 Israel González

Thanks I'm using your script to save my figures :)

09 Jul 2014 E Akbas

E Akbas (view profile)

Andy, it's probably because you are re-using the same figure window for multiple saves, are you? I would suggest to create a new figure for each of your plots. Your code will run slower but the problem will probably be fixed. Thanks.

Comment only
09 Jul 2014 Andy Xiong

This is very helpful.

!But when I run the script as batch job. The generated graph is too tight, some caption is invisible.

27 Jun 2014 E Akbas

E Akbas (view profile)

Nishant, you are right, I updated the file. Thanks!

Comment only
27 Jun 2014 Nishant

I think You should chnge lines 13-18 to this

if nargin==1
hfig = gcf;
outfilename = h;
hfig = h;

or else passing a handle will throw an error

27 May 2014 E Akbas

E Akbas (view profile)

Thanks for the heads up Susanna. It's fixed now. It seems that legend position is automatically changed when the main axes position is changed. So, I fixed the script accordingly.

Comment only
27 May 2014 Susana Ochoa Rodriguez

Very useful function - it reduces the white margins around printed/saved figures, but it has problems with the legend.

In my case, the legend was originally located 'southoutside' with horizontal orientation, but the saveTightFigure function pushed it up so that it ended up overlapping with the x-axis.

I'm not sure what is causing this. Would be great if it could be fixed.

Following Geoff's suggestion, I managed to fix it (for my own figure), by changing the second 'for i=1:length(hax)' to for i=length(hax):-1:2'

08 May 2014 E Akbas

E Akbas (view profile)

It works correctly for this example:

imagesc(rand(50,50)); colorbar

Comment only
08 May 2014 E Akbas

E Akbas (view profile)

Can you provide a simple example to reproduce the problem?

Comment only
08 May 2014 Geoff Stanley

Very useful function, thanks! Unfortunately, even with the all-axes update, I've been finding that my colorbar (standard, vertical on the right) is being pushed too far left such that it's on top of the figure. Not sure why, but changing the second 'for i=1:length(hax)' to 'for i=length(hax):-1:1' seems to fix this, at least for the specific figure I'm working with. Any idea why?

26 Mar 2014 E Akbas

E Akbas (view profile)

Ubaldo, if the xlabel and title are overlapping in the actual figure displayed, then it will be reflected in the output. So, you have to make sure your figure is big enough for prevent those overlaps.

Comment only
26 Mar 2014 E Akbas

E Akbas (view profile)

Ubaldo, I updated the file to handle subplots. Please check if it works for you.

Comment only
26 Mar 2014 Ubaldo Tiberi

I have found an update that also handle suplots here:

However, when you use subplots the x-axis names overlap with the title of the next subplot. Is there any further update planned of this very nice and useful function??

07 May 2013 Caspar Jespersen  
26 Mar 2014 1.2

Updated the code to handle multiple axes within a figure, like subplots, colorbars.

26 Mar 2014 1.3

Edited author's name in the m-file.

10 Apr 2014 1.4

Abbreviated author name in the license file.

28 May 2014 1.5

Outside legends were not handled correctly. Fixed.

27 Jun 2014 1.6

Bugfix: an error was being produced when figure handle was given as input.

Contact us