You are reading an old version of the documentation (v2.1.1). For the latest version see https://matplotlib.org/stable/
matplotlib
Fork me on GitHub


Travis-CI:

Related Topics

This Page

Using a ttf font file in MatplotlibΒΆ

Although it is usually not a good idea to explicitly point to a single ttf file for a font instance, you can do so using the font_manager.FontProperties fname argument.

Here, we use the Computer Modern roman font (cmr10) shipped with Matplotlib.

For a more flexible solution, see Configuring the font family and Fonts demo (object-oriented style).

../../_images/sphx_glr_font_file_001.png
import os
from matplotlib import font_manager as fm, pyplot as plt, rcParams

fig, ax = plt.subplots()

fpath = os.path.join(rcParams["datapath"], "fonts/ttf/cmr10.ttf")
prop = fm.FontProperties(fname=fpath)
fname = os.path.split(fpath)[1]
ax.set_title('This is a special font: {}'.format(fname), fontproperties=prop)
ax.set_xlabel('This is the default font')

plt.show()

Total running time of the script: ( 0 minutes 0.016 seconds)

Gallery generated by Sphinx-Gallery