Treemap

logo of a chart:Tree

A treemap displays hierarchical data as a set of nested rectangles. Each group is represented by a rectangle, which area is proportional to its value. In Python, the squarify library allows to compute the rectangle positions and plot it.

⏱ Quick start

# libraries
import matplotlib.pyplot as plt
import squarify    # pip install squarify (algorithm for treemap)
import pandas as pd

# Create a data frame with fake data
df = pd.DataFrame({'nb_people':[8,3,4,2], 'group':["group A", "group B", "group C", "group D"] })

# plot it
squarify.plot(sizes=df['nb_people'], label=df['group'], alpha=.8 )
plt.axis('off')
plt.show()

Matplotlib logoTreemap with Matplotlib and Squarify

Matplotlib is probably the most famous and flexible python library for data visualization. It is appropriate to build any kind of chart, including the lollipop plot thanks to its stem() function.

Plotly logoTreemap with Plotly

Plotly is a very powerful library to create interactive graphics. It is known for its great interactivity and its ability to handle large datasets.

It comes with multiple functions to create treemaps, for both of its API: plotly.graph_objects and plotly.express. Try to hover and click on the following treemap to see how it reacts:

See code

Contact


👋 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! 🔥