Python 图中的上标 [英] Superscript in Python plots

查看:22
本文介绍了Python 图中的上标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  pylab.xlabel('metres 10 ^ 1')

但是我不想包含^符号.

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

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

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

由于某种原因,它会在字母N上标上一个字母.

有人知道如何在 python 图中给数字上标吗?谢谢.

解决方案

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

您可能还想使用原始字符串(例如 r"\t", vs "\t")来避免诸如 \n\a\b\t\f 等>

例如:

 将matplotlib.pyplot导入为plt无花果,ax = plt.subplots()ax.set(title=r'这是一个表达式 $e^{\sin(\omega\phi)}$',xlabel='meters $10^1$', ylabel=r'Hertz $(\frac{1}{s})$')plt.show()

如果您不希望上标文本的字体与文本的其余部分不同,请使用 \mathregular(或等效的 \mathdefault).某些符号将不可用,但大多数将可用.这对于像您这样的简单上标尤其有用,您希望在其中将表达式与文本的其余部分融合在一起.

 将matplotlib.pyplot导入为plt无花果,ax = plt.subplots()ax.set(title = r'这是一个表达式$ \ mathregular {e ^ {\ sin(\ omega \ phi)}} $',xlabel='meters $\mathregular{10^1}$',ylabel=r'Hertz $\mathregular{(\frac{1}{s})}$')plt.show()

有关更多信息(以及matplotlib的"mathtext"的概述),请参见: http://matplotlib.org/users/mathtext.html

I want to label my x axis at follows :

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 .

Anyone know how to superscript numbers in python plots ? thanks .

解决方案

You just need to have the full expression inside the $. Basically, you need "meters $10^1$". You don't need usetex=True to do this (or most any mathematical formula).

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.

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 $10^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.

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

这篇关于Python 图中的上标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆