我刚刚开始学习使用Dash,目前在我的网站上有一张显示用户参与度的图表。然而,
当涉及颜色时,我似乎无法弄清楚如何为Dash组件设置样式,并且Dash文档似乎与主题无关。如何从默认的蓝色/橙色/绿色更改系列的颜色?有问题的图形的代码在下面`
dcc.Graph(
id='average_engagement_graph',
figure={
'data': [
{'x': list(avg_df['day']), 'y': list(avg_df['countMessageIn']),
'type': 'bar', 'name': 'Incoming Messages from Users'},
{'x': list(avg_df['day']), 'y': list(avg_df['countMessageOut']),
'type': 'bar', 'name': 'Outgoing Message by Bot'},
],
'layout': {
'title': 'Average User-Bot Engagement by Day of Week',
'xaxis': {
'title': 'Day of the Week'
},
'yaxis': {
'title': 'Average Number of Messages'
},
}
}
),
最佳答案
对于bar
,您必须将其添加到名为marker
的键中。
dcc.Graph(
id='average_engagement_graph',
figure={
'data': [
{'x': list(avg_df['day']), 'y': list(avg_df['countMessageIn']),
'type': 'bar', 'name': 'Incoming Messages from Users'},
'marker' : { "color" : your_color_array}
{'x': list(avg_df['day']), 'y': list(avg_df['countMessageOut']),
'type': 'bar', 'name': 'Outgoing Message by Bot'},
'marker' : { "color" : your_color_array}
],
'layout': {
'title': 'Average User-Bot Engagement by Day of Week',
'xaxis': {
'title': 'Day of the Week',
},
'yaxis': {
'title': 'Average Number of Messages'
},
}
}
),
编辑
看来他们做了很多编辑,您也可以这样做...
colors = ['lightslategray',] * 5
colors[1] = 'crimson'
fig = go.Figure(data=[go.Bar(
x=['Feature A', 'Feature B', 'Feature C',
'Feature D', 'Feature E'],
y=[20, 14, 23, 25, 22],
marker_color=colors # marker color can be a single color value or an iterable
)])
关于python - 更改Dash图表系列颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57110822/