How to get presentable graphs and figures from MATLAB

MATLAB is great for making high-quality figures for publication. It has a pretty steep learning curve, though, if you're used to spreadsheets.

MATLAB's huge advantage for graphing is that everything can be tuned and tweaked with commands and scripts. You only have to fuss over the first graph in a series; once you have everything the way you want it to look, you can use the same formatting script on all your other graphs. Presto, they all look good and they all match.

In case anyone's interested, here are my notes and scripts for getting MATLAB graphs to look the way I want them to look, and to get them into usable formats at the correct scale for publication.

Exporting images from figure windows

Using the Image Processing Toolbox.

You should run “iptprefs”  and set “border style” to “tight”. In “loose” mode, it’ll embed a fat white border in each image file. Matlab remembers your iptprefs from day to day, but not between versions (2011a vs. 2011b). Also set "Initial magnification" to 100% so that you get a true pixel-for-pixel rendition of your image.

It's often a good idea to tweak the brightness and contrast (level and window) of an image for presentation. The following commands work pretty well. "im" is the name of the image array in the Matlab workspace.

>> figure; imshow(im); imcontrast;

(now play with the contrast sliders)

File > Save As, choose a file type and give it a name. The result will be the displayed image, with your contrast and level corrections.

PNG format is very good for most things, JPG is good for photographs, TIF gives excellent quality but sometimes has compatibility problems.

If you just want the image exactly as it is, without contrast/brightness tweaking, use:

>> imsave(im);

Generating press-quality graphs

Copy the following text into your own code, and tweak it as desired. A very convenient graphics export function is saveas2.m by Carlos Adrian Vargas Aguilera, free from the Matlab file exchange. Use saveas2.m to save the figure as a graphics file, at the appropriate resolution for publication.

% Figures Example: How to get MATLAB figures that work well with
% professional typesetting systems.
% Copy this code into your own scripts where you want to create plots, then
% tweak the parameters as you see fit.
% Matt Marsh
% 24 Dec 2011

%%Here's a bit of meaningless data to test it with

X = [0 1 2 3 4 5];
Y1 = [2 3 1 3 6 4];
Y2 = [5 4 3 1 2 2];

%%User sets the size of the figure

% Set the font size you want for the final printed copy, in points

% Column width and resolution at which the figure will be printed. Get this
% from the journal publisher. For a thesis, 6 inches and 300 dpi is normal.
width = 6;    % inches (I know it's not SI, but it's what paper comes in)
dpi = 300;    % dots per inch, use 300 unless told otherwise

% Aspect ratio (= width/height) for the figure
aspectratio = 4/3;

% Base name for saving the final figure
name = 'test';

%%Set up the axes

% Open a new figure window and call it 'f'
f = figure;

% Prevent resizing with the mouse. (This will automatically disable if you
% dock the completed figure into the Plot Tools window. If you resize with
% the mouse, the font size and column width will no longer match up.)

% Publishers specify column widths in inches

% Set the font family and size, default is 'Helvetica' but I like 'Garamond'

% Set the figure to the width and aspect ratio you specified
set(f, 'Position', [0 0 width width/aspectratio])

%%Plotting Commands

% Now replace this with your plotting commands
p = plot(X,Y1);

% Set the axis limits to suit your data, or comment out for auto-fit to the
% series you just plotted
xlim([0 5])
ylim([0 7])

% Lock the axes so that additional data won't mess things up
hold on

% If you want to plot more than one series, add the rest now
q = plot(X,Y2);

%%Labels, Legend & Style Tweaks

% Give it labels. Use LaTeX expressions for math and special characters.
xlabel('Postion, \epsilon_{nil}^{\circ}')
ylabel('Measurement, \Psi^{g}')
title('How To Plot Stuff')

% Legend commands
% ( see )
legend('Low power','High power')

% The legend box is ugly, turn it off- this must be the last legend command

% Set the line width (in points), and tweak the colours if you want to. Add
% any additional formatting commands here.
set(p,'LineWidth',1) %Default is 0.5 which is too thin when printed

% Unlock the axes, now that you're done plotting
hold off

%%Save in a few common formats

This uses saveas2.m by Carlos Adrian Vargas Aguilera. Look it up on the MATLAB file exchange if you don't already have it.

% Save the MATLAB figure for you to tweak later

% Save as a PNG raster image. Recommended for use with LibreOffice,
% MS-Office, journal submissions, and for PDFLaTeX users who don't want to
% fight with EPS conversion.

% Save as an EPS vector graphics file. EPS export in Matlab is a bit wonky,
% so use this only if you know what you're doing. For use with LaTeX and
% other professional typesetting software.

% An alternate EPS export command
%set(gcf, 'PaperPositionMode', 'auto')
%print -depsc2 finalPlot1.eps

% If you're even thinking of saving as a JPG, go read up on image formats.
% JPG is for photographs only and doesn't work for graphs or drawings.

And the result looks like this. (Click for the high-res version, scaled for 6" width, that comes out of saveas2.)



Add new comment