Using a TTF Font File in Matplotlib

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48743{{"`Using a TTF Font File in Matplotlib`"}} matplotlib/figures_axes -.-> lab-48743{{"`Using a TTF Font File in Matplotlib`"}} python/tuples -.-> lab-48743{{"`Using a TTF Font File in Matplotlib`"}} python/sets -.-> lab-48743{{"`Using a TTF Font File in Matplotlib`"}} python/importing_modules -.-> lab-48743{{"`Using a TTF Font File in Matplotlib`"}} python/using_packages -.-> lab-48743{{"`Using a TTF Font File in Matplotlib`"}} python/data_visualization -.-> lab-48743{{"`Using a TTF Font File in Matplotlib`"}} end

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.

Other Python Tutorials you may like