scipy 如何将pdf绘制在与直方图相同的图形上

yqkkidmi  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(165)

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

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的新手!有什么提示吗?

snz8szmq

snz8szmq1#

有几种方法可以从样本中估计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()

相关问题