MATLAB figures

From Fluids Wiki
Revision as of 13:09, 7 July 2011 by Fluidslab (talk | contribs)
Jump to navigation Jump to search

This page describes the best practices for making figures in MATLAB for inclusion in a LATEX document

Vector graphics

Most of the time you will want to choose vector graphics, that is, use MATLAB's "painters" renderer. Code to select this is:

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

Set the figure size

Now you need to decide what the side of the figure should be. For a standard LaTeX report, we might want the plot to span the width of the page (ex: 6 inches wide) and half as tall (3 inches). To set 6x3 inch we use

set(gcf, 'PaperUnits', 'inches', 'PaperSize', [6 3],'PaperPosition',[0 0 6 3]);

Plot your data: heat map

Pcolor is often not the best way to produce a heatmap if you plan to use vector graphics. The reason is that each data point becomes two triangular elements in the PDF file. For most purposes, contourf is a superior replacement for pcolor. The code below demonstrates pcolor in figure 1 and contourf in figure 2.

x=linspace(0,100,1000); [X,Y]=meshgrid(x,x); data=X.*Y;
figure(1); pcolor(X,Y,data); shading flat
figure(2); [ch,ch]=contourf(X,Y,data,100); set(ch,'edgecolor','none');

Export as PDF

Now you print to a PDF file.

print('-dpdf','filename.pdf');

All of the commands here

Here are the above commands in one easy to copy-paste block,

set(gcf,'renderer','painters');
set(gcf, 'PaperUnits', 'inches', 'PaperSize', [6 3],'PaperPosition',[0 0 6 3]);
print('-dpdf','filename.pdf');

Inclusion in LaTeX

\begin{figure}
  \centering
  \includegraphics{filename.pdf}
  \caption{Description goes here.}
  \label{label:here}
\end{figure}