Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Superscript in Python plots
Superscript notation is essential for displaying scientific formulas and units in Python plots. Matplotlib supports LaTeX-style mathematical notation using the $\mathregular{}$ syntax to create superscripts and subscripts in titles, axis labels, and legends.
Basic Superscript Syntax
Use $\mathregular{text^{superscript}}$ format where the caret ^ indicates superscript and curly braces {} contain the superscript text ?
import matplotlib.pyplot as plt
# Simple superscript example
plt.figure(figsize=(6, 4))
plt.text(0.5, 0.5, r'$\mathregular{x^2}$', fontsize=20, ha='center')
plt.text(0.5, 0.3, r'$\mathregular{E=mc^2}$', fontsize=16, ha='center')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.title('Basic Superscript Examples')
plt.show()
Physics Formula Plot with Superscripts
Let's create a force vs acceleration plot with proper scientific notation in labels ?
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [8, 5]
plt.rcParams["figure.autolayout"] = True
# Create data points
acceleration = np.linspace(1, 10, 100)
mass = 20
force = mass * acceleration
# Plot with superscript labels
plt.plot(acceleration, force, color="red", linewidth=3, label="F = ma")
plt.title(r"Force vs Acceleration $\mathregular{(kgms^{-2})}$", fontsize=14)
plt.xlabel(r"Acceleration $\mathregular{(ms^{-2})}$", fontsize=12)
plt.ylabel(r"Force $\mathregular{(kgms^{-2})}$", fontsize=12)
plt.grid(True, alpha=0.3)
plt.legend(fontsize=11)
plt.show()
Multiple Superscript Examples
Here are common scientific notations with superscripts ?
import matplotlib.pyplot as plt
import numpy as np
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# Left plot - Area calculation
x = np.linspace(0, 5, 100)
y = x**2
ax1.plot(x, y, 'b-', linewidth=2)
ax1.set_title(r'Area = $\mathregular{x^2}$')
ax1.set_xlabel(r'Length $\mathregular{(m)}$')
ax1.set_ylabel(r'Area $\mathregular{(m^2)}$')
ax1.grid(True, alpha=0.3)
# Right plot - Volume calculation
y2 = x**3
ax2.plot(x, y2, 'g-', linewidth=2)
ax2.set_title(r'Volume = $\mathregular{x^3}$')
ax2.set_xlabel(r'Length $\mathregular{(m)}$')
ax2.set_ylabel(r'Volume $\mathregular{(m^3)}$')
ax2.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Advanced Superscript Formatting
You can combine multiple superscripts and use negative exponents ?
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
x = np.linspace(0.1, 5, 100)
y1 = 1/x**2 # Inverse square law
y2 = np.exp(-x) # Exponential decay
plt.figure(figsize=(10, 6))
plt.plot(x, y1, 'r-', label=r'$\mathregular{y = x^{-2}}$', linewidth=2)
plt.plot(x, y2, 'b-', label=r'$\mathregular{y = e^{-x}}$', linewidth=2)
plt.title(r'Functions with Negative Exponents', fontsize=16)
plt.xlabel(r'x $\mathregular{(units)}$', fontsize=12)
plt.ylabel(r'y $\mathregular{(units^{-1})}$', fontsize=12)
plt.legend(fontsize=12)
plt.grid(True, alpha=0.3)
plt.show()
Key Formatting Rules
Use
r'$\mathregular{}'for raw strings to avoid escape sequence issuesEnclose superscripts in curly braces:
^{-2}instead of^-2Use
\mathregular{}for normal text style instead of italicsCombine with regular text:
r'Force $\mathregular{(kgms^{-2})}$'
Conclusion
Matplotlib's $\mathregular{}$ syntax enables professional scientific notation in plots. Use ^{} for superscripts and raw strings to ensure proper rendering of mathematical expressions in titles, labels, and legends.
