5 python scientific-notation matplotlib python-3.x
我知道如何在 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()
Run Code Online (Sandbox Code Playgroud)
根据参数的不同,偏移量的格式也不同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()
Run Code Online (Sandbox Code Playgroud)
请注意,上述内容不适用于 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()
Run Code Online (Sandbox Code Playgroud)