Code covered by the BSD License  

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Get rid of the white margin in saveas output

4.3
4.3 | 10 ratings Rate this file 32 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

by

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
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 (22)
26 Mar 2016 Luchen Li  
17 Mar 2016 Mohamed LAKRIT

Hi,

when i use this script to save plotyy figures.
It cut them from one side. Is there an option that i should do to resolve this problem.

tx

Comment only
14 Mar 2016 E Akbas

E Akbas (view profile)

OK, great! I can setup a github repository and put the file there, ans then you can fork it from there. Then, I will put the github link in the description above to let visitors of this page know about the new github repo.

Or, if you have any other suggestion,let me know.

Comment only
14 Mar 2016 craq

craq (view profile)

sure, I'd be glad to.

Comment only
11 Mar 2016 E Akbas

E Akbas (view profile)

Hi craq,

Thanks for the feedback.

I haven't been using MATLAB for a while and it seems that I'm not going back to MATLAB. I am very happy with Python, numpy and scipy.

Would you like to continue maintaining this file? Let me know.
--
Emre

Comment only
10 Mar 2016 craq

craq (view profile)

one of the best scripts out there for saving figures. I've tried a fair few (exportfig, export_fig, I even wrote my own). It does however seem to have a problem with being a bit too aggressive in the cropping of axes titles and legends. Here's my suggestion to fix that (starting at line 39):

htext = findall(hfig, 'type','Text');
hlegend = findall(hfig, 'type','Legend');

hall = [htext; hlegend];
for ii=1:length(hall)
my_parent = get(hall(ii),'parent');
if strcmpi(get(my_parent,'type'),'Axes')
%if this object has an Axes as a parent, it's position will be relative
%to the axes' position and limits
p_child = get(hall(ii), 'position');
p_ax = get(my_parent, 'position');
my_xlim = get(my_parent,'xlim');
my_ylim = get(my_parent,'ylim');

p(1) = p_ax(1) + (p_child(1) - my_xlim(1)) / (my_xlim(2) - my_xlim(1)) * p_ax(3);
p(2) = p_ax(2) + (p_child(2) - my_ylim(1)) / (my_ylim(2) - my_ylim(1)) * p_ax(4);

if length(p_child) == 4
p(3) = p(1) + (p_child(3) - my_xlim(1)) / (my_xlim(2) - my_xlim(1)) * p_ax(3);
p(4) = p(2) + (p_child(4) - my_ylim(1)) / (my_ylim(2) - my_ylim(1)) * p_ax(4);
else
%some objects only have 3 values for position: x,y,z
p(3) = max([p(1) p_ax(1) + p_ax(3)]);
p(4) = max([p(2) p_ax(2) + p_ax(4)]);
end
else
p = get(hall(ii),'position');
p(3) = p(1) + p(3);
p(4) = p(2) + p(4);
end

tighest_box(1) = min(tighest_box(1), p(1));
tighest_box(2) = min(tighest_box(2), p(2));
tighest_box(3) = max(tighest_box(3), p(3));
tighest_box(4) = max(tighest_box(4), p(4));
end

%% move all axes to left-bottom
%...

16 Nov 2015 Mat Labber  
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])
colorbar
xlabel('X'),ylabel('Y'),title('Title')

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;
else
hfig = h;
end

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:

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 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