Reputation: 735
I need to generate figures that fit into a particular width and use a particular font size, preferably without post-processing the pdf file.
On my system, the default GUI backend is 'TkAgg' and the default font size is 12.0 as reported by the get_fontsize()
method on a text object. When I generate a figure using
f = pyplot.figure(figsize=(2.0,2.0))
f.text(0.5,0.5,'TEXT')
pyplot.show()
and save as PDF, the figure size is too large. pdfinfo
reports
Creator: matplotlib 1.1.1rc, http://matplotlib.sf.net
Producer: matplotlib pdf backend
CreationDate: Sat May 25 10:52:02 2013
Tagged: no
Pages: 1
Encrypted: no
Page size: 196.2 x 153 pts
File size: 9357 bytes
Optimized: no
PDF version: 1.4
196.2 pt == 2.725 inch
However, if I resize the figure to be 2 inch wide, the fontsize matches (visually) with 12pt LaTeX fonts.
Contrarily, skipping the GUI produces a figure with the right dimensions, but fonts too large:
import matplotlib
matplotlib.use('PDF')
f = pyplot.figure(figsize=(2.0,2.0))
f.text(0.5,0.5,'TEXT')
f.savefig('test.pdf')
Creator: matplotlib 1.1.1rc, http://matplotlib.sf.net
Producer: matplotlib pdf backend
CreationDate: Sat May 25 10:50:42 2013
Tagged: no
Pages: 1
Encrypted: no
Page size: 144 x 144 pts
File size: 9254 bytes
Optimized: no
PDF version: 1.4
144 pt == 2.0 inch
The Cairo backend produces the same result. Adding f.set_size_inches((2,2))
doesn't change anything either.
Is there a recipe to get font and figure sizes just right with matplotlib?
Upvotes: 2
Views: 4159
Reputation: 87376
check your .matplotlibrc
and look for sivefig.bbox
, make sure it is commented out, or set to something that is not tight
.
You can try:
fig.savefig('so.png', bbox_inches=matplotlib.transforms.Bbox(np.array(((0, 0), (2, 2)))))
which explictly sets the bounding box.
That said, if you do not have anything in your .matplotlibrc
, you should report this as a bug.
Upvotes: 2
Reputation: 1008
You should use fig.set_size_inches as you can give exactly the physical size of the pdf.
Upvotes: 0