【问题标题】:How would I set the y axis to be in millions?我如何将 y 轴设置为数百万?
【发布时间】:2020-04-14 23:06:25
【问题描述】:

您好,我想知道您如何将图形的 y 轴设置为百万,因此它不会显示 5e7,而是会在同一位置显示 50。谢谢

【问题讨论】:

    标签: matplotlib plot axis ticker


    【解决方案1】:

    您可以使用tick formatters 显示以百万为单位的数字,如下所示

    import numpy as np
    import matplotlib.ticker as ticker
    
    @ticker.FuncFormatter
    def million_formatter(x, pos):
        return "%.1f M" % (x/1E6)
    
    
    x = np.arange(1E7,5E7,0.5E7)
    y = x
    fig, ax = plt.subplots()
    
    ax.plot(x,y)
    
    ax.xaxis.set_major_formatter(million_formatter)
    ax.yaxis.set_major_formatter(million_formatter)
    
    ax.set_xlabel('X in millions')
    ax.set_ylabel('Y in millions')
    
    plt.xticks(rotation='45')
    
    plt.show
    

    导致

    【讨论】:

    • 嗨,我想知道你如何摆脱数字末尾的 .0
    • 用“%f M”代替“%.1f M”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多