亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將顏色映射到 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 關注
  • 318 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號