使用Plotly Go在饼图中将颜色映射到标签

3

我正在使用make_subplots和go.Pie绘制一系列的3个饼图。我想最终将它们放入一个dash应用程序中,用户可以过滤数据并更新图表。如何将特定颜色映射到变量,以便男性始终为蓝色,女性始终为粉色等。您可以使用plotly express的color_discrete_map实现此目的,但据我所知,plotly express不支持子图。

这是一个示例数据框,我从中制作性别饼图。其他数据框具有相同的格式,只是值不同。

    Gender  ACC_ID  percent
0   Female  57647   57.0
1   Male    37715   37.0
2   Other   5875    6.0

以下是我用来制作图形的代码

fig = make_subplots(rows=1, cols=3, specs=[[{'type':'domain'}, {'type':'domain'},{'type':'domain'}]])
fig.add_trace(go.Pie(labels=age["Age_Group"], values=age["percent"],customdata=age["ACC_ID"], textinfo='label+percent',insidetextorientation='horizontal', textfont=dict(color='#000000'), marker_colors=px.colors.qualitative.Plotly),
              1, 1)
fig.add_trace(go.Pie(labels=gender["Gender"], values=gender["percent"], customdata=gender["ACC_ID"],textinfo='label+percent',insidetextorientation='horizontal',textfont=dict(color='#000000'),marker_colors=px.colors.qualitative.Plotly),
              1, 2)
fig.add_trace(go.Pie(labels=sample["Sample_Type"], values=sample["percent"], customdata=sample["ACC_ID"],textinfo='label+percent',texttemplate='%{label}<br>%{percent:.1%f}',insidetextorientation='horizontal',textfont=dict(color='#000000'),marker_colors=px.colors.qualitative.Prism),
              1, 3)


fig.update_traces(hole=.4, hoverinfo='label+percent', hovertemplate="<b>%{label}</b><br>Percent: %{percent}<br>Total: %{customdata}<extra></extra>")

fig.update_layout(
    showlegend=False,
    uniformtext_minsize=14, 
    uniformtext_mode='hide',

    annotations=[dict(text='Age', x=0.13, y=0.5, font_size=20, showarrow=False, font=dict(color="black")),
                 dict(text='Gender', x=0.5, y=0.5, font_size=20, showarrow=False,font=dict(color="black")),
                 dict(text='Sample', x=0.879, y=0.5, font_size=20, showarrow=False,font=dict(color="black"))])
    

plot(fig)
3个回答

6

如果没有 Plotly Express,您可以尝试以下方法:

  • Stop go.Pie() from sorting your data by adding:

    sort=False
    
  • Define the colours:

    fig.update_traces(marker=dict(colors=['blue', 'red', 'green']))
    

不过它没有color_discrete_map那么清晰。


5

您实际上可以使用来自go.Pie() marker_colors 参数将值(颜色)映射到标签。您只需要创建一个 dict ,将 label:color 相关联,并像这样使用:

gender_color = {'Female':'pink', 'Male':'blue', 'Other':'yellow'}

fig = make_subplots(rows=1, cols=3, specs=[[{'type':'domain'}, {'type':'domain'},{'type':'domain'}]])
fig.add_trace(go.Pie(labels=age["Age_Group"], values=age["percent"],customdata=age["ACC_ID"], textinfo='label+percent',insidetextorientation='horizontal', textfont=dict(color='#000000'), marker_colors=px.colors.qualitative.Plotly),1, 1)
fig.add_trace(go.Pie(labels=gender["Gender"], values=gender["percent"], customdata=gender["ACC_ID"],textinfo='label+percent',insidetextorientation='horizontal',textfont=dict(color='#000000'),marker_colors=gender["Gender"].map(gender_color)),1, 2)
fig.add_trace(go.Pie(labels=sample["Sample_Type"], values=sample["percent"], customdata=sample["ACC_ID"],textinfo='label+percent',texttemplate='%{label}<br>%{percent:.1%f}',insidetextorientation='horizontal',textfont=dict(color='#000000'),marker_colors=px.colors.qualitative.Prism),1, 3)

fig.update_traces(hole=.4, hoverinfo='label+percent', hovertemplate="<b>%{label}</b><br>Percent: %{percent}<br>Total: %{customdata}<extra></extra>")

fig.update_layout(
    showlegend=False,
    uniformtext_minsize=14, 
    uniformtext_mode='hide',

    annotations=[dict(text='Age', x=0.13, y=0.5, font_size=20, showarrow=False, font=dict(color="black")),
                 dict(text='Gender', x=0.5, y=0.5, font_size=20, showarrow=False,font=dict(color="black")),
                 dict(text='Sample', x=0.879, y=0.5, font_size=20, showarrow=False,font=dict(color="black"))])
plot(fig)

1

看起来离散颜色地图就是您需要的。

您可以创建一个字典,将所需的标签作为键,将相应的颜色HEX代码作为值。

palette = {"Male": "#0064FF","Female":"#FF8FDF", "Other": "#FFC300"}

然后将其作为参数传递到您的饼图(pie() chart)中。

很乐意提供更详细的答案,但您需要提供一个MRE


我是Plotly的新手,仍在努力理解goffpx各自的应用场景。但是,color_discrete_map是否只与px有关呢?因为我的问题明确涉及到go,我正在尝试在饼图中硬编码颜色,但不得不使用go,因为基本示例基于go,而颜色示例则使用了px - Slaus
简短的回答是,你可以用PX做的任何事情都可以用GO做到。PX是GO的高级封装,因此它在底层使用GO。另一方面,FF用于创建非常特定类型的图表,这些图表在创建时很难使用GO实现。例如Styled Pie Chart示例中使用了GO和硬编码颜色序列。您还可以在示例中使用camps而不是列表。 - Geom
我认为这个不起作用。 - mtoto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接