pythonmatplotlibcolorserrorbar

Assigning a color to each dot of a dataset using plt.errorbar


I want to plot a dataset with errobars, with each dot in a different color.

I tried the following (which works if i use plt.scatter)

colors = np.array(['blue','red','orange','orange','green','green'])

plt.figure()
plt.errorbar(position,cd,yerr=error,c=colors)
plt.show()

But this code gives me the error : "ValueError: array(['blue', 'red', 'orange', 'orange', 'green', 'green'], dtype='<U6') is not a valid value for color"

Does anyone have an idea on how to solve this issue?


Solution

  • If you want to only give different-different color to the errorbar lines then instead of using the color(c) argument use the ecolor argument

    ecolorcolor, default: None

    The color of the errorbar lines. If None, use the color of the line connecting the markers.

    colors = np.array(['blue', 'red', 'orange', 'orange', 'green', 'green'])
    plt.figure()
    plt.errorbar(position, cd, yerr=error, ecolor=colors)
    plt.show()
    
    

    [![enter image description here][2]][2]


    IF you want to also change the color of point and the errorlines, then you can use this code

    
    plt.figure()
    for i in range(len(position)):
        plt.errorbar(position[i], cd[i], yerr=error[i], ecolor=colors[i], fmt="o", capsize=5)
    plt.show()
    

    enter image description here