Density chart

Density plots allow you to visualize the distribution of a numeric variable for one or several groups. They are very well adapted for large datasets, as stated in Note that 2 approaches exist to build them in python: the first one consists of computing a kernel density estimate, and the second one involves building a high resolution histogram.

Seaborn logoDensity charts with Seaborn

Seaborn is a python library allowing to make better charts easily. It is well adapted to build density charts thanks to its kdeplot function. The following charts will guide you through its usage, going from a very basic density plot to something much more customized.

Seaborn logoDensity charts, Seaborn and multiple groups

It is a common use case to compare the density of several groups in a dataset. Several options exist to do so. You can plot all items on the same chart, using transparency and annotation to make the comparison possible. Or you can a technique called small multiples where the graph window is split in individual charts, avoiding group overlaps that are sometimes hard to read.

A density chart with multiple groups displayed, transparency and clean annotation.

A density chart with multiple groups displayed, transparency and clean annotation.

🔎 kdeplot function parameters→ see full doc

stringcolor under the curve

Matplotlib logo Density charts with Matplotlib

It is possible to build a density chart with matplotlib, but it is truely a struggle compared to making it with seaborn as shown above. Here is an example to prove it is doable, but I strongly advise to go for seaborn.


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥