我有以下名为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 |
+----+----------+-----------+---------+我可以做一个条形图:
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()但是,我希望根据蓝色的值来更改颜色(如果为真,则为“Go_NoGo”;如果为假,则为"red“)。
我研究了在set_color所在位置添加marker=dict(color(list(map(set_color,y))))的各种方法:
def set_color(value):
if value:
return "blue"
else:
return "red"我想不出如何在第三列中传递来设置标记颜色。
我已经尝试了各种方法,但可能我的搜索功能还不够。任何帮助都将不胜感激。
发布于 2021-04-10 22:55:45
你可以使用marker_color代替marker。下面是一个例子:
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()所以预期的输出是底特律和弗雷斯诺是蓝色的,其余的是红色的。

https://stackoverflow.com/questions/66603518
复制相似问题