【发布时间】:2020-12-26 02:20:04
【问题描述】:
我正在尝试在 haberman 数据集中回答 (https://www.kaggle.com/gilsousa/habermans-survival-data-set)
- 在不同年龄段的人中存活的百分比是多少
在熊猫数据框“数据”中获取数据后,我采取了以下步骤。无法创建具有“存活百分比”的计算列。存活百分比 = 存活总数 / 该年龄组的总数。
print(data.head(3))
age year nodes status
0 30 64 1 1
1 30 62 3 1
2 30 65 0 1
data['age group'] = pd.cut(data.age,range(25,85,5))
table = data.pivot_table(index =['age group'], columns = ['status'], values='year',aggfunc=np.count_nonzero).fillna(0)
table.reset_index(level=0, inplace=True)
table['surv_pc'] = table.iloc[:,1]/(table.iloc[:,1]+table.iloc[:,2])*100
sns.set_theme(style="whitegrid")
bar = sns.barplot(data=table, x='age group',y='surv_pc')
图表类型为matplotlib.axes._subplots.AxesSubplot
如何访问条形高度,以便按照 plot.text() 方法放置标签?
根据文档
https://matplotlib.org/3.1.0/api/axes_api.html?highlight=matplotlib#matplotlib.axes.Axes,它只返回 Axes 对象。请帮助理解...
【问题讨论】:
-
我试图查看条形图元素内部,但这给出了错误 ---> 对于 bar 中的元素:print(elements) TypeError: 'AxesSubplot' object is not iterable
标签: python pandas matplotlib seaborn