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.
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.