Adjust exponent text after setting scientific limits on matplotlib axis

后端 未结 5 983
旧时难觅i
旧时难觅i 2020-12-05 16:13

At the moment if I set matplotlib y axis ticklabels to scientific mode it gives me an exponent at the top of the y axis of the form 1e-5

I\'d like to ad

5条回答
  •  -上瘾入骨i
    2020-12-05 17:05

    It seems that plt.ticklabel_format does not work correctly. However if you define the ScalarFormatter yourself and set the limits for scientific notation to the formatter, you can get the offset automatically in the mathtext format like so:

    import matplotlib.pyplot as plt
    import numpy as np
    import matplotlib.ticker
    
    x = np.linspace(3,5)
    y = np.sin(np.linspace(0,6*np.pi))*1e5
    
    plt.plot(x,y)
    
    mf = matplotlib.ticker.ScalarFormatter(useMathText=True)
    mf.set_powerlimits((-2,2))
    plt.gca().yaxis.set_major_formatter(mf)
    
    plt.show()
    

提交回复
热议问题