我有一个数据帧如下:
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
值。由于在Contact
中有5个唯一值,即'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()
如果你喜欢plt.show()
,所有的情节都会显示在同一个数字上
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
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'])
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()