【问题标题】:matplotlib: format axis ticks without offsetmatplotlib:格式轴刻度没有偏移
【发布时间】:2016-02-03 13:40:39
【问题描述】:

我想将我的刻度格式化为一定数量的有效数字,并删除自动偏移量。对于后者,我使用https://stackoverflow.com/a/6654046/1021819,对于前者,我将使用https://stackoverflow.com/a/25750438/1021819,即

y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
ax.yaxis.set_major_formatter(y_formatter)

ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2e'))

如何结合使用 FormatStrFormatter 和 useOffset 语法?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    FormatStrFormatter 不使用偏移量,因此通过使用第二种格式,您将自动没有偏移量。

    比较本例中的两个子图

    import matplotlib.pyplot as plt
    import matplotlib.ticker as mtick
    import numpy as np  
    
    fig,(ax1,ax2)=plt.subplots(2)
    
    ax1.plot(np.arange(10000,10010,1))
    
    ax2.plot(np.arange(10000,10010,1))
    ax2.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.4e'))
    
    plt.show()
    

    【讨论】:

    • 谢谢!扮演魔鬼的拥护者,如果我确实想要在这个计划下得到补偿怎么办?
    • 使用yaxis.set_major_formatter(mtick.FormatStrFormatter('%.4e')) 为我工作。你知道为什么plt.plot(x,y); ax=plt.gca(); ax.ticklabel_format(useOffset=False) 不会删除偏移量吗?是否需要这三行以外的内容?
    猜你喜欢
    • 2017-08-13
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 2014-11-26
    相关资源
    最近更新 更多