第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Plotly:如何在不更改數(shù)據(jù)源的情況下更改 go.pie 圖表的圖例?

Plotly:如何在不更改數(shù)據(jù)源的情況下更改 go.pie 圖表的圖例?

富國滬深 2023-06-27 14:30:21
我正在練習(xí)使用 Python 在 Plotly Express 中構(gòu)建餅圖。這是我制作的餅圖;該圖表是根據(jù)一個(gè)包含兩列的文件構(gòu)建的,名為gender值為[0, 1, 2]count_genders值為[total_count_0, total_count_1, total_count_2]我計(jì)劃為這些值添加一些描述;例如0 - female1 - male2 - undefined這就是我目前陷入困境的地方。如果我沒記錯(cuò)的話,如果您想更改圖例中的標(biāo)簽(至少在等值區(qū)域地圖中),您可以操作ticks位于colorscale欄中的標(biāo)簽。通過操作它們,您可以重命名有關(guān)數(shù)據(jù)的標(biāo)簽。因此我想知道你是否可以在餅圖中做同樣的事情?我當(dāng)前的該圖代碼:import pandas as pdimport plotly.express as px            '''Pandas DataFrame:'''users_genders = pd.DataFrame({'gender': {0: 0, 1: 1, 2: 2},               'count_genders': {0: 802420, 1: 246049, 2: 106}})''' Pie Chart Viz '''gender_distribution = px.pie(users_genders,                             values='count_genders',                             names='gender',                             color_discrete_map={'0': 'blue',                                                 '1': 'red',                                                 '2': 'green'},                             title='Gender Distribution <br>'                                   'between 2006-02-16 to 2014-02-20',                             hole=0.35)gender_distribution.update_traces(textposition='outside',                                  textinfo='percent+label',                                  marker=dict(line=dict(color='#000000',                                                        width=4)),                                  pull=[0.05, 0, 0.03],                                  opacity=0.9,                                  # rotation=180                                  )我嘗試添加 ,ticks但update_layout無濟(jì)于事。它返回有關(guān)不正確參數(shù)的錯(cuò)誤消息。有人能幫我解決這個(gè)問題嗎?編輯1:如果我不清楚,我想知道是否可以修改圖例中顯示的值而不更改文件中的原始值。非常感謝您抽出時(shí)間幫助我解決這個(gè)問題!編輯 2:添加代碼的導(dǎo)入和其他先前詳細(xì)信息,刪除 Dropbox 鏈接。
查看完整描述

2 回答

?
楊魅力

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊

如果我正確理解您的問題,您希望更改圖例中顯示的內(nèi)容,而不更改數(shù)據(jù)源中的名稱??赡苡懈鼉?yōu)雅的方法可以做到這一點(diǎn),但我已經(jīng)組合了一個(gè)自定義函數(shù)newLegend(fig, newNames)來為您完成此任務(wù)。

因此,對于這樣的數(shù)字:

http://img1.sycdn.imooc.com//649a823300018c8305900387.jpg

...跑步:

fig = newLegend(fig = fig, newNames = {'Australia':'Australia = Dangerous',
                                       'New Zealand' : 'New Zealand = Peaceful'})

...會給你:

http://img1.sycdn.imooc.com//649a8243000106bf06360360.jpg

我希望這就是您正在尋找的。如果沒有,請隨時(shí)告訴我!


完整代碼:

import plotly.express as px


df = px.data.gapminder().query("continent == 'Oceania'")

fig = px.pie(df, values='pop', names='country')

fig.update_traces(textposition='inside')

fig.update_layout(uniformtext_minsize=12, uniformtext_mode='hide')


def newLegend(fig, newNames):

    for item in newNames:

        for i, elem in enumerate(fig.data[0].labels):

            if elem == item:

                fig.data[0].labels[i] = newNames[item]

    return(fig)


fig = newLegend(fig = fig, newNames = {'Australia':'Australia = Dangerous',

                                       'New Zealand' : 'New Zealand = Peaceful'})

fig.show()

編輯 1:來自 OP 的數(shù)據(jù)樣本示例

您的數(shù)據(jù)面臨的挑戰(zhàn)是genders屬于類型integer而不是類型string。因此,自定義函數(shù)嘗試將一種類型的元素替換為另一種類型的元素。我通過一次性替換包含標(biāo)簽的整個(gè)數(shù)組來解決這個(gè)問題,而不是逐個(gè)元素地操作它。


陰謀:

http://img1.sycdn.imooc.com//649a824e0001f49e06470306.jpg

完整代碼:

import pandas as pd

import plotly.express as px

import numpy as np


# custom function to change labels    

def newLegend(fig, newNames):

    newLabels = []

    for item in newNames:

        for i, elem in enumerate(fig.data[0].labels):

            if elem == item:

                #fig.data[0].labels[i] = newNames[item]

                newLabels.append(newNames[item])

    fig.data[0].labels = np.array(newLabels)

    return(fig)


'''

Pandas DataFrame:

'''

users_genders = pd.DataFrame({'0': {0: 1, 1: 2}, 

                              '802420': {0: 246049, 1: 106}})


users_genders = pd.DataFrame({'gender':[0,1,2],

                               'count_genders': [802420, 246049, 106]})


''' Pie Chart Viz '''

gender_distribution = px.pie(users_genders,

                             values='count_genders',

                             names='gender',

                             color_discrete_map={'0': 'blue',

                                                 '1': 'red',

                                                 '2': 'green'},

                             title='Gender Distribution <br>'

                                   'between 2006-02-16 to 2014-02-20',

                             hole=0.35)

gender_distribution.update_traces(textposition='outside',

                                  textinfo='percent+label',

                                  marker=dict(line=dict(color='#000000',

                                                        width=4)),

                                  pull=[0.05, 0, 0.03],

                                  opacity=0.9,

                                  # rotation=180

                                  )

gender_distribution.update_layout(legend=dict({'traceorder': 'normal'}

                                              # ticks='inside',

                                              # tickvals=[0, 1, 2],

                                              # ticktext=["0 - Female",

                                              #           "1 - Male",

                                              #           "2 - Undefined"],

                                              # dtick=3

                                              ),

                                   legend_title_text='User Genders')


# custom function set to work

gender_distribution=newLegend(gender_distribution, {0:"0 - Female",

                                                    1:"1 - Male",

                                                    2: "2 - Undefined"})



gender_distribution.show()



查看完整回答
反對 回復(fù) 2023-06-27
?
一只萌萌小番薯

TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊

newnames = {'0': 'zero', '1': 'one', '2': 'two'}


fig.for_each_trace(lambda t: t.update(

  labels=[newnames[label] for label in t.labels]

)


查看完整回答
反對 回復(fù) 2023-06-27
  • 2 回答
  • 0 關(guān)注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號