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”.
flowchart LR A["Data points"] --> B["Place a small
bump (kernel) at each point"] B --> C["Add all the bumps
together"] C --> D["One smooth curve
(the KDE)"]
KDE plot
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:
Compare KDE across groups
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.
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
How is a KDE best described?
A KDE stacks small bumps at each data point to produce a smooth density curve.
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.
What happens if the bandwidth is too small?
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.
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.
Which argument tunes smoothing in sns.kdeplot?
bw_adjust scales the default bandwidth. bins belongs to histograms, and common_norm controls normalization across hue groups.
Show answer
B — bw_adjust — bw_adjust scales the default bandwidth. bins belongs to histograms, and common_norm controls normalization across hue groups.
What does common_norm=False do in the KDE by sex example?
Each hue group gets its own density scaled to area one rather than sharing one normalization.
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
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Compare Normalization With common_norm
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Adjust the Bandwidth
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.