Tom Xue Tom Xue - 4 months ago 8x
Python Question

What does the $\ symbol mean in Python code?

I find below code from a book to show how to use matplotlib in Python. The code runs normally. But I don't understand what does the symbol $ or $\ mean here?

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 1000)
y = np.sin(x) + 1
z = np.cos(x**2) + 1

plt.figure(figsize = (8, 4))
plt.plot(x,y,label = '$\sin x+1$', color = 'red', linewidth = 2)
plt.plot(x, z, 'b--', label = '$\cos x^2+1$')
plt.xlabel('Time(s) ')
plt.title('A Simple Example')
plt.ylim(0, 2.2)


It doesn't mean anything in Python. $ marks the beginning and end of maths in a LaTeX string.

$\sin x+1$ means

Similarly, $\cos x^2+1$ will be

For a basic tutorial in MathJax (which uses LaTeX syntax) read this.