Skip to content

Kernel Density Estimation (KDE)

A Kernel Density Estimate (KDE) is a smooth curve that approximates the probability density of a continuous variable.

Think of it as a “smoothed histogram”.

diagram Building a KDE curve mermaid
Each data point contributes a small bump (a kernel); adding all the bumps together makes one smooth curve.
kdeplot
import seaborn as sns
import matplotlib.pyplot as plt
 
values = [55, 60, 62, 63, 65, 67, 70, 72, 76, 80, 81, 85, 90, 92, 95]
 
plt.figure(figsize=(7, 4))
sns.kdeplot(values, fill=True)
plt.title("KDE plot")
plt.xlabel("Value")
plt.tight_layout()
plt.show()

Each dot on the axis drops a small bump (its own mini bell curve). Stacking every bump together produces the single smooth KDE line:

sketch KDE as stacked bumps p5.js
Each data point (dot) contributes a small bump; the blue line is the sum of all the bumps.
KDE by group
import seaborn as sns
import matplotlib.pyplot as plt
 
tips = sns.load_dataset("tips")
 
plt.figure(figsize=(7, 4))
sns.kdeplot(data=tips, x="total_bill", hue="sex", fill=True, common_norm=False)
plt.title("KDE of total_bill by sex")
plt.tight_layout()
plt.show()

The “bandwidth” controls smoothing:

  • Too small → noisy curve
  • Too large → over-smoothed curve

Seaborn chooses a default automatically, but you can tune it.

Adjust bandwidth
import seaborn as sns
import matplotlib.pyplot as plt
 
plt.figure(figsize=(7, 4))
sns.kdeplot(values, bw_adjust=0.6, label="less smooth")
sns.kdeplot(values, bw_adjust=1.6, label="more smooth")
plt.legend()
plt.title("KDE bandwidth effects")
plt.tight_layout()
plt.show()

Continue to Box Plots and Whiskers to compare distributions using quartiles instead of a smooth curve.

Exercise 2 – Compare Normalization With common_norm

Section titled “Exercise 2 – Compare Normalization With common_norm”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading