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).

from pathlib import Path

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

fig, ax = plt.subplots()

fpath = Path(mpl.get_data_path(), "fonts/ttf/cmr10.ttf")
prop = fm.FontProperties(fname=fpath)
ax.set_title(f'This is a special font: {}', fontproperties=prop)
ax.set_xlabel('This is the default font')


The use of the following functions, methods, classes and modules is shown in this example:

import matplotlib


<function Axes.set_title at 0x7f154d197700>

Keywords: matplotlib code example, codex, python plot, pyplot Gallery generated by Sphinx-Gallery