MATLAB Figures

The default plotting parameters in MATLAB produce poor quality figures. Luckily, with a few tweaks, we can make them a lot prettier.

Standard graphics

Here is what we get with standard graphics.

x = -pi:0.1:pi;
plot(x, sin(x))
Default save as…png in MATLAB r2010b

The plot is OK, but there are some things we'd like to improve:

  • The background might be gray. We want white. Newer versions of MATLAB already do this (like above).
  • The ticks go inwards. I like them pointing out.
  • The box goes all the way around the plot. I usually like it only on two sides in an “L”.
  • The plot is bitmap. We'd like to export in a vector format.

Put default plotting parameters in startup.m

When MATLAB launches, it looks for a startup.m file in your path. You can use this to set your preferred default plotting options, among other things. Here are the lines I added to take care of the first two issues, above.

set(0, 'DefaultAxesTickDir', 'out')

The format for these default properties begins with DefaultFigure for figure properties (i.e. the property you want to set is in get(gcf)), and DefaultAxes for axis properties (i.e. the property you want to set is in get(gca)).

The plot function changes some axis properties even if you have set defaults. Box is one of these, so unfortunately it doesn't help much to set a default here.

Instead, I settle for using box off after my plotting commands.

Print vector graphics to pdf

Lastly, we can save the figure as a pdf instead of one of the default bitmap formats. The one trick to this is that MATLAB will try to save the figure in the middle of a full blank page by default. To get around this, I like to keep a small function in my MATLAB path that makes it convenient to set the figure and page dimensions.

function printFig(h, name, width, height)
    set(h, 'Color', 'white')
    set(h, 'PaperSize', [width height], 'PaperPosition', [0 0 width height])
    print(h, '-dpdf', name)

Then you can save a figure like:

printFig(gcf, 'MATLAB sinx.pdf', 4, 3)
Snapshot of .pdf output
computing/matlab-figures.txt · Last modified: 2010/12/29 1:49 pm PST by John Colby
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki