Plotly:如何为使用多条轨迹创建的图形设置调色板? [英] Plotly: How to set up a color palette for a figure created with multiple traces?
问题描述
我使用下面的代码生成具有多个跟踪的图表.然而,我知道为每条轨迹应用不同颜色的唯一方法是使用 randon 函数,该函数为颜色设置数字 RGB.
但随机颜色不利于演示.
如何为下面的代码使用调色板颜色而不获得更多随机颜色?
groups53 = dfagingmedioporarea.groupby(by='Area')数据53 = []color53=get_colors(50)对于 group53,group53 中的 dataframe53:dataframe53 = dataframe53.sort_values(by=['Aging_days'], 升序=假)trace53 = go.Bar(x=dataframe53.Area.tolist(),y=dataframe53.Aging_days.tolist(),标记 = dict(color=colors53[len(data53)]),名称=group53,text=dataframe53.Aging_days.tolist(),文本位置='自动',)data53.append(trace53)layout53 = go.Layout(xaxis={'title': 'Area', 'categoryorder': '总降序', 'showgrid': False},yaxis={'title': 'dias', 'showgrid': False},margin={'l': 40, 'b': 40, 't': 50, 'r': 50},悬停模式='最近的',模板='plotly_white',标题={'text': "Aging Médio (Dias)",'y':.9,'x':0.5,'xanchor': '中心','yanchor': '顶'})图53 = go.Figure(数据=数据53,布局=布局53)
许多关于情节颜色主题的问题已经被提出和回答.参见例如
完整代码:
import plotly.graph_objects as go导入 plotly.express 作为 px从 itertools 导入循环# 颜色调色板 = 循环(px.colors.qualitative.Bold)#palette = cycle(['黑色','灰色','红色','蓝色'])调色板 = 循环(px.colors.sequential.PuBu# 数据df = px.data.gapminder().query(大洲 == '欧洲' and year == 2007 and pop > 2.e6")# 情节设置fig = go.Figure()# 添加痕迹国家 = '德国'fig.add_traces(go.Bar(x=[country],y = df[df['country']==country]['pop'],名称 = 国家,标记颜色=下一个(调色板)))国家 = '法国'fig.add_traces(go.Bar(x=[country],y = df[df['country']==country]['pop'],名称 = 国家,标记颜色=下一个(调色板)))国家 = '英国'fig.add_traces(go.Bar(x=[country],y = df[df['country']==country]['pop'],名称 = 国家,标记颜色=下一个(调色板)))图.show()
I using code below to generate chart with multiple traces. However the only way that i know to apply different colours for each trace is using a randon function that ger a numerico RGB for color.
But random color are not good to presentations.
How can i use a pallet colour for code below and dont get more random colors?
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)
Many questions on the topic of plotly colors have already been asked and answered. See for example Plotly: How to define colors in a figure using plotly.graph_objects and plotly.express? But it seems that you would explicitly like to add traces without using a loop. Perhaps because the attributes for trace not only differ in color? And to my knowledge there is not yet a description on how to do that efficiently.
The answer:
- Find a number of available palettes under
dir(px.colors.qualitative)
, or - define your very own palette like
['black', 'grey', 'red', 'blue']
, and - retrieve one by one using
next(palette)
for each trace you decide to add to your figure.
And next(palette)
may seem a bit cryptic at first, but it's easily set up using Pythons itertools
like this:
import plotly.express as px
from itertools import cycle
palette = cycle(px.colors.qualitative.Plotly)
palette = cycle(px.colors.sequential.PuBu
Now you can use next(palette)
and return the next element of the color list each time you add a trace. The very best thing about this is, as the code above suggests, that the colors are returned cyclically, so you'll never reach the end of a list but start from the beginning when you've used all your colors once.
Example plot:
Complete code:
import plotly.graph_objects as go
import plotly.express as px
from itertools import cycle
# colors
palette = cycle(px.colors.qualitative.Bold)
#palette = cycle(['black', 'grey', 'red', 'blue'])
palette = cycle(px.colors.sequential.PuBu
# data
df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
# plotly setup
fig = go.Figure()
# add traces
country = 'Germany'
fig.add_traces(go.Bar(x=[country],
y = df[df['country']==country]['pop'],
name = country,
marker_color=next(palette)))
country = 'France'
fig.add_traces(go.Bar(x=[country],
y = df[df['country']==country]['pop'],
name = country,
marker_color=next(palette)))
country = 'United Kingdom'
fig.add_traces(go.Bar(x=[country],
y = df[df['country']==country]['pop'],
name = country,
marker_color=next(palette)))
fig.show()
这篇关于Plotly:如何为使用多条轨迹创建的图形设置调色板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!