将KDE添加到直方图 [英] Add KDE on to a histogram
本文介绍了将KDE添加到直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在直方图中添加密度图.我对 pdf 函数有所了解,但我很困惑,其他类似的问题也没有帮助.
I would like to add a density plot to my histogram diagram. I know something about pdf function but I've got confused and other similar questions were not helpful.
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()
我正在寻找这样的东西
推荐答案
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))
这篇关于将KDE添加到直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文