Code covered by the BSD License  

Highlights from
tightfig

4.76923

4.8 | 13 ratings Rate this file 769 Downloads (last 30 days) File Size: 2.41 KB File ID: #34055
image thumbnail

tightfig

by

 

02 Dec 2011 (Updated )

Remove excess margins from figures.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

Alters a figure so that it has the minimum size necessary to
enclose all axes in the figure without excess space around them.

Works for figures containing multiple axes as subplots.

Note that tightfig will expand the figure to completely encompass all
axes if necessary.

Returns an error if the figure contains any 3D axes which have been zoomed.

Acknowledgements

Get Rid Of The White Margin In Saveas Output inspired this file.

This file inspired Subtightplot and Spreadfigures.

Required Products MATLAB
MATLAB release MATLAB 7.6 (R2008a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (23)
18 Dec 2014 Shuqin

very helpful! Thanks for sharing.

04 Dec 2014 pietro

It works fine but not when the figures are docked because it modifies the figure properties and not the axis ones. Anyway it is a great function. Hopefully it will work also when the figure are docked.

05 Nov 2014 Thomas Edwards

I had a colorbar addded to my figure of multiple subplots. The annoying thing was that the exponent of the colorbar was cut-off on the edge of the figure. Using tightfig fixed this for me so that the exponent would fit in the figure. Very useful!

01 Oct 2014 João

Great. Although it sometimes clips the edges off fonts that are really close to the limits (I was not able to understand in which conditions it does this, but it happened unfrequently)

25 Sep 2014 Daniel  
04 Sep 2014 arnold

it doesn't take into account an xlabel of a boxplot. Just cuts it away. Ylabel seems fine though.

28 Aug 2014 Chad Greene

I use this frequently; thanks for sharing. I recommend adding this to the end of the function to suppress unwanted outputs:

if nargout==0; clear hfig; end

19 Aug 2014 arnold

I'll see when I find the time to look into it.
Something else I just noticed: Using plotyy and setting both y-labels, title, xlabel etc and tightfig. to wrap things up. Tightfig seems to fast somehow. It messes up the figure window. It works if I include a pause(0.2) before tightfig. Strange..... but the workaround is no problem of course.

15 Aug 2014 Richard Crozier

@arnold, I'll implement this if you suggest how to identify them. Unfortunately I don't have time to do a lot of research on this right now.

11 Aug 2014 arnold

too bad it doesn't recognize my x-labels when using hierarchicalboxplot. It would be most useful if it were looking for all elements in a figure, not just axes.

09 Jul 2014 Pablo Deossa

very useful!

08 Jul 2014 Suntaree

awesome

27 Mar 2014 Gergely Takács  
24 Jan 2014 grega  
02 Oct 2013 Mike Shen  
07 Jan 2013 K E

Perfect. Very useful if you are using the Windows clipboard to copy figures into Word and need to trim off the excess whitespace beforehand.

22 Dec 2012 Richard Crozier

Hi Jurgan, from the description for crop.m I gather it crops whitespace from a bitmap (i.e. non vector type) image file which is already saved to disk, e.g. a jpeg, bitmap, png etc. It does not appear to act on figures.

I suppose you could get similar functionality to tightfig by first saving your figure to disk in one of these formats, then running crop.m on it. I prefer the tightfig workflow personally.

20 Dec 2012 Jurgen

I see, I have export_fig. Based on what you said then maybe CROP (http://www.mathworks.nl/matlabcentral/fileexchange/20427-crop-whitespace-from-an-image) is worth comparing :)

19 Dec 2012 Richard Crozier

Jurgen, most of the functions you mention adjust the space between and around individual axes in subplots, and at least some have to be called on creation of a subplot. tightfig crops the white space around the outside of all the axes (and legends) in any figure without adjusting the rest of the plots at all. tightfig also identifies if you have a 3D zoomed axes in your figure and ignores it, since such figures cannot be cropped. tightfig is also well commented so you can understand it's workings.

Actually you failed to identify the one other function on the file exchange I know which does also have this functionality, export_fig.m.

18 Dec 2012 Jurgen

How does this compare to: spaceplots, subplot1, tight_subplot, subplot_tight and subplotplus?

26 Nov 2012 Ben

Hi, I found this very useful. However, I have one suggestion: in the line
ti(ti < 0) = 0.1

You should consider
ti(ti < 0.1) = 0.1
instead.

I had a graph where the top line of the outer box was being cutoff by saveas after I used tightfig, presumably because it had some ti in (0,0.1).

19 Jan 2012 Richard Crozier

Thanks Thomas, I've submitted a new version with the fix.

16 Jan 2012 Thomas

Great work... i was just going to implement it myself... but here it is ;) Thank you.

Just a minor issue:
Restoring the original axes units fails when there is only a single axes object in the figure:

set(hax(i), 'Units', origaxunits{i});

... since origaxunits is no cell array then. A simple if else does the trick.

Updates
19 Jan 2012

Corrected bug noted by commenter Thomas, also tidied up some other parts of code and comments.

17 Dec 2012

Added suggested improvement for minimum bound by Ben

Contact us