Kernel Density Estimation (KDE)
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”.
KDE plot
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()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()Compare KDE across groups
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()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()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.
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()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()If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
