为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

互换的青春 2023-04-11 16:20:58
我正在使用 make_subplots 和 go.Pie 绘制一系列 3 个饼图。我想最终将它们放在 dash 应用程序中,用户可以在其中过滤数据并且图形将更新。我如何将特定颜色映射到变量,以便男性始终为蓝色,女性始终为粉红色等。您可以使用 color_discrete_map 的 plotly express 来执行此操作,但 plotly express 不支持子图 afaik。这是我制作性别饼图的示例 df。其他 dfs 具有相同的格式只是不同的值。    Gender  ACC_ID  percent0   Female  57647   57.01   Male    37715   37.02   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 回答

?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

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

  • 通过添加以下内容来停止 go.Pie() 对数据进行排序:

    sort=False
  • 定义颜色:

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

虽然它不如 color_discrete_map 干净。


查看完整回答
反对 回复 2023-04-11
?
慕的地10843

TA贡献1785条经验 获得超8个赞

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


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)


查看完整回答
反对 回复 2023-04-11
?
猛跑小猪

TA贡献1858条经验 获得超8个赞

看起来color_discrete_map就是你所追求的。

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

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

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


查看完整回答
反对 回复 2023-04-11
  • 3 回答
  • 0 关注
  • 159 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信