44

e.g. when I want to set font in

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

Thanks.

nye17
  • 12,057
  • 11
  • 54
  • 64
  • Apparently none of these will list the fonts available in the `mpl-data` directory found in `~/anaconda/lib/pythonX.X/site-packages/matplotlib/mpl-data/fonts/ttf` (try `import matplotlib; matplotlib.matplotlib_fname()` to get the exact location). Despite the fact that **using `fname=` to load fonts in this location is sucessful**. Does anyone have an answer that will do so? – Luke Davis Dec 08 '17 at 23:52

4 Answers4

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

Check this for other options.

imsc
  • 7,004
  • 6
  • 46
  • 68
  • cool, thanks! btw: apparently you have to `import matplotlib.font_manager` to make things happen ;-) – nye17 Jan 06 '12 at 16:12
  • 1
    Also, the arguments specified are the defaults, so `matplotlib.font_manager.findSystemFonts()` returns the same thing. – Joe Kington Jan 07 '12 at 23:43
  • 12
    Any idea how to get the names of the installed font-families instead of the single files? – pwuertz Jul 01 '12 at 14:27
31

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.

Torbjørn T.
  • 345
  • 1
  • 3
  • 10
alodi
  • 646
  • 1
  • 6
  • 9
  • 1
    Neat, thanks. Looks like this one gives the actual **callable names** while the accepted answer shows the actual `.ttf` files. Unfortunately for whatever reason **this fails on my macbook (MacOS Sierra)**, but works on some remote Linux servers I use. The accepted answer works everywhere. – Luke Davis Dec 08 '17 at 13:11
  • 1
    Gives error "RuntimeError: In FT2Font: Can not load face. Unknown file format." – Asier R. Jun 04 '20 at 08:59
  • 4
    Returns empty list Python 3.8.3 + matploblib 3.3.2 – YTZ Nov 22 '20 at 16:14
21

Per this blog post, this code will get you fonts available and samples:

import matplotlib.font_manager
from IPython.core.display import HTML

def make_html(fontname):
    return "<p>{font}: <span style='font-family:{font}; font-size: 24px;'>{font}</p>".format(font=fontname)

code = "\n".join([make_html(font) for font in sorted(set([f.name for f in matplotlib.font_manager.fontManager.ttflist]))])

HTML("<div style='column-count: 2;'>{}</div>".format(code))

For example:

enter image description here

Max Ghenis
  • 12,769
  • 13
  • 73
  • 119
  • This didn't work for me in Google Colab as least. It seems Matplotlib fonts aren't necessarily available outside of Matplotlib maybe? – getup8 Jan 22 '21 at 04:33
10

The accepted answer provides only a list of paths to fonts on your machine, but not the font name that you can pass to rcParams. @Alodi's answer addresses this point, but is outdated.

In Python 3.8.8 with Matplotlib 3.3.4, you can use the following:

import matplotlib.font_manager
fpaths = matplotlib.font_manager.findSystemFonts()

for i in fpaths:
    f = matplotlib.font_manager.get_font(i)
    print(f.family_name)

It prints a list of font names:

Padauk Book
Laksaman
Waree
Umpush
Latin Modern Roman Demi
Tlwg Mono
Gubbi
...
Max
  • 502
  • 4
  • 15