I managed to create a plot that shows the number of records per class label for each age in my Pandas dataframe. But I would also like to see a percentage label for the "non functional" class in each age group.
The Python code for the graph is
train['age_wpt'] = train.date_recorded.str.split('-').str.get(0).apply(int) - train.construction_year
figure = plt.figure(figsize=(15,8))
plt.hist([
train[(train.status_group=='functional') & (train.age_wpt < 60.0) & (train.age_wpt >= 0.0)]['age_wpt'],
train[(train.status_group=='non functional') & (train.age_wpt < 60.0) & (train.age_wpt >= 0.0)]['age_wpt'],
train[(train.status_group=='functional needs repair') & (train.age_wpt < 60.0) & (train.age_wpt >= 0.0)]['age_wpt']
],
stacked=True, color = ['b','r','y'],
bins = 30,label = ['functional','non functional', 'functional needs repair'])
plt.xlabel('Age')
plt.ylabel('Number of records')
plt.legend()
normed : boolean, optional If
True
, the first element of the return tuple will be the counts normalized to form a probability density, ie,n/(len(x)`dbin)
, ie, the integral of the histogram will sum to 1. If stacked is also True , the sum of the histograms is normalized to 1. Default isFalse
plt.hist([
train[(train.status_group=='functional') & (train.age_wpt < 60.0) & (train.age_wpt >= 0.0)]['age_wpt'],
train[(train.status_group=='non functional') & (train.age_wpt < 60.0) & (train.age_wpt >= 0.0)]['age_wpt'],
train[(train.status_group=='functional needs repair') & (train.age_wpt < 60.0) & (train.age_wpt >= 0.0)]['age_wpt']
],
stacked=False, color = ['b','r','y'], normed=True
bins = 30,label = ['functional','non functional', 'functional needs repair'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.