Using Matplotlib's LaTeX for Math Typesetting

PythonPythonBeginner
Practice Now

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

Introduction

In this tutorial, we will learn how to use Matplotlib's internal LaTeX parser and layout engine to create math text. We will be using Python programming language to write the code.

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`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) 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`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/legend_config("`Legend Configuration`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48823{{"`Using Matplotlib's LaTeX for Math Typesetting`"}} matplotlib/figures_axes -.-> lab-48823{{"`Using Matplotlib's LaTeX for Math Typesetting`"}} matplotlib/line_plots -.-> lab-48823{{"`Using Matplotlib's LaTeX for Math Typesetting`"}} matplotlib/legend_config -.-> lab-48823{{"`Using Matplotlib's LaTeX for Math Typesetting`"}} python/lists -.-> lab-48823{{"`Using Matplotlib's LaTeX for Math Typesetting`"}} python/tuples -.-> lab-48823{{"`Using Matplotlib's LaTeX for Math Typesetting`"}} python/sets -.-> lab-48823{{"`Using Matplotlib's LaTeX for Math Typesetting`"}} python/importing_modules -.-> lab-48823{{"`Using Matplotlib's LaTeX for Math Typesetting`"}} python/data_visualization -.-> lab-48823{{"`Using Matplotlib's LaTeX for Math Typesetting`"}} end

Importing Libraries

In this step, we will import the necessary libraries - matplotlib.

import matplotlib.pyplot as plt

Creating a Figure

In this step, we will create a figure and an axis object using the subplots() function.

fig, ax = plt.subplots()

Adding a Plot

In this step, we will add a plot to the axis object using the plot() function.

ax.plot([1, 2, 3], label=r'$\sqrt{x^2}$')
ax.legend()

Setting Labels

In this step, we will set the labels for the x and y-axis using the set_xlabel() and set_ylabel() functions.

ax.set_xlabel(r'$\Delta_i^j$', fontsize=20)
ax.set_ylabel(r'$\Delta_{i+1}^j$', fontsize=20)

Setting Title

In this step, we will set the title for the plot using the set_title() function.

ax.set_title(r'$\Delta_i^j \hspace{0.4} \mathrm{versus} \hspace{0.4} '
             r'\Delta_{i+1}^j$', fontsize=20)

Adding Text

In this step, we will add text to the plot using the text() function.

tex = r'$\mathcal{R}\prod_{i=\alpha_{i+1}}^\infty a_i\sin(2 \pi f x_i)$'
ax.text(1, 1.6, tex, fontsize=20, va='bottom')

Adjusting Layout

In this step, we will adjust the layout of the plot using the tight_layout() function.

fig.tight_layout()

Displaying the Plot

In this step, we will display the plot using the show() function.

plt.show()

Summary

In this tutorial, we have learned how to use Matplotlib's internal LaTeX parser and layout engine to create math text. We have also learned how to create a plot, add labels, title, text, and adjust the layout. This tutorial can be used as a reference for creating plots with math text in Matplotlib.

Other Python Tutorials you may like