如何在轴上显示 x10(上标数字)而不是 1e(数字)?

问题描述 投票:0回答:2

我知道如何在 matplotlib 中使用科学记数法表示轴末尾的唯一方法是使用

plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))

但这将使用 1e 而不是 x10。在下面的示例代码中,它显示 1e6,但我想要 x10 的 6 次方,x10superscript6(x10^6,其中 6 小且没有 ^)。有办法做到这一点吗?

编辑:我不想在轴上的每个刻度上使用科学计数法(恕我直言,这看起来不太好),只在最后,如示例所示,但仅将 1e6 部分更改为 x10superscript6。

我还不能包含图像。

谢谢

import numpy as np
import matplotlib.pyplot as plt
plt.figure()
x = np.linspace(0,1000)
y = x**2
plt.plot(x,y)
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.show()
python python-3.x matplotlib scientific-notation
2个回答
7
投票

偏移量的格式不同,具体取决于

useMathText
参数。如果
True
它将以类似乳胶(MathText)格式的偏移量显示为
x 10^6
而不是
1e6

import numpy as np
import matplotlib.pyplot as plt
plt.figure()
x = np.linspace(0,1000)
y = x**2
plt.plot(x,y)
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0), useMathText=True)
plt.show()

请注意,上述内容不适用于 2.0.2 版本(可能还有其他旧版本)。在这种情况下,您需要手动设置格式化程序并指定选项:

import numpy as np
import matplotlib.pyplot as plt
plt.figure()
x = np.linspace(0,1000)
y = x**2
plt.plot(x,y)
plt.gca().yaxis.set_major_formatter(plt.ScalarFormatter(useMathText=True))
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.show()

1
投票

如果您的所有绘图都需要这个,您可以编辑

rcParams
,例如如下。

import matplotlib as mpl

mpl.rc('axes.formatter', use_mathtext=True)

放在脚本的顶部。如果您想要它用于所有脚本我建议查找 matplotlib 样式表。

© www.soinside.com 2019 - 2024. All rights reserved.