Python 图中的上标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21226868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Superscript in Python plots
提问by Kantura
I want to label my x axis at follows :
我想在以下位置标记我的 x 轴:
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 .
出于某种原因,它上标了一个字母 N。
Anyone know how to superscript numbers in python plots ? thanks .
有人知道如何在 python 图中给数字上标吗?谢谢 。
采纳答案by Joe Kington
You just need to have the full expression inside the $. Basically, you need "meters $10^1$". You don't need usetex=Trueto do this (or most any mathematical formula).
您只需要在$. 基本上,您需要"meters $10^1$". 您不需usetex=True要这样做(或大多数数学公式)。
You may also want to use a raw string (e.g. r"\t", vs "\t") to avoid problems with things like \n, \a, \b, \t, \f, etc.
您可能还需要使用原始的字符串(例如r"\t",VS "\t"),以避免事情喜欢的问题\n,\a,\b,\t,\f,等。
For example:
例如:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set(title=r'This is an expression $e^{\sin(\omega\phi)}$',
xlabel='meters ^1$', ylabel=r'Hertz $(\frac{1}{s})$')
plt.show()


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


For more information (and a general overview of matplotlib's "mathtext"), see: http://matplotlib.org/users/mathtext.html
有关更多信息(以及 matplotlib 的“mathtext”的一般概述),请参阅:http://matplotlib.org/users/mathtext.html
回答by Engr M Faysal
If you want to write unit per meter (m^-1), use $m^{-1}$), which means -1inbetween {}
如果要写 unit per meter (m^-1),请使用$m^{-1}$),这意味着-1介于两者之间{}
Example:
plt.ylabel("Specific Storage Values ($m^{-1}$)", fontsize = 12 )
例子:
plt.ylabel("Specific Storage Values ($m^{-1}$)", fontsize = 12 )
回答by xyzzyqed
Alternatively, in python 3.6+, you can generate Unicode superscriptand copy paste that in your code:
或者,在 python 3.6+ 中,您可以生成 Unicode 上标并将其复制粘贴到您的代码中:
ax1.set_ylabel('Rate (min?1)')

