3 回答

TA貢獻1851條經驗 獲得超5個贊
如果沒有 Plotly Express,您可以嘗試以下操作:
通過添加以下內容來停止 go.Pie() 對數據進行排序:
sort=False
定義顏色:
fig.update_traces(marker=dict(colors=['blue', 'red', 'green']))
雖然它不如 color_discrete_map 干凈。

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)

TA貢獻1858條經驗 獲得超8個贊
看起來color_discrete_map就是你所追求的。
您可以創建一個字典,將所需的標簽作為鍵,將相應的顏色十六進制代碼作為值。
palette?=?{"Male":?"#0064FF","Female":"#FF8FDF",?"Other":?"#FFC300"}
然后將其作為參數傳遞到您的 pie() 圖表中。
添加回答
舉報