Treemap
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
A treemap
displays each element of a dataset as a rectangle. It allows to see what proportion each element has compared to the whole.
This is doable with python
and Matplotlib
thanks to the squarify
library that can be used as follow:🔥
# 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()
Treemap 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.
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! 🔥