Nice figures using MATLAB

MATLAB (or, conversely, Octave) is pretty great at processing data and quickly plotting them, but its default plot possibilities (better now after Release > 2014b) are definitely not perfect.

Four plots, using MATLAB default properties
Four plots, using MATLAB default properties. Not great, but not bad.

Image and font sizes ...

Imposing the font size and the total size of an image (or PDF export) can be tricky. It requires to know the final size of the image you will use. For this, you can cheat and determine what the line size is using TeX commands:

\the\columnwidth
\the\textwidth

This will output something like 510.0pt. We then have to recast the 510 directly in MATLAB for both raster (position option) and vector (paperposition, paperwidth, paperunits options) formats:

set(gca, 'fontsize', 12);
set(gcf, 'paperunits'   , 'points',          'position', [100 100 510 300], ...
         'paperposition', [100 100 510 300], 'papersize', [510 300]);

which will impose a font size of 12 points on a 510 point wide image. Beware of the argument order for the figure (paperunits before papersize) or otherwise everything will blow up.


Typeface consistency

MATLAB (> R2014b) can now handle LaTeX tick labels. Hurray !

set(gca, 'TickLabelInterpreter', 'latex');

Markers

I don't really like MATLAB markers since the new versions. Let's plot a basic line :

plot([1 2 3 4 5 6], [1.1 1.0 1.05 0.95 1.0 0.98], 'o')

then export it to .pdf. Zooming on the markers will result in this :

Not really a circle
Not really a circle

I don't know any workaround for the moment.


An alternative : qTikZ / matlab2TikZ

If you want perfectly clean .pdf files compatible with LaTeX based on MATLAB graphs, you can use qTikZ coupled with matlab2TikZ.

  • matlab2TikZ converts your .fig into a .tex file for further modification.
  • qTikZ allows to edit the resulting .tex file and watch the resulting figure (with correct cropping) in real time.

It provides really great results. And you do not have to worry about font size relative to figure size, since in .tex, the two parameters are set separately. Sample figure becomes:

QTikZ default output
QTikZ default output

This image has a major advantage compared to MATLAB output : it does not create unwanted white padding (i.e. the axis size is exactly what you requested), the default font is LaTeX, and the circle marker issue is gone. You may have some trouble with colorbars, but nothing too worrisome.


Color maps

The venerable Jet colormap is now widely hated around the world for its strange light gradients at specific places and its poor lisibility in black and white. The parula colormap does a great job since MATLAB R2014b. A free alternative compatible with color-blindedness is viridis. Other useful candidates like cbrewer may be useful.

One last thing: if you want to change the default color order in MATLAB (when using the hold on command), you can use this (> R2014b):

set(groot, 'defaultAxesColorOrder', cmap(nplots));

where your colormap (cmap) has nplots lines.

Leave a Comment