gpt4 book ai didi

python - 将 KDE 添加到直方图

转载 作者:太空狗 更新时间:2023-10-29 20:18:22 24 4
gpt4 key购买 nike

我想在直方图中添加密度图。我对 pdf 函数有所了解,但我感到困惑,其他类似问题也没有帮助。

from scipy.stats import * 
from numpy import*
from matplotlib.pyplot import*
from random import*

nums = []
N = 100
for i in range(N):
a = randint(0,9)
nums.append(a)

bars= [0,1,2,3,4,5,6,7,8,9]
alpha, loc, beta=5, 100, 22

hist(nums,normed= True,bins = bars)


show()

我正在寻找这样的东西

enter image description here

最佳答案

from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(41)

N = 100
x = np.random.randint(0, 9, N)
bins = np.arange(10)

kde = stats.gaussian_kde(x)
xx = np.linspace(0, 9, 1000)
fig, ax = plt.subplots(figsize=(8,6))
ax.hist(x, density=True, bins=bins, alpha=0.3)
ax.plot(xx, kde(xx))

plot

关于python - 将 KDE 添加到直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33323432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com