Skip to content

Instantly share code, notes, and snippets.

@prnthp
Created March 2, 2020 03:38
Show Gist options
  • Save prnthp/b7e4c6d0a25e51d93bb95dbbb34241c1 to your computer and use it in GitHub Desktop.
Save prnthp/b7e4c6d0a25e51d93bb95dbbb34241c1 to your computer and use it in GitHub Desktop.
How to generate and save vector figures in MATLAB

How to generate and save vector figures in MATLAB

This is how I generate .eps/.pdf/.png files for publications. This will produce nice vector figures and rasters at a nice crispy 300 DPI

Add this to the top of your script

fontname = 'Arial'; % or your favorite font, IEEE prefers Arial in figures. Helvetica is also sexy.
set(0,'DefaultAxesFontName',fontname,'DefaultTextFontName',fontname);
set(0,'DefaultAxesFontSize',8);
set(0,'DefaultLineLineWidth',1.2);
colors = get(0, 'DefaultAxesColorOrder');

When creating a new figure window

fignum = 1;
fig = figure(fignum); clf; hold on; grid on; box on;
fig.Renderer = 'Painters'; % this ensures that your figure will be vector/ps

When you are ready to save

fig.PaperUnits = 'inches';
fig.PaperPosition = [0 0 6 4]; % adjust this to the aspect ratio you want in your publication
figpath = strcat('figure_',num2str(fignum));
print(figpath,'-dpng','-r300') % or change this to 600 (dpi) for crispier figs
print(figpath,'-depsc','-r300') % or change this to 600 (dpi) for crispier figs

Optional: Use ghostscript-based eps2pdf MATLAB function to generate pdf

eps2pdf(strcat(figpath,'.eps'),'/usr/local/bin/gs') % second parameter is path to ghostscript exec

For best results, use the .pdf in LaTeX!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment