简体   繁体   English

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

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

I want to change the labels [2,3,4,5] from my pie chart and instead have them say [Boomer, Gen X, Gen Y, Gen Z] respectively.我想从我的饼图中更改标签 [2,3,4,5],而是让它们分别说 [Boomer, Gen X, Gen Y, Gen Z]。 I can't seem to find a direct way of doing this without changing the dataframe.如果不更改 dataframe,我似乎找不到直接的方法。 Is there any way to do this by working through the code I have?有没有办法通过我的代码来做到这一点?

饼形图

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")

how about change the code怎么改代码

labels = data.keys()

to

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

I don't know the data structure of your data, so I made a sample data and created a pie chart.我不知道你的数据的数据结构,所以我做了一个示例数据并创建了一个饼图。 Please modify your code to follow this.请修改您的代码以遵循这一点。

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")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM