How to insert math equations in a matplotlib figure ?

Published: April 17, 2023

Tags: Python; Matplotlib; LaTeX;

DMCA.com Protection Status

Adding equations to Matplotlib figures is relatively easy; all you need is some basic knowledge of LaTeX:

Using Matplotlib

It is important to know that Matplotlib has its own TeX expression parser, layout engine, and fonts, so it's unnecessary to have TeX installed. The layout engine is adapted from the layout algorithms in Donald Knuth's TeX. However, if you require advanced features and greater flexibility, you can use LaTeX with Matplotlib, as explained in the following section.

Note: Please note that the way equations appear may vary depending on whether or not LaTeX is being used.

Adding math equation in title

You can include math text in any text element by using raw strings (denoted by an 'r' before the quotes) and enclosing the math text with dollar signs ($), similar to TeX. It's possible to mix regular text and mathtext within the same string.

import matplotlib.pyplot as plt

ax = plt.subplot(111)

plt.xlim(0,10)
plt.ylim(0,10)

plt.title(r"$\sum_{k=0}^{\infty}k$",fontsize=18)

plt.grid()

plt.savefig("matplotlib_math_equation_01.png", bbox_inches='tight',dpi=100, facecolor='white')

plt.show()

How to insert math equations in a matplotlib figure ?
How to insert math equations in a matplotlib figure ?

Adding math equation with annotate

import matplotlib.pyplot as plt

ax = plt.subplot(111)

plt.xlim(0,10)
plt.ylim(0,10)

ax.text(5,5,r"$\sum_{k=0}^{\infty}k$",horizontalalignment='center',fontsize=18)

plt.grid()

plt.savefig("matplotlib_math_equation_02.png", bbox_inches='tight',dpi=100, facecolor='white')

plt.show()

How to insert math equations in a matplotlib figure ?
How to insert math equations in a matplotlib figure ?

Using Matplotlib and LaTeX

Installing LaTeX

Installing LaTeX on your computer is a relatively straightforward process. Depending on your operating system, the steps you need to take may vary slightly, but the general principles are the same (see TeX Live Page).

First, download and install a TeX distribution package or software program. This will provide all of the components you'll need in order to start using LaTeX.

If you do not have LaTeX installed, you will receive an error message:

 RuntimeError: Failed to process string with tex because latex could not be found

Note: Please restart your terminal after installing LateX in order to update the path.

Adding math equation in title

import matplotlib.pyplot as plt
import matplotlib


matplotlib.rcParams['text.usetex'] = True

ax = plt.subplot(111)

plt.xlim(0,10)
plt.ylim(0,10)

plt.title(r"$\sum_{k=0}^{\infty}k$",fontsize=18)

plt.grid()

plt.savefig("matplotlib_math_equation_03.png", bbox_inches='tight',dpi=100, facecolor='white')

plt.show()

How to insert math equations in a matplotlib figure ?
How to insert math equations in a matplotlib figure ?

References

Links Site
Writing mathematical expressions matplotlib.org
Text rendering with LaTeX matplotlib.org
matplotlib.pyplot.annotate matplotlib.org
Image

of