Code covered by the BSD License  

Highlights from
Get rid of the white margin in saveas output

4.0

4.0 | 6 ratings Rate this file 91 Downloads (last 30 days) File Size: 1.81 KB File ID: #34024
image thumbnail

Get rid of the white margin in saveas output

by

 

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
Description

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

Acknowledgements

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 (13)
09 Jul 2014 E Akbas

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.

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

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

27 Jun 2014 Nishant

I think You should chnge lines 13-18 to this

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

or else passing a handle will throw an error

27 May 2014 E Akbas

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.

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

It works correctly for this example:

imagesc(rand(50,50)); colorbar

08 May 2014 E Akbas

Can you provide a simple example to reproduce the problem?

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

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.

26 Mar 2014 E Akbas

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

26 Mar 2014 Ubaldo Tiberi

I have found an update that also handle suplots here:

http://tipstrickshowtos.blogspot.se/2010/08/how-to-get-rid-of-white-margin-in.html

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  
Updates
26 Mar 2014

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

26 Mar 2014

Edited author's name in the m-file.

10 Apr 2014

Abbreviated author name in the license file.

28 May 2014

Outside legends were not handled correctly. Fixed.

27 Jun 2014

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

Contact us