Kan*_*ura 27 python matplotlib
我想在下面标记我的x轴:
pylab.xlabel('metres 10^1')
Run Code Online (Sandbox Code Playgroud)
但我不想包含^符号.
pylab.xlabel('metres 10$^{one}$')
Run Code Online (Sandbox Code Playgroud)
这种方法有效并且会上标字母,但似乎不适用于数字.如果我尝试:
pylab.xlabel('metres 10$^1$')
Run Code Online (Sandbox Code Playgroud)
由于某种原因,它上标字母N.
任何人都知道如何在python图中标注数字?谢谢 .
Joe*_*ton 50
你只需要在里面有完整的表达$.基本上,你需要"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()
Run Code Online (Sandbox Code Playgroud)

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

有关更多信息(以及matplotlib的"mathtext"的一般概述),请参阅:http://matplotlib.org/users/mathtext.html
或者,在 python 3.6+ 中,您可以生成 Unicode 上标并将其复制粘贴到您的代码中:
ax1.set_ylabel('Rate (min?¹)')
Run Code Online (Sandbox Code Playgroud)