New Tutorials:   TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

Mathematical expressions in matplotlib

In this tutorial, we will cover how to write mathematical expressions in matplotlib while plotting any dataset.

Writing Mathematical Expressions

The subset TeX markup can be used in any matplotlib text string just by placing it inside a pair of dollar signs ($).

  • In order to make subscripts and superscripts use the _ and ^ symbols respectively.

  • To create Fractions, binomials, and stacked numbers you can use \frac{}{}, \binom{}{} and \genfrac{}{}{}{}{}{} commands, respectively.

  • Also, the Radicals can be produced with the \sqrt[]{} command.

  • For mathematical symbols the default font is italics.

Let us cover example for more clear understanding.

Using Mathematical Expression:

In the example given below, we will represent subscript and superscript:

r'$\alpha_i> \beta_i$'

import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0.0, 2.0, 0.01)
s = np.cos(1*np.pi*t)

plt.title(r'$\alpha_i> \beta_i$', fontsize=20)

plt.text(0.6, 0.6, r'$\mathcal{A}\mathrm{cos}(2 \omega t)$', fontsize = 20)
plt.xlabel('The time (s)')
plt.ylabel('volts (mV)')

Following is the output of the above code:

using mathematical expression matplotlib example