Kernel Density Estimation

Learn how to add kernel density estimation to an Altair chart.

Kernel density estimation (KDE) is a statistical method used to estimate the probability density function of a random variable based on a set of observed data points. In simpler terms, KDE creates a smooth curve approximating the distribution of a dataset, which can help us better understand the underlying patterns and characteristics of the data.

How to use KDE in Altair

Altair supports KDE by allowing us to plot a KDE line directly into an Altair plot. To plot KDE in Altair, we use the transform_density() method. This function receives the following parameters as input:

  • The column of the dataset used for the density estimation

  • The as_ parameter renames the new columns created after the transformation.

We can use the transform_density() method with mark_area() or mark_line().

Let’s practice!

We’ll consider the following dataset:

Get hands-on with 1200+ tech skills courses.