Add KDE on to a histogram
Here's a solution using seaborn
0.11.1 and pandas
1.1.5:
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np
N = 100
nums = [np.random.randint(i-i, 9) for i in range(N)]
df = pd.DataFrame(nums, columns=["value"])
fig, ax1 = plt.subplots()
sns.kdeplot(data=df, x="value", ax=ax1)
ax1.set_xlim((df["value"].min(), df["value"].max()))
ax2 = ax1.twinx()
sns.histplot(data=df, x="value", discrete=True, ax=ax2)
Note how I use numpy
to generate the random values because I need actual values, not generators. The discrete=True
in the last line assures that the ticks are centered.
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))