Print PDF from Matlabplot

Saving PDF of plots from Matplotlib is an effective way to use Matplotlib plots in LaTeX or for general sharing of high quality plots. This can be accomplished by either

  • save to PDF from the Matplotlib figure GUI
  • use figure.savefig() to save to PDF from Matplotlib–simply use a filename with “.pdf” suffix.

Headless (fast) PDF generation

Where local display of figures is not desired, that is, to save figures to disk without displaying them onscreen first, which is generally significantly faster, first do:

import matplotlib
matplotlib.use('Agg')

The Agg Matplotlib backend is commonly used in web / cloud services.

Example

from matplotlib.pyplot import figure

fg = figure()
ax = fg.gca()
ax.plot(range(10))

fg.savefig('line.pdf', bbox_inches='tight', dpi=300)