Displaying first decimal digit in scientific notation in Matplotlib

前端 未结 3 1609
独厮守ぢ
独厮守ぢ 2021-01-22 12:56

I am currently generating different figures with a scientific notation for the y-axis leading to ticks like 2 or 6 on some plots, but 2.5 or 8.9 on some others. I would like to

3条回答
  •  长情又很酷
    2021-01-22 13:51

    You can get the ticks and format it like you want.

    plt.plot(np.arange(1, 10), np.arange(1, 10)**5)
    ax = plt.gca()
    plt.ticklabel_format(axis='y', style='sci')
    ax.yaxis.major.formatter.set_powerlimits((0,0))
    
    xx, locs = plt.xticks()
    ll = ['%.1f' % a for a in xx]
    plt.xticks(xx, ll)
    plt.show()
    

提交回复
热议问题