Python 将颜色映射到plotly go.饼图中的标签
我正在使用make_子图和go.pie绘制一系列3个饼图。我想最终把它们放在dash应用程序中,用户可以在其中过滤数据并更新图形。如何将特定颜色映射到变量,以便男性始终为蓝色,女性始终为粉色等。您可以使用plotly express with color_discrete_map执行此操作,但plotly express不支持子地块afaik 下面是我制作性别饼图的一个例子。其他dfs具有相同的格式,只是值不同Python 将颜色映射到plotly go.饼图中的标签,python,plotly,plotly-dash,Python,Plotly,Plotly Dash,我正在使用make_子图和go.pie绘制一系列3个饼图。我想最终把它们放在dash应用程序中,用户可以在其中过滤数据并更新图形。如何将特定颜色映射到变量,以便男性始终为蓝色,女性始终为粉色等。您可以使用plotly express with color_discrete_map执行此操作,但plotly express不支持子地块afaik 下面是我制作性别饼图的一个例子。其他dfs具有相同的格式,只是值不同 Gender ACC_ID percent 0 Female 57
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)
fig=make_子图(行=1,列=3,规格=[[{'type':'domain'},{'type':'domain'},{'type':'domain'}])
图添加跟踪(go.Pie(标签=年龄[“年龄组”]、值=年龄[“百分比”]、客户数据=年龄[“ACC\U ID”]、textinfo='label+percent',insidetextorientation='horizontal',textfont=dict(颜色='#000000')、marker\u colors=px.colors.Quality.Plotly),
1, 1)
图.添加跟踪(go.Pie(标签=性别[“性别”]、值=性别[“百分比”]、客户数据=性别[“ACC_ID”]、textinfo='label+percent',insidetextorientation='horizontal',textfont=dict(颜色='#000000')、标记颜色=px.colors.Quality.Plotly),
1, 2)
fig.add_trace(go.Pie(标签=样本[“样本类型”]、值=样本[“百分比”]、客户数据=样本[“ACC_ID”]、textinfo='label+percent',texttemplate='{label}
%{percent:.1%f}、insidetextorientation='Horizontation',textfont=dict(颜色='.'000000')、marker_colors=px.colors.Quality.Prism),
1, 3)
图.update_跟踪(孔=.4,hoverinfo='label+percent',hovertemplate=“%{label}
百分比:%{percent}
总计:%{customdata}”)
图1.2.2.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1(
showlegend=False,
uniformtext_minsize=14,
uniformtext_mode='hide',
注释=[dict(text='Age',x=0.13,y=0.5,font\u size=20,showarrow=False,font=dict(color=“black”),
dict(text='Gender',x=0.5,y=0.5,font\u 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”))
绘图(图)
看来这就是你想要的
您可以创建一个字典,其中所需的标记作为键,相应的颜色十六进制代码作为值
palette = {"Male": "#0064FF","Female":"#FF8FDF", "Other": "#FFC300"}
然后将其作为参数传递到饼图()中
愿意回答更多细节,但您需要提供一个