Thursday, August 30, 2012

Auto-generate PDF output

I'm plotting 4 panels of spectra with line identifications on screen.    I just figured out a quick way to automatically generate a multipage PDF of my multi-panel plots:

Add this near the beginning of the plotting script:

from matplotlib.backends.backend_pdf import PdfPages
pp = PdfPages('multipage.pdf')

Add this where you generate each panel:
     pp.savefig()    

Finally, add this at the end to close the file:
pp.close()
Update: By default, Matplotlib pads acres of whitespace around the PDF.  That's annoying when embedding PDF plots in a latex paper.   Here's how to prevent the whitespace:
plt.savefig("output_plot.pdf", bbox_inches='tight', pad_inches=0.1)

No comments:

Post a Comment