Note
Go to the endto download the full example code.
Using ttf font files#
Although it is usually not a good idea to explicitly point to a single ttf filefor a font instance, you can do so by passing apathlib.Path instance as thefont parameter. Note that passing paths asstrs is intentionally notsupported, but you can simply wrapstrs inpathlib.Paths as needed.
Here, we use the Computer Modern roman font (cmr10) shipped withMatplotlib.
For a more flexible solution, seeConfigure the font family andFonts demo (object-oriented style).
frompathlibimportPathimportmatplotlib.pyplotaspltimportmatplotlibasmplfig,ax=plt.subplots()fpath=Path(mpl.get_data_path(),"fonts/ttf/cmr10.ttf")ax.set_title(f'This is a special font:{fpath.name}',font=fpath)ax.set_xlabel('This is the default font')plt.show()

References
The use of the following functions, methods, classes and modules is shownin this example: