How can I use any type of font in my font library on my computer (e.g. *otf
or *ttf
) in all my matplotlib
figures?

- 275,208
- 71
- 604
- 463

- 2,806
- 6
- 25
- 31
4 Answers
See the example here: http://matplotlib.sourceforge.net/examples/api/font_file.html
In general, you'd do something like this if you're wanting to use a specific .ttf
file. (Keep in mind that pointing to a specific font file is usually a bad idea!)
import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10))
prop = fm.FontProperties(fname='/usr/share/fonts/truetype/groovygh.ttf')
ax.set_title('This is some random font', fontproperties=prop, size=32)
plt.show()
Usually, you'd just point to the name of the font, and let matplotlib worry about finding the specific file. E.g.
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.title('This is some random font', family='GroovyGhosties', size=32)
plt.show()
If you want to have matplotlib always use a particular font, then customize your .matplotlibrc
file. (font.family
is what you'd want to set. Note that you should specify the name of the font, not the path to a specific .ttf file.)
As an example of doing this dynamically (i.e. without setting up a specific .matplotlibrc
file):
import matplotlib as mpl
mpl.rcParams['font.family'] = 'GroovyGhosties'
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.title('Everything is crazy!!!', size=32)
plt.show()

- 275,208
- 71
- 604
- 463
-
4it doesn't work for me (Python 3.6), if the font is not recognized it just uses the default font – irenemeanspeace Aug 07 '18 at 13:27
On *nix, you can use all your system fonts by enabling matplotlib's fontconfig backend.
However matplotlib does not really talk to fontconfig libraries it emulates its behaviour by running fontconfig cli utilities.
Therefore, nuking the matplotlib fontconfig cache so it discovers new fonts can be a lifesaver (the existence of this cache is direct proof of lack of complete fontconfig integration).

- 2,345
- 14
- 13
-
2Probably this doesn't answer the question however you saved my last drops of sanity. Thanks. A new system font wasn't being applied so I just deleted these folders: ~/.matplotlib, ~/.cache/matplotlib, ~/.cache/fontconfig Be sure to also restart your python environment. – Mateo de Mayo Oct 14 '20 at 07:20
-
You can specify the font and override the default font family in matplot config as well, e.g. for *nix
~/.matplotlib/matplotlibrc
font.family: sans-serif
font.sans-serif: your font,sans-serif

- 1
- 1
Here is an example how to set any OTF/TTF file to the default font of Mathplotlib. This way you do not need to pass the font as a parameter to every figure.
import os
import matplotlib
import matplotlib.font_manager as font_manager
def load_matplotlib_local_fonts():
# Load a font from TTF file,
# relative to this Python module
# https://stackoverflow.com/a/69016300/315168
font_path = os.path.join(os.path.dirname(__file__), 'Humor-Sans.ttf')
assert os.path.exists(font_path)
font_manager.fontManager.addfont(font_path)
prop = font_manager.FontProperties(fname=font_path)
# Set it as default matplotlib font
matplotlib.rc('font', family='sans-serif')
matplotlib.rcParams.update({
'font.size': 16,
'font.sans-serif': prop.get_name(),
})

- 82,057
- 50
- 264
- 435