简体   繁体   中英

How to display percentage label in histogram plot in Python

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

This results in the following graph 在此处输入图片说明

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 is False

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.

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