0
我設法創建一個圖表,顯示我的Pandas數據框中每個年齡段的每個類標籤的記錄數。但我還想看看每個年齡段的「非功能性」課程的百分比標籤。如何在Python中的直方圖中顯示百分比標籤
爲圖形的Python代碼是
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 = True將每個小節的百分比與圖表中所有小節的總數相加。 –
你能提供這些數據嗎? 'data = {'functional':train [(train.status_group =='functional')&(train.age_wpt <60.0)&(train.age_wpt> = 0.0)] ['age_wpt'],''非功能':火車[(train.status_group =='non functional')&(train.age_wpt <60.0)&(train.age_wpt> = 0.0)] ['age_wpt'],「功能需要修理」:train [(train.status_group = ='功能需要修復')&(train.age_wpt <60.0)&(train.age_wpt> = 0.0)] ['age_wpt']]}' – Alexander
此外,嘗試與'stacked = False' – Alexander