KDE - kernel density estimate

Seaborn basics

2 min read

Published Oct 7 2025


24
0
0
0

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 data
  • x, y = Variables for 1D or 2D density
  • hue = Adds separate KDEs for subgroups
  • fill = 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 separately
  • bw_adjust = Bandwidth adjustment (controls smoothness)
  • cut = Extent of curve beyond data range
  • gridsize = Number of evaluation points (resolution)
  • cmap = Colourmap for 2D plots
  • levels = 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.


seaborn kde plot basic example





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.


seaborn kde plot fill example





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.


seaborn kde plot hue example





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

seaborn kde plot stack example





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_adjustmore detail (wigglier curve)
  • Larger bw_adjustsmoother (less detailed)

seaborn kde plot smoothness example





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).


seaborn kde plot 2D example





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.


seaborn kde plot 2D filled example





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.


seaborn kde plot 2D hue example





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.


seaborn kde plot clip range example





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.


seaborn kde plot cumulative example





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.


seaborn kde plot horizontal example





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.


seaborn kde plot cut example
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact