Plotly:如何为使用多个轨迹创建的地物设置调色板?

2024-10-02 08:22:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用下面的代码生成具有多个跟踪的图表。 然而,我所知道的为每条轨迹应用不同颜色的唯一方法是使用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)

Tags: 函数falsegobytitle颜色areadays
1条回答
网友
1楼 · 发布于 2024-10-02 08:22:45

许多关于色彩主题的问题已经被提出和回答。 参见示例Plotly: How to define colors in a figure using plotly.graph_objects and plotly.express? 但您似乎明确希望添加跟踪而不使用循环。 也许是因为trace的属性不仅在颜色上不同?据我所知,目前还没有关于如何有效地做到这一点的描述


答案是:

  1. dir(px.colors.qualitative)下查找许多可用的选项板,或
  2. 定义自己的调色板,比如['black', 'grey', 'red', 'blue'],然后
  3. 使用next(palette)逐个检索您决定添加到图形中的每个轨迹

而且next(palette)一开始可能看起来有点神秘,但使用python itertools可以很容易地设置它,如下所示:

import plotly.express as px
from itertools import cycle
palette = cycle(px.colors.qualitative.Plotly)
palette = cycle(px.colors.sequential.PuBu)

现在,您可以使用next(palette)并在每次添加跟踪时返回颜色列表的下一个元素。最棒的是,正如上面的代码所建议的,颜色是循环返回的,所以你永远不会到达列表的末尾,而是从一开始就开始,当你使用了所有的颜色一次

示例图:

enter image description here

完整代码:

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()

相关问题 更多 >

    热门问题