Matplotlib Math Fontfamily

PythonPythonBeginner
Practice Now

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

Introduction

This lab will guide you through the process of changing the family of fonts for each individual text element in a plot using the Python Matplotlib library.

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 python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/AdvancedTopicsGroup -.-> python/iterators("`Iterators`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48825{{"`Matplotlib Math Fontfamily`"}} matplotlib/importing_matplotlib -.-> lab-48825{{"`Matplotlib Math Fontfamily`"}} matplotlib/figures_axes -.-> lab-48825{{"`Matplotlib Math Fontfamily`"}} matplotlib/line_plots -.-> lab-48825{{"`Matplotlib Math Fontfamily`"}} python/for_loops -.-> lab-48825{{"`Matplotlib Math Fontfamily`"}} python/tuples -.-> lab-48825{{"`Matplotlib Math Fontfamily`"}} python/sets -.-> lab-48825{{"`Matplotlib Math Fontfamily`"}} python/importing_modules -.-> lab-48825{{"`Matplotlib Math Fontfamily`"}} python/standard_libraries -.-> lab-48825{{"`Matplotlib Math Fontfamily`"}} python/iterators -.-> lab-48825{{"`Matplotlib Math Fontfamily`"}} python/math_random -.-> lab-48825{{"`Matplotlib Math Fontfamily`"}} python/data_visualization -.-> lab-48825{{"`Matplotlib Math Fontfamily`"}} python/build_in_functions -.-> lab-48825{{"`Matplotlib Math Fontfamily`"}} end

Import Required Libraries

First, we need to import the required libraries. We will be using Matplotlib to create the plot and to manipulate the text elements.

import matplotlib.pyplot as plt

Create the Plot

Now, we will create a simple plot for the background using the plot() function.

fig, ax = plt.subplots(figsize=(6, 5))
ax.plot(range(11), color="0.9")

Set Text in the Plot

Next, we will set the text in the plot using the text() function. We will use the math_fontfamily parameter to change the font family for each individual text element.

## A text mixing normal text and math text.
msg = (r"Normal Text. $Text\ in\ math\ mode:\ "
       r"\int_{0}^{\infty } x^2 dx$")

## Set the text in the plot.
ax.text(1, 7, msg, size=12, math_fontfamily='cm')

## Set another font for the next text.
ax.text(1, 3, msg, size=12, math_fontfamily='dejavuserif')

Set Font for the Title

We can also change the font family for the title using the math_fontfamily parameter.

ax.set_title(r"$Title\ in\ math\ mode:\ \int_{0}^{\infty } x^2 dx$",
             math_fontfamily='stixsans', size=14)

Display the Plot

Finally, we will display the plot using the show() function.

plt.show()

Summary

In this lab, we learned how to change the family of fonts for each individual text element in a plot using the math_fontfamily parameter in Matplotlib. This feature allows us to customize the look of our plots and make them more visually appealing.

Other Python Tutorials you may like