57

e.g. when I want to set font in

matplotlib.rc('font', **font)
tdy
  • 36,675
  • 19
  • 86
  • 83
nye17
  • 12,857
  • 11
  • 58
  • 68
  • 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

5 Answers5

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

Check this for other options.

imsc
  • 7,492
  • 7
  • 47
  • 69
  • 2
    Also, the arguments specified are the defaults, so `matplotlib.font_manager.findSystemFonts()` returns the same thing. – Joe Kington Jan 07 '12 at 23:43
  • 16
    Any idea how to get the names of the installed font-families instead of the single files? – pwuertz Jul 01 '12 at 14:27
33

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.
  • 365
  • 1
  • 3
  • 10
alodi
  • 666
  • 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
  • matplotlib.font_manager.get_fontconfig_fonts() C:\QGB\Anaconda3\Scripts\ipython:1: MatplotlibDeprecationWarning: The get_fontconfig_fonts function was deprecated in Matplotlib 3.5 and will be removed two minor releases later. Out[178]: [] – CS QGB Jun 05 '22 at 18:25
  • This works if you just replace `get_fontconfig_fonts()` with `findSystemFonts()` – Brandon Kuczenski May 12 '23 at 08:31
27

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
  • 14,783
  • 16
  • 84
  • 132
  • 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
17

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
  • 695
  • 5
  • 18
  • As of 2022 this should be the accepted answer. BTW, jeez I have a lot of fonts... – Michael Currie May 23 '22 at 13:55
  • 1
    When I run this I get a lot of duplicates. I got a more useful list by adding the fonts to a set and then printing the set after the loop. That has the added benefit of sorting the list. – craigim Jul 29 '22 at 19:28
9

New in matplotlib 3.6.0

There is now a helper method get_font_names() to list all available fonts:

from matplotlib import font_manager
font_manager.get_font_names()

Note that the resulting list won't be alphabetical. That's fine if you're just checking something programmatically, but if you're checking visually, sort alphabetically for easier scanning:

sorted(font_manager.get_font_names())

# ['Advent Pro',
#  'Anonymous Pro',
#  ...
#  'DejaVu Sans',
#  'DejaVu Sans Mono',
#  'DejaVu Serif',
#  ...
#  'Noto Mono',
#  'Noto Sans',
#  'Noto Serif',
#  ...
#  'Roboto',
#  'Roboto Flex',
#  'Roboto Mono',
#  'Roboto Serif',
#  ...
#  'TeX Gyre Adventor',
#  'TeX Gyre Bonum',
#  'TeX Gyre Chorus',
#  'TeX Gyre Cursor',
#  'TeX Gyre Heros',
#  'TeX Gyre Pagella',
#  'TeX Gyre Schola',
#  'TeX Gyre Termes',
#  ...
#  'Ubuntu',
#  'Ubuntu Mono']
tdy
  • 36,675
  • 19
  • 86
  • 83