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

用python绘制Matplotlib非对称errorbar

  •  0
  • Theory94  · 技术社区  · 5 年前

    尝试绘制从负值到正值的非对称条形图时出错。我修改了文档中的示例:

    import numpy as np
    import matplotlib.pyplot as plt
    
    # example data
    x = np.arange(0, 4, 1)
    y = -0.2* x
    
    
    # example error bar values that vary with x-position
    error = 0.1 + 0.2 * x
    
    # error bar values w/ different -/+ errors that
    # also vary with the x-position
    lower_error = -1 * error
    upper_error = 4* error
    asymmetric_error = [lower_error, upper_error]
    
    plt.errorbar(x, y, yerr=asymmetric_error, fmt='.', ecolor = 'red')
    plt.show()
    

    enter image description here

    但不对称误差的值如下:

    array([-0.1, -0.3, -0.5, -0.7]), array([0.4, 1.2, 2. , 2.8])]

    0 回复  |  直到 5 年前
        1
  •  1
  •   Scott Boston    5 年前

    你不需要在下误差前面加负数。

    import numpy as np
    import matplotlib.pyplot as plt
    
    # example data
    x = np.arange(0, 4, 1)
    y = -0.2* x
    
    
    # example error bar values that vary with x-position
    error = 0.1 + 0.2 * x
    
    # error bar values w/ different -/+ errors that
    # also vary with the x-position
    lower_error =  error
    upper_error =  4*error
    asymmetric_error = np.array(list(zip(lower_error, upper_error))).T
    
    plt.errorbar(x, y, yerr=asymmetric_error, fmt='.', ecolor = 'red')
    plt.show()
    

    输出:

    enter image description here