Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 views
in Technique[技术] by (71.8m points)

matplotlib - Superscript in Python plots

I want to label my x axis at follows :

pylab.xlabel('metres 10^1')

But I don't want to have the ^ symbol included .

pylab.xlabel('metres 10$^{one}$')

This method works and will superscript letters but doesn't seem to work for numbers . If I try :

pylab.xlabel('metres 10$^1$')

It superscripts a letter N for some reason .

Anyone know how to superscript numbers in python plots ? thanks .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

You may also want to use a raw string (e.g. r"", vs "") to avoid problems with things like , a, , , f, etc.

For example:

import matplotlib.pyplot as plt

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

enter image description here

If you don't want the superscripted text to be in a different font than the rest of the text, use mathregular (or equivalently mathdefault). Some symbols won't 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=r'This is an expression $mathregular{e^{sin(omegaphi)}}$',
       xlabel='meters $mathregular{10^1}$',
       ylabel=r'Hertz $mathregular{(frac{1}{s})}$')
plt.show()

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...