07/05/2009

matplotlib and PDF bounding boxes

python-matplotlib can be a bit temperamental sometimes (at least 0.91). At the moment, I am preparing some figures to be included in a (PDF)LaTeX document, which should use correct fonts, math typesetting in the axis labels and a rather small figure size, so that I don't have to scale down in LaTeX, which would give me thin lines and tiny fonts. There is a very handy example in the SciPy Cookbook for all of that, which uses EPS output. No matter, pylab.savefig() also generates PDF when asked to do so - however, with little regard for the actual figure size, so usually the labels are cut off somewhere at the page border.
OK. EPS output and convert to PDF afterwards:
pylab.savefig('myfig.eps')
dum,my=os.popen('epstopdf myfig.eps')
A bit cumbersome; the converted eps figs look murky on some pdf viewers, but they print out OK.
Edit (02/2012): Being less ignorant about the matplotlib by now, I'd also recommend tweaking the figure's default parameter set, e.g.:
from matplotlib import pyplot as pl
import matplotlib
para = { 'axes.labelsize': 14, 'text.fontsize': 8, 'legend.fontsize': 11, 'xtick.labelsize': 10, 'ytick.labelsize': 10,  'figure.subplot.left' : 0.12, 'figure.subplot.right' : 0.98, 'figure.subplot.bottom' : 0.11, 'figure.subplot.top' : 0.97}
pl.rcParams.update(para)
Or, even easier, use the pyplot.subplots_adjust() method for on-the-fly modification. Savefig to PDF should  yield decent results without the EPS workaround.

No comments: