Search code examples
pythonmarkererrorbar

Python errorbar with varied marker size


I used python errorbar to make a plot, however the arguments markersize can only be a scalar not an array, which means all the data points have the same marker size. I am wondering how can I assign different markersize to different data points?

ax.errorbar(x,y,xerr=[xlow,xup],yerr=[ylow,yup],color='r',ls='None',marker='o',markersize=5.)

If I create the marker size to be 5.0*y, how can I make it work?


Solution

  • Why don't you plot error bars and markers separately?

    import matplotlib.pyplot as plt
    import numpy as np
    
    #create x and y values
    xval = np.arange(1,10)
    yval = np.square(xval)
    #define error bars and markersize
    yerrmi = np.abs(np.cos(xval) * xval)
    yerrpl = np.abs(np.sin(xval) * xval)
    yerr = np.stack([yerrpl, yerrmi])
    markerpl = 5 * yval
    
    #plot error bars
    plt.errorbar(xval, yval, yerr = yerr, ls = "None", color = "r")
    #plot scatter plot
    plt.scatter(xval, yval, s = 5 * yval, marker = "h", color = "r")
    
    plt.show()
    

    Output:

    enter image description here