如何使线图显示在同一图形上而不是在不同图形上?

时间:2018-09-04 07:41:48

标签: python matplotlib plot

我有一个数据框,如下所示:

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

enter image description here

我想将Age列分为10组,然后分别针对每个唯一的Contact值,将每个组的百分比绘制为线图。由于Contact中有5个唯一值,即'Email', 'SMS', 'Other', 'In Person', 'Phone',所以我希望有1个图,其中应该有5条线,每个唯一的Contact值各有1条线。但我得到以下信息:

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

enter image description here

1 个答案:

答案 0 :(得分:2)

如果将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()

enter image description here