如何 [plotly] 更改此饼图中的标签?

How can I change the labels in this pie chart [plotly]?

我想更改饼图中的标签 [2,3,4,5],让它们分别显示 [Boomer、X 世代、Y 世代、Z 世代]。在不更改数据框的情况下,我似乎无法找到直接执行此操作的方法。有没有办法通过我的代码来做到这一点?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

data = df.groupby("Q10_Ans")["Q4_Agree"].count()

pie, ax = plt.subplots(figsize=[10,6])
labels = data.keys()
plt.pie(x=data, autopct="%.1f%%", explode=[0.05]*4, labels=labels, pctdistance=0.5)
plt.title("Generations that agree data visualization will help with job prospects", fontsize=14);
pie.savefig("DeliveryPieChart.png")

我不知道你的数据的数据结构,所以我做了一个样本数据并创建了一个饼图。请修改您的代码以遵循此要求。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

# data = df.groupby("Q10_Ans")["Q4_Agree"].count()
data = pd.DataFrame({'Q10_Ans':['Boomer','Gen X','Gen Y','Gen Z'],'Q4_Agree':[2,3,4,5]})

fig, ax = plt.subplots(figsize=[10,6])
labels = data['Q10_Ans']
ax.pie(x=data['Q4_Agree'], autopct="%.1f%%", explode=[0.05]*4, labels=labels, pctdistance=0.5)
ax.set_title("Generations that agree data visualization will help with job prospects", fontsize=14);
plt.savefig("DeliveryPieChart.png")

改一下密码如何

labels = data.keys()

labels = ['Boomer','Gen X','Gen Y','Gen Z']