Reputation:
I know there is some similar question asked, but i have test all the solution and it still give me the same error which is Value error: Label must be of length 'x'. Below is snippets of the code and data set in case needed.
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()
Upvotes: 0
Views: 3894
Reputation: 89
Your df["age"] need to be the same length as your labels list. Run this code below and see how it works.
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()
Upvotes: 1
Reputation: 4440
In labels you need to pass the df['Age']
so it can show the percentage of each 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()
Upvotes: 1