Writing mathematical expressions#

Matplotlib implements a lightweight TeX expression parser and layout engine and Mathtext is the subset of Tex markup that this engine supports. Note that Matplotlib can also render all text directly using TeX if rcParams["text.usetex"] (default: False) is True; see Text rendering with LaTeX for more details. Mathtext support is available if rcParams["text.usetex"] (default: False) is False.

Any string can be processed as Mathtext by placing the string inside a pair of dollar signs '$'. Mathtext often contains many backslashes '\'; so that the backslashes do not need to be escaped, Mathtext is often written using raw strings. For example:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(3, 3), linewidth=1, edgecolor='black')
fig.text(.2, .7, "plain text: alpha > beta")
fig.text(.2, .5, "Mathtext: $\\alpha > \\beta$")
fig.text(.2, .3, r"raw string Mathtext: $\alpha > \beta$")