Reputation: 23
I've got the following dataframe called data
:
+----+----------+-----------+---------+
| | COUNT | City | Go_NoGo |
|----+----------+-----------+---------|
| 1 | 1 | Maimi | False |
| 3 | 570 | Chicago | False |
| 0 | 406 | Denver | False |
| 10 | 1220 | New York | False |
| 2 | 1557 | Boston | False |
| 7 | 90 | Seattle | False |
| 9 | 3 | Provo | False |
| 11 | 323 | Bismark | False |
| 4 | 1 | St. Louis | False |
| 6 | 562 | Detroit | True |
| 5 | 8391 | Fresno | True |
+----+----------+-----------+---------+
I can make a bar chart just fine:
fig = go.Figure()
fig.update_layout(width = 800, height = 400, template = 'plotly_white',xaxis_title = x_title, yaxis_title = y_title)
fig.add_trace(go.Bar(x = data['City'].tolist(),
y = data['COUNT'].tolist()))
fig.show()
However, I'd like to change the color based on the Go_NoGo
value ("blue" if true, "red" if false).
I've looked at various ways of adding marker=dict(color(list(map(set_color,y))))
where set_color
is :
def set_color(value):
if value:
return "blue"
else:
return "red"
I can't figure out how to pass in the 3rd column to set the marker color.
I've already tried various options and it's possible my search-fu isn't sufficient. Any help would be appreciated.
Upvotes: 1
Views: 1346
Reputation: 1280
You can use marker_color instead marker. Here's an example:
import pandas as pd
import plotly_graph_objects as go
def Bar_Color(i):
if (i == False):
return "red"
elif (i == True):
return "blue"
fig = go.Figure()
fig.update_layout(width = 800, height = 400)
fig.add_trace(go.Bar(x = data['City'].tolist(),
y = data['COUNT'].tolist(),
marker_color=list(map(Bar_Color, data['Go_NoGo']))
)
)
fig.show()
So expected output is Detroit and Fresno are blue and the rest are red.
Upvotes: 3