#200 Basic Treemap with python

 

 

 

 

 

Here is a basic treemap example. It relies on the squarify library. This treemap only has one group level. The input is just a set of numeric value. Each square will be as big as its value.

 

 

 

 

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

# If you have 2 lists 
squarify.plot(sizes=[13,22,35,5], label=["group A", "group B", "group C", "group D"], alpha=.7 ) 
plt.axis('off') 
plt.show() 

# If you have a data frame? 
import pandas as pd 
df = pd.DataFrame({'nb_people':[8,3,4,2], 'group':["group A", "group B", "group C", "group D"] }) 
squarify.plot(sizes=df['nb_people'], label=df['group'], alpha=.8 ) 
plt.axis('off') 
plt.show() 

Leave a Reply

Your email address will not be published.