Introduction
In this tutorial, we will learn how to use a ttf font file in Matplotlib. We will use the Computer Modern Roman font (cmr10) shipped with Matplotlib to display a special font in the plot.
VM Tips
After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.
Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.
If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.
Import the necessary libraries
We need to import matplotlib.pyplot and matplotlib to create and display the plot.
import matplotlib.pyplot as plt
import matplotlib as mpl
Create the plot
We create a plot using the subplots() method and store the figure and axes objects in fig and ax variables respectively.
fig, ax = plt.subplots()
Set the font path
We set the font path by using the mpl.get_data_path() method to get the path of the data directory and then append the path of the font file cmr10.ttf to it using Path() method from the pathlib module.
from pathlib import Path
fpath = Path(mpl.get_data_path(), "fonts/ttf/cmr10.ttf")
Set the font for the title
We set the font for the title of the plot using the set_title() method of the Axes class. We pass the font path as the font parameter and the name of the font file as the title of the plot.
ax.set_title(f'This is a special font: {fpath.name}', font=fpath)
Display the plot
We display the plot using the show() method.
plt.show()
Summary
In this tutorial, we learned how to use a ttf font file in Matplotlib to display a special font in the plot. We used the set_title() method to set the font for the title of the plot and passed the font path as the font parameter.