Python剧情中的上标

时间:2014-01-20 05:33:14

标签: python matplotlib

我想在下面标记我的x轴:

pylab.xlabel('metres 10^1')

但我不想包含^符号。

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

此方法有效并且将上标字母,但似乎不适用于数字。 如果我尝试:

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

由于某种原因,它上标字母N。

任何人都知道如何在python图中标注数字? 谢谢。

3 个答案:

答案 0 :(得分:42)

您只需要在$中包含完整的表达式。基本上,您需要"meters $10^1$"。您不需要usetex=True来执行此操作(或大多数任何数学公式)。

您可能还想使用原始字符串(例如r"\t"和vs "\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()

enter image description here

如果您不希望上标文本使用与文本其余部分不同的字体,请使用\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()

enter image description here

有关更多信息(以及matplotlib“mathtext”的一般概述),请参阅:http://matplotlib.org/users/mathtext.html

答案 1 :(得分:3)

或者,在python 3.6+中,您可以generate Unicode superscript并将其复制并粘贴到您的代码中:

ax1.set_ylabel('Rate (min⁻¹)')

答案 2 :(得分:0)

如果要写入单元per meter (m^-1),请使用$m^{-1}$),这表示-1之间的{}

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