How to load .ttf file in matplotlib using mpl.rcParams?

Specifying a font family:

If all you know is the path to the ttf, then you can discover the font family name using the get_name method:

import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager

path="/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf"
prop = font_manager.FontProperties(fname=path)
mpl.rcParams['font.family'] = prop.get_name()
fig, ax = plt.subplots()
ax.set_title('Text in a cool font', size=40)
plt.show()

Specifying a font by path:

import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager

path="/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf"
prop = font_manager.FontProperties(fname=path)
fig, ax = plt.subplots()
ax.set_title('Text in a cool font', fontproperties=prop, size=40)
plt.show()

Leave a Comment