[Matplotlib-users] How to register custom font (from ttf) with matplotlib

Duncan Macleod duncan.macleod at ligo.org
Fri May 20 12:46:04 EDT 2016


Hi all,

I have a custom font file (ttf) I would like to use with matplotlib. I
don't have sudo access, so can't just add it to the standard fonts
directory, but would like to add it to matplotlib's list of known fonts.

I know that I can do something like this for one-off uses:

from matplotlib import font_manager
fp = font_manager.FontProperties(fname='myfont.ttf')
fig = pyplot.figure()
ax = fig.gca()
ax.plot([1, 2, 3, 4, 5])
ax.set_xlabel('X-axis label', fontproperties=fp)

But I would like to use the font as the default for all text objects in my
figure. I have tried using rcParams:

rcParams['font.family'] = fp.get_family()
rcParams['font.sans-serif'] = fp.get_name()

but I get the standard warning:

UserWarning: findfont: Font family [u'sans-serif'] not found. Falling back
to Bitstream Vera Sans

and it uses the default font, even after blowing away my fontList.cache
file (many times). I suspect that I have loaded the font using the
FontProperties object, but haven't actually 'registered' it with matplotlib
so it knows what to do when I specify the font name, or something like that.

Any help would be great.


Many thanks
Duncan
-- 
Duncan Macleod
duncan.macleod at ligo.org
LIGO Data Grid systems development
Louisiana State University
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20160520/32d15e0c/attachment.html>


More information about the Matplotlib-users mailing list