将颜色映射到plotly go.饼图中的标签

Gin*_*aze 3 python plotly plotly-dash

我正在使用 make_subplots 和 go.Pie 绘制一系列 3 个饼图。我想最终将它们放入破折号应用程序中,用户可以在其中过滤数据并且数字将更新。如何将特定颜色映射到变量,以便男性始终为蓝色,女性始终为粉红色等。您可以使用 color_discrete_map 的plotlyexpress 来完成此操作,但据我所知,plotlyexpress 不支持子图。

这是我制作性别饼图的示例 df。其他 dfs 具有相同的格式,只是值不同。

    Gender  ACC_ID  percent
0   Female  57647   57.0
1   Male    37715   37.0
2   Other   5875    6.0
Run Code Online (Sandbox Code Playgroud)

这是我用来制作数字的代码

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

小智 6

如果没有 Plotly Express,您可以尝试以下操作:

但它不如 color_discrete_map 那么干净。


小智 6

实际上,您可以使用go.Pie()中的参数marker_colors将值(颜色)映射到标签。您只需要创建一个关联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)
Run Code Online (Sandbox Code Playgroud)