Plotly:如何在不更改数据源的情况下更改 go.pie 图表的图例?

我正在练习使用 Python 在 Plotly Express 中构建饼图。
这是我制作的饼图;

http://img2.mukewang.com/649a82140001583b06290335.jpg

该图表是根据一个包含两列的文件构建的,名为

  1. gender值为[0, 1, 2]

  2. count_genders值为[total_count_0, total_count_1, total_count_2]

我计划为这些值添加一些描述;例如

  • 0 - female

  • 1 - male

  • 2 - undefined

这就是我目前陷入困境的地方。
如果我没记错的话,如果您想更改图例中的标签(至少在等值区域地图中),您可以操作ticks位于colorscale栏中的标签。通过操作它们,您可以重命名有关数据的标签。因此我想知道你是否可以在饼图中做同样的事情?

我当前的该图代码:

import pandas as pd

import 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无济于事。它返回有关不正确参数的错误消息。有人能帮我解决这个问题吗?


编辑1:如果我不清楚,我想知道是否可以修改图例中显示的值而不更改文件中的原始值。非常感谢您抽出时间帮助我解决这个问题!


编辑 2:添加代码的导入和其他先前详细信息,删除 Dropbox 链接。


富国沪深
浏览 158回答 2
2回答

杨魅力

如果我正确理解您的问题,您希望更改图例中显示的内容,而不更改数据源中的名称。可能有更优雅的方法可以做到这一点,但我已经组合了一个自定义函数newLegend(fig, newNames)来为您完成此任务。因此,对于这样的数字:...跑步:fig&nbsp;=&nbsp;newLegend(fig&nbsp;=&nbsp;fig,&nbsp;newNames&nbsp;=&nbsp;{'Australia':'Australia&nbsp;=&nbsp;Dangerous', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'New&nbsp;Zealand'&nbsp;:&nbsp;'New&nbsp;Zealand&nbsp;=&nbsp;Peaceful'})...会给你:我希望这就是您正在寻找的。如果没有,请随时告诉我!完整代码:import plotly.express as pxdf = 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):&nbsp; &nbsp; for item in newNames:&nbsp; &nbsp; &nbsp; &nbsp; for i, elem in enumerate(fig.data[0].labels):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if elem == item:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fig.data[0].labels[i] = newNames[item]&nbsp; &nbsp; return(fig)fig = newLegend(fig = fig, newNames = {'Australia':'Australia = Dangerous',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'New Zealand' : 'New Zealand = Peaceful'})fig.show()编辑 1:来自 OP 的数据样本示例您的数据面临的挑战是genders属于类型integer而不是类型string。因此,自定义函数尝试将一种类型的元素替换为另一种类型的元素。我通过一次性替换包含标签的整个数组来解决这个问题,而不是逐个元素地操作它。阴谋:完整代码:import pandas as pdimport plotly.express as pximport numpy as np# custom function to change labels&nbsp; &nbsp;&nbsp;def newLegend(fig, newNames):&nbsp; &nbsp; newLabels = []&nbsp; &nbsp; for item in newNames:&nbsp; &nbsp; &nbsp; &nbsp; for i, elem in enumerate(fig.data[0].labels):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if elem == item:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #fig.data[0].labels[i] = newNames[item]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newLabels.append(newNames[item])&nbsp; &nbsp; fig.data[0].labels = np.array(newLabels)&nbsp; &nbsp; return(fig)'''Pandas DataFrame:'''users_genders = pd.DataFrame({'0': {0: 1, 1: 2},&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '802420': {0: 246049, 1: 106}})users_genders = pd.DataFrame({'gender':[0,1,2],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'count_genders': [802420, 246049, 106]})''' Pie Chart Viz '''gender_distribution = px.pie(users_genders,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;values='count_genders',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;names='gender',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color_discrete_map={'0': 'blue',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'1': 'red',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'2': 'green'},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;title='Gender Distribution <br>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'between 2006-02-16 to 2014-02-20',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hole=0.35)gender_distribution.update_traces(textposition='outside',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textinfo='percent+label',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; marker=dict(line=dict(color='#000000',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width=4)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pull=[0.05, 0, 0.03],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opacity=0.9,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # rotation=180&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )gender_distribution.update_layout(legend=dict({'traceorder': 'normal'}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # ticks='inside',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # tickvals=[0, 1, 2],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # ticktext=["0 - Female",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"1 - Male",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"2 - Undefined"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # dtick=3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;legend_title_text='User Genders')# custom function set to workgender_distribution=newLegend(gender_distribution, {0:"0 - Female",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1:"1 - Male",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2: "2 - Undefined"})gender_distribution.show()

一只萌萌小番薯

newnames = {'0': 'zero', '1': 'one', '2': 'two'}fig.for_each_trace(lambda t: t.update(&nbsp; labels=[newnames[label] for label in t.labels])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python