如何使线条图出现在同一个图上而不是出现在不同的数字上?

问题描述 投票:1回答:1

我有一个数据帧如下:

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值各一个。但我得到以下内容:

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

python matplotlib plot
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.