Tobin Fricke's Lab Notebook (nibot_lab) wrote,

Some Matlab tricks for making figures

When generating figures for publication from within Matlab, one annoyance (of many!) is the unnecessary whitespace that Matlab introduces. One would prefer Matlab to make the bounding box as tight as possible and let LaTeX handle the whitespace.

It turns out that an "axes" in Matlab has three properties which determine its positioning: OuterPosition, Position, and TightInset. OuterPosition gives the coordinates of the entire region within the current figure belonging to the axes. Position gives the region contained by the "data" region of the axes. TightInset gives the distance beyond this "data" region which is used by tick marks, labels, titles, and other decorations. Here's a sample plot with the OuterPosition illustrated in yellow, the Position in magenta, and the TightInset in red:

[Figure 1]

Notice the space between the red rectangle and the boundary of the image. Why is that there? We can eliminate it by setting Position to be equal to OuterPosition minus the margins given by TightInset:

set(gca, 'Position', get(gca, 'OuterPosition') - ...
    get(gca, 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 1 0; 0 0 0 1]);

That little matrix is there because OuterPosition and Position are stored as [x_0 y_0 width height] whereas TightInset is [dx_left dy_bottom dx_right dy_top]. The results are:

Voilà! The offending whitespace has been removed. (For some reason, however, Matlab, seeing your modification of Position, takes it upon itself to compute a new, nonsensical value of OutsidePosition. This seems to be harmless...)

Another annoyance is in getting your figure to be the right size. Sure, you can scale your figure, but then the fonts will be the wrong size. The easiest thing to do is to estimate the desired size of your final figure, and have Matlab generate the figure in the correct size to begin with. The trick is to force not just the PaperSize but also the PaperPosition. If we want a figure 6.25 inches by 7.5 inches, we would do this:

set(gcf, 'PaperUnits', 'inches');
set(gcf, 'PaperSize', [6.25 7.5]);
set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperPosition', [0 0 6.25 7.5]);

A yet further annoying situation is this: you dutifully utilize PDF or EPS files for your figures, and yet they appear to be included as ugly, non-scalable raster graphics! I encountered this problem just today. Matlab allows you to choose between several renderers, which determine just how your figures get made. Suppose you have utilized transparency in your figure, yet are asking Matlab to output a figure to PDF, which (in the version supported by Matlab) does not support transparency. What's Matlab to do? Here's what it does do: it takes a screenshot, producing a bitmap, which is then wrapped in EPS or PDF. Doh!

The trick is to ask Matlab to use the "painters" renderer. It does not support transparency, but at least you're guaranteed vector graphics output:

set(gcf, 'renderer', 'painters');

Finally, just for the sake of completeness, you might want to actually generate your output file:

print(gcf, '-dpdf', 'my-figure.pdf');
print(gcf, '-dpng', 'my-figure.png');
print(gcf, '-depsc2', 'my-figure.eps');
Tags: matlab
  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded  



September 18 2007, 03:47:15 UTC 6 years ago

It would be nice if LaTeX could tell Matlab the available space for a figure, and have an appropriately sized/proportioned figure generated on the fly... but one can see that going down that road would give us OLE.


September 18 2007, 12:31:47 UTC 6 years ago

It seems this scheme is not foolproof. Matlab seems to misestimate the space required by labels/titles/etc.

Additionally, the "TightInset" size, expressed in "Normalized Coordinates" will change as you scale the image, since the fraction of the image taken up by labels will decrease as you increase the size of the image. Not sure of a good way around this.


September 18 2007, 17:45:21 UTC 6 years ago

/me prefers gnuplot + xmgrace for datasets, and final trickery in GIMP.


September 18 2007, 20:45:05 UTC 6 years ago

If you're doing final trickery in GIMP... that means you're using bitmapped images, and lack vector graphics goodness!


September 18 2007, 20:52:03 UTC 6 years ago

yeah, you got me :) But it's so easy!

I'm still looking for alternatives, but xmgrace really does work well. It even does DFT/FFT, histograms, running averages, etc. on loaded data sets. Really handy. GNUplot just makes things really pretty.

Have you used anything outside of matlab for graphics/plotting? any recommendations?


September 19 2007, 00:19:51 UTC 6 years ago

I pretty much just use Matlab, though I'd like to try using Python with MatPlotLib (in part as an open source alternative).

The plot that inspired this post is Figure 4 from this document:


September 19 2007, 00:41:02 UTC 6 years ago

Python with gnuplot extensions are also pretty killer. I've heard good things about the matplotlib. Have you been python coding much?


September 19 2007, 01:24:54 UTC 6 years ago

I don't use Python at all. I think it's a reasonably decent language, though, despite having some annoyances.


May 21 2008, 16:58:23 UTC 5 years ago

transparent axes/figure

Hi, very useful tips, thanks.

I've also found that I want to insert Matlab figures into presentations, in particular Keynote. I don't want to the background painted white/gray or whatever color.

If you set the axis and figure background colors to 'none', then they become transparent:

set(gcf, 'Color', 'none');

(obviously you can be selective about this.)

PDF export does honor the 'none' color, and the axes will not be filled. ("print -dpdf")

EPS export does not appear to honor the 'none' color.


July 14 2011, 21:21:36 UTC 2 years ago

possibly relevant:

Silvina Montes

September 8 2013, 17:50:14 UTC 7 months ago

What about subplotting? How would this be applied to a figure composed of subplots?