我有一个数据框如下:data = {'Contact':['Email', 'SMS', 'Email', 'Other', 'In Person', 'Other', 'SMS', 'Other', 'Phone', 'Email', 'Other', 'Phone', 'Phone', 'In Person', 'Email', 'Email', 'Other', 'Other', 'Other', 'Phone', 'Other', 'Email', 'Other', 'Other'], 'Age': [34, 50, 30, 43, 38, 43, 26, 37, 30, 30, 34, 38, 48, 30, 46, 37, 29, 36, 31, 31, 53, 25, 37, 25]}data = pd.DataFrame(data, columns=['Contact', 'Age'])data我想将Age列分成 10 组,然后将每个组的百分比绘制为线图,分别为每个唯一Contact值。由于 中有 5 个唯一值Contact,它们是'Email', 'SMS', 'Other', 'In Person', 'Phone',因此我希望有 1 个图,其中应该有 5 条线,每条线对应于每个唯一Contact值。但我得到以下信息:contacts = data['Contact'].unique()for c in contacts: df = data[data['Contact']==c] y,binEdges=np.histogram(df['Age'], bins=10) y = 100*y/sum(y) bincenters = 0.5*(binEdges[1:]+binEdges[:-1]) plt.plot(bincenters,y,label=c) plt.xlabel('Age') plt.ylabel('Percentage count') plt.show()
添加回答
举报
0/150
提交
取消