Matplotlib PDF export uses wrong font

Posted by Konrad Rudolph on Stack Overflow See other posts from Stack Overflow or by Konrad Rudolph
Published on 2010-05-09T11:46:16Z Indexed on 2010/05/09 11:48 UTC
Read the original article Hit count: 348

Filed under:
|
|

I want to generate high-quality diagrams for a presentation. I’m using Python’s matplotlib to generate the graphics. Unfortunately, the PDF export seems to ignore my font settings.

I tried setting the font both by passing a FontProperties object to the text drawing functions and by setting the option globally. For the record, here is a MWE to reproduce the problem:

import scipy
import matplotlib
matplotlib.use('cairo')
import matplotlib.pylab as pylab
import matplotlib.font_manager as fm

data = scipy.arange(5)

for font in ['Helvetica', 'Gill Sans']:
    fig = pylab.figure()
    ax = fig.add_subplot(111)
    ax.bar(data, data)
    ax.set_xticks(data)
    ax.set_xticklabels(data, fontproperties = fm.FontProperties(family = font))
    pylab.savefig('foo-%s.pdf' % font)

In both cases, the produced output is identical and uses Helvetica (and yes, I do have both fonts installed).

Just to be sure, the following doesn’t help either:

matplotlib.rc('font', family = 'Gill Sans')

Finally, if I replace the backend, instead using the native viewer:

matplotlib.use('MacOSX')

I do get the correct font displayed – but only in the viewer GUI. The PDF output is once again wrong.

To be sure – I can set other fonts – but only other classes of font families: I can set serif fonts or fantasy or monospace. But all sans-serif fonts seem to default to Helvetica.

© Stack Overflow or respective owner

Related posts about python

Related posts about matplotlib