代码之家  ›  专栏  ›  技术社区  ›  jason

Matplotlib:将Y轴更改为对数刻度后,将Y轴设置为非科学符号

  •  0
  • jason  · 技术社区  · 4 年前

    我有一个简单的对象图 axs 对于轴。在我将Y轴更改为对数刻度之前,格式只是常规数字。

    使用以下方法将Y轴更改为对数刻度后: axs.set_yscale('log') …然后尝试将数字改回

    axs.set_yticklabels(['{:,}'.format(int(x)) for x in axs.get_yticks().tolist()])

    它不起作用……标签仍然保留在科学符号中。

    我只想要回正常的号码。

    我正在使用:

    fig = plt.figure(figsize=(30, 15))
    axs = fig.add_subplot(1, 1, 1)
    axs.plot()
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   JohanC    4 年前

    如所解释的, here 你可以设置一个 ScalarFormatter 省略科学符号。 .set_scientific(False) 还需要压制大数字的科学符号。

    你可能需要 axs.yaxis.set_major_formatter(ticker.FuncFormatter(lambda y, _: '{:g}'.format(y))) 如果你正在处理负面权力。

    from matplotlib import pyplot as plt
    from matplotlib.ticker import ScalarFormatter
    
    fig = plt.figure(figsize=(30, 15))
    axs = fig.add_subplot(1, 1, 1)
    axs.plot()
    axs.set_ylim(100000, 100000000)
    axs.set_yscale('log')
    formatter = ScalarFormatter()
    formatter.set_scientific(False)
    axs.yaxis.set_major_formatter(formatter)
    plt.show()
    

    resulting plot