【问题标题】:PyPlot - Positive values on Y-axis in both directionsPyPlot - Y 轴上两个方向的正值
【发布时间】:2022-01-22 02:12:05
【问题描述】:

我有两个数据系列,都有正值,我想将它们显示在同一个图表中,如下图所示。如果我反转第二个系列,我会得到想要的结果,但是这样 Y 轴值将是负数。有没有基本在Y轴上显示绝对值的解决方案?

提前致谢!

【问题讨论】:

    标签: matplotlib


    【解决方案1】:

    您可以设置格式化程序以显示绝对值:

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.arange(100)
    y1 = np.random.normal(0.1, 1, 100).cumsum()
    y1 -= y1.min()
    y2 = np.random.normal(0.1, 1, 100).cumsum()
    y2 -= y2.min()
    fig, ax = plt.subplots()
    ax.bar(x, y1)
    ax.bar(x, -y2)
    ax.yaxis.set_major_formatter(lambda x, pos: f'{abs(x):g}')
    ax.margins(x=0)
    plt.show()
    

    【讨论】:

    • 非常感谢!这就是我一直在寻找的。​​span>
    猜你喜欢
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 2011-01-04
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    相关资源
    最近更新 更多