代码之家  ›  专栏  ›  技术社区  ›  Medulla Oblongata

默认使用Python图的科学记数法

  •  1
  • Medulla Oblongata  · 技术社区  · 8 年前

    简单的问题:如何在默认情况下让Python在其图中使用科学符号?从各种各样的文章中我可以写一些

    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轴的空白图形。)

    1 回复  |  直到 8 年前
        1
  •  1
  •   Diziet Asahi    8 年前

    可以通过编辑“rc”文件来修改matplotlib的默认行为。见 Customizing 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