首页 > 解决方案 > 默认情况下对 Python 绘图使用科学记数法

问题描述

简单的问题:如何让 Python 默认在其绘图中使用科学记数法?从 SO 上的各种帖子中,我可以写一些类似的东西

from numpy import linspace
import matplotlib.pyplot as plt

plt.figure()
plt.plot(linspace(1e6,2e6),linspace(1e6,1e7))

plt.figure()
plt.plot(linspace(8e6,9e6),linspace(2e6,2.5e7))

plt.ticklabel_format(style='sci', axis='both', scilimits=(-2,2))
plt.show()

ticklabel_format仅作用于 matplotlib 生成的最后一个图。(如果plt.ticklabel_format()放在代码的开头,我还会得到一个显示 x、y 轴的空白图。)

标签: pythonmatplotlib

解决方案


您可以通过编辑“rc”文件来修改 matplotlib 的默认行为。请参阅自定义 matplotlib

在您的情况下,您似乎可以调整项目:

axes.formatter.limits : -2, 2 # use scientific notation if log10
                              # of the axis range is smaller than the
                              # first or larger than the second

推荐阅读