KDE - kernel density estimate
Seaborn basics
2 min read
This section is 2 min read, full guide is 42 min read
Published Oct 7 2025
Log in to enable the "Like" button
Log in to enable the "Save" button
Share or download this guide
ChartsGraphsMatplotlibNumPyPandasPythonSeabornVisualisation
seaborn.kdeplot()
draws a Kernel Density Estimate (KDE) plot — a smoothed version of a histogram.
Instead of showing discrete bins, it estimates the probability density function (PDF) of a continuous variable.
This makes it perfect for visualising:
- The shape of a distribution
- Comparisons between multiple distributions
- Smoothed trends rather than raw counts
Syntax:
sns.kdeplot(
data=None,
x=None,
y=None,
hue=None,
fill=False,
multiple="layer",
common_norm=True,
common_grid=False,
bw_adjust=1,
cut=3,
clip=None,
gridsize=200,
thresh=0.05,
levels=10,
cmap=None,
shade=None, # deprecated, use fill
ax=None,
**kwargs
)
Parameters:
data
= DataFrame containing the datax
, y
= Variables for 1D or 2D densityhue
= Adds separate KDEs for subgroupsfill
= Fill the area under the curve (default False)multiple
= How multiple hues are displayed ("layer", "stack", "fill")common_norm
= Whether densities are normalised together or separatelybw_adjust
= Bandwidth adjustment (controls smoothness)cut
= Extent of curve beyond data rangegridsize
= Number of evaluation points (resolution)cmap
= Colourmap for 2D plotslevels
= Number of contour levels (for 2D plots)
Basic example
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.kdeplot(data=tips, x="total_bill")
plt.show()
Shows a smooth density curve of the total_bill
variable - similar to a histogram, but continuous.
Fill the area under the curve
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.kdeplot(data=tips, x="total_bill", fill=True, color="skyblue")
plt.show()
Fills the area under the KDE curve — great for visual clarity.
Add multiple distributions with hue
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.kdeplot(data=tips, x="total_bill", hue="sex", fill=True, palette="Set2")
plt.show()
Draws one curve per group (Male
vs. Female
), coloured separately.
Stack or normalise multiple distributions
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.kdeplot(
data=tips,
x="total_bill",
hue="day",
multiple="stack",
fill=True,
palette="coolwarm"
)
plt.show()
Options for multiple:
"layer"
= Overlapping curves (default)"stack"
= Stacked densities"fill"
= Stacked and normalised to 100% height
Adjust smoothness (bandwidth)
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.kdeplot(data=tips, x="total_bill", bw_adjust=0.5, fill=True)
plt.show()
- Smaller
bw_adjust
→ more detail (wigglier curve) - Larger
bw_adjust
→ smoother (less detailed)
2D KDE plot (Bivariate Distribution)
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.kdeplot(data=tips, x="total_bill", y="tip")
plt.show()
Displays a contour plot showing where data points are most dense (darker = higher density).
2D KDE with filled contours
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.kdeplot(
data=tips,
x="total_bill",
y="tip",
fill=True,
cmap="mako"
)
plt.show()
Adds filled contours, similar to a topographic heatmap.
Hue in 2D KDE
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.kdeplot(
data=tips,
x="total_bill",
y="tip",
hue="sex",
fill=True,
cmap="coolwarm"
)
plt.show()
One filled contour per hue group — useful for comparison.
Clip KDE to data range
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.kdeplot(data=tips, x="total_bill", fill=True, clip=(0, 60))
plt.show()
Restricts the KDE curve to a specific range.
Cumulative distribution (CDF)
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.kdeplot(data=tips, x="total_bill", cumulative=True, fill=True, color="lightgreen")
plt.show()
Shows how the cumulative probability increases across values.
Orientation (horizontal)
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.kdeplot(data=tips, y="total_bill", fill=True, color="tomato")
plt.show()
Flip orientation by using y
instead of x
.
Control density extent (cut)
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.kdeplot(data=tips, x="total_bill", cut=0, fill=True)
plt.show()
Prevents the curve from extending beyond the actual data range.