-1

I want to color my Bar "Red" when below zero and "light green" when above zero in plotly python. enter image description here

1

2 Answers 2

1

Firstly, would be great if you drop a snippet of your code. That way we could for instance figure out whether ur using px or go, and what the df looks like.

Nevertheless, heres the fix:

import plotly.graph_objects as go
values = [10, -100, 22, 45, -57]
threshold = 0

fig = go.Figure()
fig.add_trace(go.Bar(x=[0,1,2,3,4], y=values, marker=dict(color=['green' if value > threshold else 'red' for value in values])))

marker_color can take a list in python, and list comprehension is an easy to read, and efficient method.

Sign up to request clarification or add additional context in comments.

Comments

0

One of the ways is creating bins. In your case, there would be 2 bins: +ve values and -ve values. Each bin may be assigned a color as you need.

The following example solves it:

https://community.plotly.com/t/different-colors-for-bars-in-barchart-by-their-value/6527/7

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.