Reputation: 13357
e.g. when I want to set font
in
matplotlib.rc('font', **font)
Upvotes: 62
Views: 97091
Reputation: 41417
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']
Upvotes: 13
Reputation: 877
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
...
Upvotes: 21
Reputation: 15863
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:
Upvotes: 28
Reputation: 676
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.
Upvotes: 33