Thursday, October 25, 2012

Getting Good Figures out of MATLAB

If you're working with MATLAB and need to produce a figure that can be included in a publication, getting the font sizes and line weights right is essential. Moreover, you should first output the figure in a vector format, either eps or pdf, and then convert to something else, such as png, if needed.

Here is an example script that produces the subsequent figure. Note the use of latex in both the figure axes and in the legend and title.

% Construct two curves
x = linspace(0,4);
u = exp(-x.^2);
v = sin(x);

% Plot the curves
plot(x, u, x, v,'linewidth',2);
set(gca, 'fontsize',16);

% Annotate the figure
xlabel('$x$','Interpreter', 'Latex');
lgnd = legend('$u$', '$v$');
set(lgnd,'interpreter','latex');
title('Example Figure','interpreter','latex');

% Save figure, note the argument 'epsc2', which saves color information
saveas(gcf,'fig1.eps','epsc2');
And here's an PNG conversion of the EPS output: