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

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

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

编辑:我不希望轴中每个刻度的科学记数法(imho 看起来不太好),只在最后,如示例所示,但仅将 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()

最佳答案

偏移量的格式取决于 useMathText 参数。如果 True 它将以类似 latex (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()

python - 如何在 matplotlib 中为轴显示 x10(上标数字)而不是 1e(数字)?-LMLPHP

请注意,上述内容不适用于 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()

关于python - 如何在 matplotlib 中为轴显示 x10(上标数字)而不是 1e(数字)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54354823/

10-13 02:20