【问题标题】:Plotly: How to set up a color palette for a figure created with multiple traces?Plotly:如何为使用多条轨迹创建的图形设置调色板?
【发布时间】:2020-12-15 15:35:52
【问题描述】:

我使用下面的代码生成具有多条轨迹的图表。 然而,我知道为每条轨迹应用不同颜色的唯一方法是使用随机函数,该函数为颜色生成数字 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)

【问题讨论】:

  • 请考虑将我的建议标记为已接受的答案

标签: python plotly plotly-python


【解决方案1】:

关于情节色彩主题的许多问题已经被询问和回答。 参见例如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) 并在每次添加跟踪时返回颜色列表的下一个元素。最好的一点是,正如上面的代码所暗示的,颜色是循环返回的,所以你永远不会到达列表的末尾,而是在你用完所有颜色一次时从头开始。

示例图:

完整代码:

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

【讨论】:

  • 我明白了,但是在这种情况下我如何选择 PuBu 作为调色板?
  • @CaioEuzébio inlcude palette = cycle(px.colors.sequential.PuBu)
猜你喜欢
  • 1970-01-01
  • 2021-05-03
  • 2020-09-05
  • 1970-01-01
  • 2016-12-01
  • 2021-05-18
  • 2021-03-09
  • 2018-10-16
  • 1970-01-01
相关资源
最近更新 更多