matplotlib – Superscript in Python plots

matplotlib – Superscript in Python plots

You just need to have the full expression inside the $. Basically, you need meters $10^1$. You dont need usetex=True to do this (or most any mathematical formula).

You may also want to use a raw string (e.g. rt, vs t) to avoid problems with things like n, a, b, t, f, etc.

For example:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.set(title=rThis is an expression $e^{sin(omegaphi)}$,
       xlabel=meters $10^1$, ylabel=rHertz $(frac{1}{s})$)
plt.show()

enter

If you dont want the superscripted text to be in a different font than the rest of the text, use mathregular (or equivalently mathdefault). Some symbols wont be available, but most will. This is especially useful for simple superscripts like yours, where you want the expression to blend in with the rest of the text.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.set(title=rThis is an expression $mathregular{e^{sin(omegaphi)}}$,
       xlabel=meters $mathregular{10^1}$,
       ylabel=rHertz $mathregular{(frac{1}{s})}$)
plt.show()

enter

For more information (and a general overview of matplotlibs mathtext), see: http://matplotlib.org/users/mathtext.html

Alternatively, in python 3.6+, you can generate Unicode superscript and copy paste that in your code:

ax1.set_ylabel(Rate (min⁻¹))

matplotlib – Superscript in Python plots

If you want to write unit per meter (m^-1), use $m^{-1}$), which means -1 inbetween {}

Example:
plt.ylabel(Specific Storage Values ($m^{-1}$), fontsize = 12 )

Leave a Reply

Your email address will not be published. Required fields are marked *