plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

Colorful error-bars in px.bar() plots

Open papomail opened this issue 6 years ago • 6 comments

Error-bars (error_y, error_y) in px.bar() plots are drawn in black regardless of the facecolor. It'd be great to have the option to specify the color; e.g:

fig=px.bar(df, x='x_vals', y='y_vals', 
                  color='color_vals', 
                  error_y='error_vals',
                  **error_y_color='color_vals'**
)

Thank you!!

papomail avatar Aug 04 '20 18:08 papomail

Would you be interested in changing the color of the error bars individually or all at once? The latter case can be addressed with an update_traces() call:

import plotly.express as px

fig = px.bar(dict(a=[1,2], b=[4,5]), barmode="group", error_y=[0.5,0.5])
fig.update_traces(error_y_color="yellow")
fig.show()

nicolaskruchten avatar Aug 10 '20 14:08 nicolaskruchten

I wasn't aware that you could change the 'error_y_color'. Thank you!

Ideally the error bar would match the color of the bar; as in the box-plots. Is there a good way of getting something like this? color_errorbars

Thanks for your time!

Would you be interested in changing the color of the error bars individually or all at once? The latter case can be addressed with an update_traces() call:

import plotly.express as px

fig = px.bar(dict(a=[1,2], b=[4,5]), barmode="group", error_y=[0.5,0.5])
fig.update_traces(error_y_color="yellow")
fig.show()

papomail avatar Aug 10 '20 15:08 papomail

Yep, you can do that with a slightly more complicated variant:

import plotly.express as px

fig = px.bar(dict(a=[1,2], b=[4,5]), barmode="group", error_y=[0.5,0.5])
fig.for_each_trace(lambda t: t.update(error_y_color=t.marker.color))
fig.show()

nicolaskruchten avatar Aug 10 '20 16:08 nicolaskruchten

Awesome! That's exactly what I was looking for.
Thank you so much for taking the time and showcasing the 'for_each_trace' method. Have a great day 👍

papomail avatar Aug 10 '20 16:08 papomail

Hi @nicolaskruchten is there a way to do this same thing with px.scatter? I just tried and got an error back (code below) here is what the code looks like. I'm using a continuous color scale for the markers (based on numeric variable) and would like the error bars to match instead for being black.

fig = px.scatter(**graph_dict) fig.for_each_trace(lambda t: t.update(error_x_color=t.marker.color, error_y_color=t.marker.color))

Error message: ValueError: Invalid value of type 'numpy.ndarray' received for the 'color' property of scatter.error_x Received value: array([2. , 2. , 2. , 2.0015 , 1.9991 , 2.0056 , 2.001981]) The 'color' property is a color and may be specified as: - A hex string (e.g. '#ff0000') - An rgb/rgba string (e.g. 'rgb(255,0,0)') - An hsl/hsla string (e.g. 'hsl(0,100%,50%)') - An hsv/hsva string (e.g. 'hsv(0,100%,100%)') - A named CSS color: aliceblue, antiquewhite, aqua, aquamarine, azure, beige, bisque, black, blanchedalmond, blue, blueviolet, brown, burlywood, cadetblue, chartreuse, chocolate, coral, cornflowerblue, cornsilk, crimson, cyan, darkblue, darkcyan, darkgoldenrod, darkgray, darkgrey, darkgreen, darkkhaki, darkmagenta, darkolivegreen, darkorange, darkorchid, darkred, darksalmon, darkseagreen, darkslateblue, darkslategray, darkslategrey, darkturquoise, darkviolet, deeppink, deepskyblue, dimgray, dimgrey, dodgerblue, firebrick, floralwhite, forestgreen, fuchsia, gainsboro, ghostwhite, gold, goldenrod, gray, grey, green, greenyellow, honeydew, hotpink, indianred, indigo, ivory, khaki, lavender, lavenderblush, lawngreen, lemonchiffon, lightblue, lightcoral, lightcyan, lightgoldenrodyellow, lightgray, lightgrey, lightgreen, lightpink, lightsalmon, lightseagreen, lightskyblue, lightslategray, lightslategrey, lightsteelblue, lightyellow, lime, limegreen, linen, magenta, maroon, mediumaquamarine, mediumblue, mediumorchid, mediumpurple, mediumseagreen, mediumslateblue, mediumspringgreen, mediumturquoise, mediumvioletred, midnightblue, mintcream, mistyrose, moccasin, navajowhite, navy, oldlace, olive, olivedrab, orange, orangered, orchid, palegoldenrod, palegreen, paleturquoise, palevioletred, papayawhip, peachpuff, peru, pink, plum, powderblue, purple, red, rosybrown, royalblue, rebeccapurple, saddlebrown, salmon, sandybrown, seagreen, seashell, sienna, silver, skyblue, slateblue, slategray, slategrey, snow, springgreen, steelblue, tan, teal, thistle, tomato, turquoise, violet, wheat, white, whitesmoke, yellow, yellowgreen

tlicquia avatar Mar 31 '22 15:03 tlicquia

@tlicquia I am facing the same issue. Were you able to find a solution for this?

hammad45 avatar Jul 01 '22 17:07 hammad45