Density chart


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

⏱ Quick start

If you are in a rush, the most straightforward way to build a density chart is to use Seaborn and its kdeplot() function. You just need to provide it with a numeric variable from your dataset. 🔥

# library & dataset
import seaborn as sns
df = sns.load_dataset('iris')

# Make default density plot
sns.kdeplot(df['sepal_width'])

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.

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

Contact

👋 This document is a work by Yan Holtz. Any feedback is highly encouraged. You can fill an issue on Github, drop me a message onTwitter, or send an email pasting yan.holtz.data with gmail.com.

Violin

Density

Histogram

Boxplot

Ridgeline

Scatterplot

Heatmap

Correlogram

Bubble

Connected Scatter

2D Density

Barplot

Spider / Radar

Wordcloud

Parallel

Lollipop

Circular Barplot

Treemap

Venn Diagram

Donut

Pie Chart

Dendrogram

Circular Packing

Line chart

Area chart

Stacked Area

Streamgraph

Timeseries with python

Timeseries

Map

Choropleth

Hexbin

Cartogram

Connection

Bubble

Chord Diagram

Network

Sankey

Arc Diagram

Edge Bundling

Colors

Interactivity

Animation with python

Animation

Cheat sheets

Caveats

3D