我想在以下位置标记我的 x 轴:
pylab.xlabel('metres 10^1')
但我不想包含 ^ 符号。
pylab.xlabel('metres 10$^{one}$')
此方法有效,并且将上标字母,但似乎不适用于数字。 如果我尝试:
pylab.xlabel('metres 10$^1$')
它出于某种原因将字母 N 上标。
有人知道如何在 python 图中为数字上标吗? 谢谢。
最佳答案
您只需要在 $
中包含完整的表达式.基本上,你需要 "meters $10^1$"
.你不需要usetex=True
这样做(或大多数数学公式)。
您可能还想使用原始字符串(例如 r"\t"
和 "\t"
)以避免出现 \n
等问题, \a
, \b
, \t
, \f
等。
例如:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set(title=r'This is an expression $e^{\sin(\omega\phi)}$',
xlabel='meters $10^1$', ylabel=r'Hertz $(\frac{1}{s})$')
plt.show()
如果您不希望上标文本与其余文本的字体不同,请使用 \mathregular
(或等效 \mathdefault
)。一些符号将不可用,但大多数会。这对于像您这样的简单上标特别有用,您希望表达式与文本的其余部分融为一体。
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set(title=r'This is an expression $\mathregular{e^{\sin(\omega\phi)}}$',
xlabel='meters $\mathregular{10^1}$',
ylabel=r'Hertz $\mathregular{(\frac{1}{s})}$')
plt.show()
有关更多信息(以及 matplotlib 的“数学文本”的一般概述),请参阅:http://matplotlib.org/users/mathtext.html
关于python - Python图中的上标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21226868/