File Exchange

image thumbnail

Get rid of the white margin in saveas output

version 1.6 (1.81 KB) by

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

24 Downloads

Updated

View License

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

Comments and Ratings (22)

Luchen Li

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

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.

craq

craq (view profile)

sure, I'd be glad to.

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

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

Mat Labber

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.

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

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.

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.

E Akbas

E Akbas (view profile)

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

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

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.

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'

E Akbas

E Akbas (view profile)

It works correctly for this example:

imagesc(rand(50,50)); colorbar

E Akbas

E Akbas (view profile)

Can you provide a simple example to reproduce the problem?

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?

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.

E Akbas

E Akbas (view profile)

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

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

Updates

1.6

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

1.5

Outside legends were not handled correctly. Fixed.

1.4

Abbreviated author name in the license file.

1.3

Edited author's name in the m-file.

1.2

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

MATLAB Release
MATLAB 7.11 (R2010b)
Acknowledgements

Inspired: tightfig

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

» Watch video