我正在使用下面的代码生成具有多个跟踪的图表。 然而,我所知道的为每条轨迹应用不同颜色的唯一方法是使用randon函数,该函数为颜色设置numerico RGB
但随机颜色不利于演示文稿
我如何使用托盘颜色作为下面的代码,而不获得更多的随机颜色
groups53 = dfagingmedioporarea.groupby(by='Area')
data53 = []
colors53=get_colors(50)
for group53, dataframe53 in groups53:
dataframe53 = dataframe53.sort_values(by=['Aging_days'], ascending=False)
trace53 = go.Bar(x=dataframe53.Area.tolist(),
y=dataframe53.Aging_days.tolist(),
marker = dict(color=colors53[len(data53)]),
name=group53,
text=dataframe53.Aging_days.tolist(),
textposition='auto',
)
data53.append(trace53)
layout53 = go.Layout(xaxis={'title': 'Area', 'categoryorder': 'total descending', 'showgrid': False},
yaxis={'title': 'Dias', 'showgrid': False},
margin={'l': 40, 'b': 40, 't': 50, 'r': 50},
hovermode='closest',
template='plotly_white',
title={
'text': "Aging Médio (Dias)",
'y':.9,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'})
figure53 = go.Figure(data=data53, layout=layout53)
许多关于色彩主题的问题已经被提出和回答。 参见示例Plotly: How to define colors in a figure using plotly.graph_objects and plotly.express? 但您似乎明确希望添加跟踪而不使用循环。 也许是因为trace的属性不仅在颜色上不同?据我所知,目前还没有关于如何有效地做到这一点的描述
答案是:
dir(px.colors.qualitative)
下查找许多可用的选项板,或['black', 'grey', 'red', 'blue']
,然后next(palette)
逐个检索您决定添加到图形中的每个轨迹而且
next(palette)
一开始可能看起来有点神秘,但使用pythonitertools
可以很容易地设置它,如下所示:现在,您可以使用
next(palette)
并在每次添加跟踪时返回颜色列表的下一个元素。最棒的是,正如上面的代码所建议的,颜色是循环返回的,所以你永远不会到达列表的末尾,而是从一开始就开始,当你使用了所有的颜色一次示例图:
完整代码:
相关问题 更多 >
编程相关推荐