python: plotting a histogram with a function line on top

后端 未结 3 1527
情书的邮戳
情书的邮戳 2021-02-04 04:35

I\'m trying to do a little bit of distribution plotting and fitting in Python using SciPy for stats and matplotlib for the plotting. I\'m having good luck with some things like

3条回答
  •  [愿得一人]
    2021-02-04 05:18

    Expanding on Malik's answer, and trying to stick with vanilla NumPy, SciPy and Matplotlib. I've pulled in Seaborn, but it's only used to provide nicer defaults and small visual tweaks:

    import numpy as np
    import scipy.stats as sps
    import matplotlib.pyplot as plt
    
    import seaborn as sns
    sns.set(style='ticks')
    
    # parameterise our distributions
    d1 = sps.norm(0, 10)
    d2 = sps.norm(60, 15)
    
    # sample values from above distributions
    y1 = d1.rvs(300)
    y2 = d2.rvs(200)
    # combine mixture
    ys = np.concatenate([y1, y2])
    
    # create new figure with size given explicitly
    plt.figure(figsize=(10, 6))
    
    # add histogram showing individual components
    plt.hist([y1, y2], 31, histtype='barstacked', density=True, alpha=0.4, edgecolor='none')
    
    # get X limits and fix them
    mn, mx = plt.xlim()
    plt.xlim(mn, mx)
    
    # add our distributions to figure
    x = np.linspace(mn, mx, 301)
    plt.plot(x, d1.pdf(x) * (len(y1) / len(ys)), color='C0', ls='--', label='d1')
    plt.plot(x, d2.pdf(x) * (len(y2) / len(ys)), color='C1', ls='--', label='d2')
    
    # estimate Kernel Density and plot
    kde = sps.gaussian_kde(ys)
    plt.plot(x, kde.pdf(x), label='KDE')
    
    # finish up
    plt.legend()
    plt.ylabel('Probability density')
    sns.despine()
    

    gives us the following plot:

    I've tried to stick with a minimal feature set while producing relatively nice output, notably using SciPy to estimate the KDE is very easy.

提交回复
热议问题