MATLAB figures: Difference between revisions

From Fluids Wiki
Jump to navigation Jump to search
No edit summary
Line 9: Line 9:
</syntaxhighlight>
</syntaxhighlight>


== Set the figure size ==
Now you need to decide what the size 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
Now you need to decide what the size 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


Line 16: Line 15:
</syntaxhighlight>
</syntaxhighlight>


== 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.  
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.  


Line 25: Line 23:
</syntaxhighlight>
</syntaxhighlight>


== Export as PDF ==
Now you print to a PDF file.
Now you print to a PDF file.


Line 32: Line 29:
</syntaxhighlight>
</syntaxhighlight>


== All of the commands here ==
Here are the above commands in one easy to copy-paste block,
Here are the above commands in one easy to copy-paste block,


Line 44: Line 40:


<syntaxhighlight lang="latex">
<syntaxhighlight lang="latex">
\begin{figure}
\begin{figure}
   \centering
   \centering
Line 52: Line 47:
\end{figure}
\end{figure}
</syntaxhighlight>
</syntaxhighlight>
== Subplot magic ==
MATLAB's subplot command is useful but it's not always the best at making multi-panel figures. Here is a script that creates four subplots, manually specifies their size and location, as well as adds one colourbar.
<syntaxhighlight lang="matlab">
clear; clf;
% create four subplots, store handles in A, B, C and D and colorbar in E
A=subplot(2,2,1); imagesc(magic(6)); caxis([0 75]); xlabel('x (m)'); ylabel('y (m)');
B=subplot(2,2,2); imagesc(magic(7)); caxis([0 75]); xlabel('x (m)'); ylabel('y (m)');
C=subplot(2,2,3); imagesc(magic(8)); caxis([0 75]); xlabel('x (m)'); ylabel('y (m)');
D=subplot(2,2,4); imagesc(magic(9)); caxis([0 75]); xlabel('x (m)'); ylabel('y (m)');
E=colorbar('Location','Southoutside');
% Specify some parameters for the plot
x0  = 0.5;  % spacing between and around figures (inches)
y0  = 1;    % offset from the bottom (inches)
w    = 2.25; % size of each subfigure (w x w inches)
y0cb = 0.25; % offset of colourbar from botom (inches)
% Now specify each figure's location and dimensions
% as [x  y  width  height]:
% 'x' and 'y' are position of the lower-left corner of each panel
% 'length' and 'height' are the dimensions of each panel
set(A,'Units','inches','Position',[x0    y0+w+x0  w w]);
set(B,'Units','inches','Position',[w+2*x0 y0+w+x0  w w]);
set(C,'Units','inches','Position',[x0    y0      w w]);
set(D,'Units','inches','Position',[w+2*x0 y0      w w]);
set(E,'Units','inches','Position',[x0+0.25*w  y0cb  1.5*w+x0  0.25]);
% total width, height
W=3*x0 + 2*w;      % three spacing and two panels wide
H=2*w + 2*x0 + y0; % two spacing, two panels and one y0 in height
fprintf('Figure is %.2fin wide, %.2fin tall\n',W,H);
set(gcf, 'PaperUnits', 'inches', 'PaperSize', [W H],'PaperPosition',[0 0 W H]);
set(gcf, 'Renderer', 'Painters');
print -dpdf output.pdf
</syntaxhighlight>
This example produces a figure that is 6 inches wide and 6.5 inches tall. It will fit into a standard LaTeX document without any scaling. The text size on the figure will be the same in your LaTeX.

Revision as of 17:05, 14 October 2011

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');

Now you need to decide what the size 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]);

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');

Now you print to a PDF file.

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

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}

Subplot magic

MATLAB's subplot command is useful but it's not always the best at making multi-panel figures. Here is a script that creates four subplots, manually specifies their size and location, as well as adds one colourbar.

clear; clf;

% create four subplots, store handles in A, B, C and D and colorbar in E
A=subplot(2,2,1); imagesc(magic(6)); caxis([0 75]); xlabel('x (m)'); ylabel('y (m)');
B=subplot(2,2,2); imagesc(magic(7)); caxis([0 75]); xlabel('x (m)'); ylabel('y (m)');
C=subplot(2,2,3); imagesc(magic(8)); caxis([0 75]); xlabel('x (m)'); ylabel('y (m)');
D=subplot(2,2,4); imagesc(magic(9)); caxis([0 75]); xlabel('x (m)'); ylabel('y (m)');
E=colorbar('Location','Southoutside');

% Specify some parameters for the plot
x0   = 0.5;  % spacing between and around figures (inches)
y0   = 1;    % offset from the bottom (inches)
w    = 2.25; % size of each subfigure (w x w inches)
y0cb = 0.25; % offset of colourbar from botom (inches)

% Now specify each figure's location and dimensions
% as [x  y  width  height]:
% 'x' and 'y' are position of the lower-left corner of each panel
% 'length' and 'height' are the dimensions of each panel
set(A,'Units','inches','Position',[x0     y0+w+x0  w w]);
set(B,'Units','inches','Position',[w+2*x0 y0+w+x0  w w]);
set(C,'Units','inches','Position',[x0     y0       w w]);
set(D,'Units','inches','Position',[w+2*x0 y0       w w]);

set(E,'Units','inches','Position',[x0+0.25*w   y0cb   1.5*w+x0   0.25]);

% total width, height
W=3*x0 + 2*w;      % three spacing and two panels wide
H=2*w + 2*x0 + y0; % two spacing, two panels and one y0 in height
fprintf('Figure is %.2fin wide, %.2fin tall\n',W,H);
set(gcf, 'PaperUnits', 'inches', 'PaperSize', [W H],'PaperPosition',[0 0 W H]);
set(gcf, 'Renderer', 'Painters');

print -dpdf output.pdf


This example produces a figure that is 6 inches wide and 6.5 inches tall. It will fit into a standard LaTeX document without any scaling. The text size on the figure will be the same in your LaTeX.