我正在使用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)
go
、ff
和px
各自的应用场景。但是,color_discrete_map
是否只与px
有关呢?因为我的问题明确涉及到go
,我正在尝试在饼图中硬编码颜色,但不得不使用go
,因为基本示例基于go
,而颜色示例则使用了px
。 - Slaus