繁体   English   中英

Map colors 到 plotly go 中的标签。

[英]Map colors to labels in plotly go.Pie charts

我正在使用 make_subplots 和 go.Pie 绘制一系列 3 个饼图。 我想最终将这些放在一个仪表板应用程序中,用户可以在其中过滤数据并且图形会更新。 How can I map specific colors to variables so that Male is always blue, Female is always pink, etc. You can do this with plotly express with color_discrete_map but plotly express doesn't support subplots afaik.

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

    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)

看来color_discrete_map是你所追求的。

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

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

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

很想回答更多细节,但您需要提供MRE ..

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

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

     sort=False
  • 定义颜色:

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

它虽然不如 color_discrete_map 干净。

您实际上可以使用从go.Pie()到 map 值(颜色)的参数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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM