【问题标题】:How to plot pdf at the same graph as the histogram如何在与直方图相同的图表上绘制 pdf
【发布时间】:2022-11-01 18:09:59
【问题描述】:

我写了一个函数来绘制以下数据的直方图(缩短)

data_1 = 
[0.68417915 0.53041328 0.05499373 0.32483917 0.30501979 0.12136537
 0.22964997 0.5837272  0.06000122 0.69908738 0.15690346 0.20363323
 0.10390346 0.98658757 0.98359924 0.29493355 0.72561782 0.75613625
 0.69628136 0.71322217 0.63060554 0.91118187 0.14915375 0.70929528
 0.42408604 0.35388851 0.62253336 0.63676291 0.44358184 0.45063505
 0.36477958 0.15807182 0.714753   0.96713497 0.4094859  0.56495619
 0.57509395 0.9355384  0.46284749 0.67779101 0.92363017 0.05682404
 0.89631817 0.52587218 0.79428246 0.14486141 0.31300898 0.10176549
 0.21841843 0.25688406 0.55415834 0.84957183 0.76246304 0.98489949
 0.3936749  0.51460251 0.50138111 0.36060756 0.44854838 0.3919771
 0.05113578 0.23980216 0.96111616 0.05969004 0.63652018 0.77869691
 0.74565952 0.53789898 0.8876854  0.02370424 0.75647449 0.1494505
 0.56362217 0.84942793 0.75265825 0.43319662 0.1012875  0.09946243
 0.69463561 0.46931918 0.12913483 0.22142044 0.77253391 0.1691685
 0.41114265 0.011321   0.41941435 0.28070956 0.65810948 0.58770776
 0.68763623 0.36828773 0.70466821 0.8332811  0.12652526 0.16867114
 0.59106388 0.56926637 0.87954323 0.62176163 7.35566843e-01 
 1.00146415e-01 6.68137620e-01 4.39246138e-01
 3.75875260e-01 2.12544712e-02 3.68062161e-01 5.35692768e-01
 6.50231419e-01 7.51573475e-01 1.43792206e-01 3.51057868e-01
 1.77127799e-03 9.88480387e-01 8.73988015e-01 3.78791845e-01
 5.89179323e-01 4.05978444e-01 6.88178816e-01 8.73515486e-01
 3.66033185e-01 7.98291151e-01 2.30921252e-01 8.68201375e-04
 4.92515713e-01 4.56100036e-01 5.66357689e-01 1.18801303e-01
 8.15197293e-01 1.90998886e-02 4.91136435e-01 4.90613456e-01
 1.31219088e-01 8.44170500e-01 1.72284226e-01 9.48296215e-01
 7.36638954e-01 2.23674369e-01 7.46383520e-02 1.56815967e-01
 6.14167905e-02 9.55175567e-01 1.74517808e-01 6.16529512e-01
 7.02704931e-01 2.17204373e-01 6.78545848e-01 8.99756168e-01
 5.28857712e-01 8.34009864e-01 5.87747412e-01 9.01901813e-02
 9.94429960e-01 8.20847209e-01 3.88627889e-01 7.99302264e-01
 1.19291073e-01 3.92748464e-01 4.84674232e-01 6.86047613e-01
 9.09811416e-01 4.11619033e-01 5.22738580e-01 7.87679969e-01
 8.31886542e-01 5.75564445e-01 7.03306890e-01 4.37121850e-01
 2.17908948e-01 9.27734103e-01 1.69151398e-01 1.02815443e-01
 8.86529746e-01 9.12471508e-01 3.62394360e-02 5.75760637e-01
 9.02910130e-01 9.46808438e-01 5.22324825e-01 7.41599515e-02
 1.67554744e-01 9.67044492e-01 6.41305316e-02 2.02375526e-01
 7.87664750e-01 4.10928526e-01 3.75066800e-01 1.02825038e-01
 7.99960722e-01 5.15931793e-01 6.07891990e-01 4.22650890e-01
 2.50692729e-01 4.76696332e-01 3.42881458e-01 4.56350909e-01
 2.21493003e-02 9.22045389e-01 4.31748031e-01 3.67451551e-01]

以及以下代码

def plot_histo(data_list, bin_count):
    plt.hist(data_list, bins=bin_count, density= True)
    return plt.show()

plot_1 = plot_histo(data_1, 100)

我也想在同一张图上绘制这个分布的 pdf,但我真的不知道该怎么做,因为我是 python 新手!有小费吗?

【问题讨论】:

    标签: python matplotlib scipy statistics


    【解决方案1】:

    有几种方法可以从样本中估计 pdf。

    一种方法是使用核密度估计,这可以通过sns.kdeplot 轻松完成。

    另一种方法是拟合已知分布的参数,例如如果您有理由认为您的数据是高斯数据,请使用 scikit-learn GaussianMixture

    import matplotlib.pyplot as plt
    import numpy as np
    import scipy.stats as stats
    import seaborn as sns
    from sklearn.mixture import GaussianMixture
    
    num_samples = 1_000
    mu = 3.14
    sigma = 1.27
    
    data = np.random.randn(num_samples) * sigma + mu
    
    fig, ax = plt.subplots()
    
    # Histogram
    ax.hist(data, bins=10, density=True, label="Histogram")
    
    # Kernel Density Estimation (KDE)
    sns.kdeplot(data, bw_method=0.2, ax=ax, label="KDE")
    
    # Gaussian fitting
    gm = GaussianMixture(n_components=1).fit(data.reshape(-1, 1))
    mu_ = gm.means_.item()
    sigma_ = gm.covariances_.item()
    xx = np.linspace(*ax.get_xlim(), num=100)
    plt.plot(xx, stats.norm.pdf(xx, mu_, sigma_), label="Gaussian")
    
    ax.legend()
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 2019-09-24
      • 2013-06-04
      • 2020-06-19
      • 2014-05-30
      相关资源
      最近更新 更多