Python Central HubSign in

Data Analytics with Python / Phase 6: Statistical Visualization with Seaborn

Kernel Density Estimation (KDE)

Understand KDE as a smooth distribution estimate and plot it using Seaborn’s kdeplot.

What is 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.

KDE plot

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

Visualize it

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.

Compare KDE across groups

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

Bandwidth matters

The “bandwidth” controls smoothing:

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

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

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

Check your understanding

quizCheck yourself
  1. How is a KDE best described?

    Show answer

    B — A smoothed histogram that approximates a probability density — A KDE stacks small bumps at each data point to produce a smooth density curve.

  2. What happens if the bandwidth is too small?

    Show answer

    B — The curve is noisy — Too small a bandwidth gives a noisy curve and too large over-smooths. bw_adjust=0.6 is less smooth than 1.6 in the lesson.

  3. Which argument tunes smoothing in sns.kdeplot?

    Show answer

    B — bw_adjust — bw_adjust scales the default bandwidth. bins belongs to histograms, and common_norm controls normalization across hue groups.

  4. What does common_norm=False do in the KDE by sex example?

    Show answer

    B — Normalizes each group on its own so shapes are comparable — Each hue group gets its own density scaled to area one rather than sharing one normalization.

Next

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

🧪 Try It Yourself

Exercise 1 – Draw a Filled KDE

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Compare Normalization With common_norm

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Adjust the Bandwidth

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did