How to get a list of all the fonts currently available for Matplotlib?


Question

e.g. when I want to set font in

matplotlib.rc('font', **font)

Thanks.

1
33
1/6/2012 5:38:14 AM

Accepted Answer

import matplotlib.font_manager
matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')

Check this for other options.

52
1/7/2012 8:27:19 PM

To get a (readable) list of fonts available to matplotlib:

import matplotlib.font_manager
flist = matplotlib.font_manager.get_fontconfig_fonts()
names = [matplotlib.font_manager.FontProperties(fname=fname).get_name() for fname in flist]
print names

The documentation recommends get_fontconfig_fonts():

This is an easy way to grab all of the fonts the user wants to be made available to applications, without needing knowing where all of them reside.

Note that you can get the (inverse) name to font lookup easily by using the FontProperties class:

font = matplotlib.font_manager.FontProperties(family='TeX Gyre Heros')
file = matplotlib.font_manager.findfont(font)

findfont is robust as it returns a default font if it can't find matching properties.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon