Menu

Understanding Using Mathematical Expressions

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

Following is the output of the above code:

![using mathematical expression matplotlib example](https://s3.ap-south-1.amazonaws.com/s3.studytonight.com/tutorials/uploads/pictures/1597141079-71449.png)

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.plot(t,s)
plt.title(r'$\alpha_i> \beta_i

Following is the output of the above code:

![using mathematical expression matplotlib example](https://s3.ap-south-1.amazonaws.com/s3.studytonight.com/tutorials/uploads/pictures/1597141079-71449.png), fontsize=20)

plt.text(0.6, 0.6, r'$\mathcal{A}\mathrm{cos}(2 \omega t)

Following is the output of the above code:

![using mathematical expression matplotlib example](https://s3.ap-south-1.amazonaws.com/s3.studytonight.com/tutorials/uploads/pictures/1597141079-71449.png), fontsize = 20)
plt.xlabel('The time (s)')
plt.ylabel('volts (mV)')
plt.show()

Following is the output of the above code:

using mathematical expression matplotlib example