我知道有人问过类似的问题,但是我已经测试了所有解决方案,但仍然给我相同的错误,即值错误:标签的长度必须为'x'。下面是必要时的代码和数据集的摘要。
data=[['E001', 'M', 34, 123,'Normal',350],
['E002', 'F', 40, 114, 'Overweight', 450],
['E003', 'F', 37, 135, 'Obesity', 169],
['E004', 'M', 30, 139, 'Underweight',189],
['E005', 'F', 44, 117, 'Underweight',183],
['E006', 'M', 36, 121, 'Normal', 80],
['E007', 'M', 33, 133, 'Obesity', 166],
['E008', 'F', 26, 140, 'Normal', 120],
['E009', 'M', 32, 133, 'Normal', 75],
['E0010','M', 36, 133, 'Underweight', 40]]
df=pd.DataFrame(data,columns=['EMPID','Gender','Age','Sales','BMI','Income'])
label=list(df.columns.values)
print(label)
plt.pie(df['Age'],labels=label,autopct='%1.1f%%', shadow=True)
plt.show()
答案 0 :(得分:1)
在标签中,您需要通过df['Age']
,以便显示每个年龄的百分比
data=[['E001', 'M', 34, 123,'Normal',350],
['E002', 'F', 40, 114, 'Overweight', 450],
['E003', 'F', 37, 135, 'Obesity', 169],
['E004', 'M', 30, 139, 'Underweight',189],
['E005', 'F', 44, 117, 'Underweight',183],
['E006', 'M', 36, 121, 'Normal', 80],
['E007', 'M', 33, 133, 'Obesity', 166],
['E008', 'F', 26, 140, 'Normal', 120],
['E009', 'M', 32, 133, 'Normal', 75],
['E0010','M', 36, 133, 'Underweight', 40]]
df=pd.DataFrame(data,columns=['EMPID','Gender','Age','Sales','BMI','Income'])
label=list(df.columns.values)
print(label)
plt.pie(df['Age'],labels=df['Age'],autopct='%1.1f%%', shadow=True)
plt.show()
答案 1 :(得分:0)
您的df [“ age”]必须与标签列表的长度相同。 在下面运行此代码,并查看其工作原理。
import pandas as pd
import matplotlib.pyplot as plt
data=[['E001', 'M', 34, 123,'Normal',350],
['E002', 'F', 40, 114, 'Overweight', 450],
['E003', 'F', 37, 135, 'Obesity', 169],
['E004', 'M', 30, 139, 'Underweight',189],
['E005', 'F', 44, 117, 'Underweight',183],
['E006', 'M', 36, 121, 'Normal', 80],
['E007', 'M', 33, 133, 'Obesity', 166],
['E008', 'F', 26, 140, 'Normal', 120],
['E009', 'M', 32, 133, 'Normal', 75],
['E0010','M', 36, 133, 'Underweight', 40]]
df=pd.DataFrame(data,columns=['EMPID','Gender','Age','Sales','BMI','Income'])
df.head()
label=list(df.columns.values)
print(label)
new_list = list(df['Age'])
plt.pie(new_list[4:],labels=label,autopct='%1.1f%%', shadow=True)
plt.show()