Skip to content Skip to sidebar Skip to footer

Why Do I Get An Invalid Rgba Argument Valueerror When Defining "capsize" To My Error Bars?

I am trying to plot horizontal bar plots with their corresponding error bars, however, when I define capsize I get a ValueError: Invalid RGBA argument. I am running the script in

Solution 1:

It is currently not possible to create multicolor caps through the bar API. (#14480)

A hack would be to use the following function

def colorize_errorbars(cont, colors, ax=None):
    ax = ax or plt.gca()
    scs = []
    for line in cont.errorbar.lines[1]:
        line.remove()
        sc = ax.scatter(*line.get_data(), c=colors, marker=line.get_marker(),
                               s=line.get_markersize()**2, zorder=3)
        scs.append(sc)
    cont.errorbar.lines = (cont.errorbar.lines[0], tuple(scs), cont.errorbar.lines[2])
    for col in cont.errorbar.lines[2]:
        col.set_color(colors)

e.g., like

fig, ax = plt.subplots( figsize=(3,3))

colors=['crimson', 'limegreen', 'indigo']

bars = ax.bar( [0,1,2], 
        [5,3,4],
        linewidth=2,
        color="papayawhip",
        edgecolor=colors,
        capsize=3,
        yerr=[1.5, 1, 1.5], 
        error_kw = { 'elinewidth': 2, }
        )

colorize_errorbars(bars, colors, ax)    

plt.show()

enter image description here

Post a Comment for "Why Do I Get An Invalid Rgba Argument Valueerror When Defining "capsize" To My Error Bars?"