python - Python图中的上标

标签 python matplotlib

我想在以下位置标记我的 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()

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 的“数学文本”的一般概述),请参阅:http://matplotlib.org/users/mathtext.html

关于python - Python图中的上标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21226868/

相关文章:

python - Matplotlib 多边形补丁不使用 pandas df 中的 xy 列表进行绘制

python - 如何舍入 matplotlib 饼图中显示的值?

python - 我可以将串行对象转换为文件描述符并将其传递给 C 程序吗?

python - 从 python 脚本自动将命令行输入到运行的 python shell 脚本中

python - 控制散点图中的颜色

python - 如何将 x 轴标签添加到 seaborn 图形级图中的每个图

python - 如何用python将方形图像像素化为256个大像素?

python - MPI4PY:不同数量的节点返回不同的结果

python - 匹配所有的正则表达式模式,并在找到特定单词时返回 null

python - 读取 xml 并尝试将其提取到 2 个不同的 xml 中