Add KDE to Bar Graph

I would like to add a density graph to my histogram chart. I know something about the PDF function, but I got confused and other similar questions did not help.

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()

I'm looking for something like this

enter image description here

+9
source share
1 answer
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, normed=True, bins=bins, alpha=0.3)
ax.plot(xx, kde(xx))

plot

+12
source

Source: https://habr.com/ru/post/1613047/


All Articles