Plotly:如何为使用多条轨迹创建的图形设置调色板?

Cai*_*bio 6 python plotly plotly-python

我使用下面的代码生成具有多个跟踪的图表。但是,我知道为每个轨迹应用不同颜色的唯一方法是使用 randon 函数,该函数为颜色使用数字 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)
Run Code Online (Sandbox Code Playgroud)

ves*_*and 7

已经提出并回答了许多关于情节颜色主题的问题。例如,参见Plotly:如何使用 plotly.graph_objects 和 plotly.express 在图形中定义颜色? 但您似乎希望在不使用循环的情况下添加跟踪。也许是因为跟踪的属性不仅在颜色上不同?据我所知,目前还没有关于如何有效地做到这一点的描述。


答案:

  1. 在 下找到许多可用的调色板dir(px.colors.qualitative),或
  2. 定义你自己的调色板一样['black', 'grey', 'red', 'blue'],和
  3. 使用next(palette)您决定添加到图形中的每条轨迹一一检索。

并且next(palette)可以乍一看有点神秘,但它很容易设置使用蟒蛇itertools是这样的:

import plotly.express as px
from itertools import cycle
palette = cycle(px.colors.qualitative.Plotly)
palette = cycle(px.colors.sequential.PuBu)
Run Code Online (Sandbox Code Playgroud)

现在,您可以next(palette)在每次添加跟踪时使用并返回颜色列表的下一个元素。最好的一点是,正如上面的代码所暗示的那样,颜色是循环返回的,因此当您使用所有颜色一次时,您永远不会到达列表的末尾,而是从头开始。

示例图:

在此处输入图片说明

完整代码:

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()
Run Code Online (Sandbox Code Playgroud)