Treemap with colors mapped on values

logo of a chart:Tree

This post aims to show how to map colors on values in a treemap.

You can create a treemap with colors mapped on values by using squarify and matplotlib libraries. In the following example, in order to map colors, matplotlib.colors.Normalize() is used with vmin and vmax, minimum values and maximum values respectively. Each value in my_values list is normalized and then map to a color.

#libraries
import matplotlib
import matplotlib.pyplot as plt
import squarify # pip install squarify (algorithm for treemap)</pre>
 
# Create a dataset:
my_values=[i**3 for i in range(1,100)]
 
# create a color palette, mapped to these values
cmap = matplotlib.cm.Blues
mini=min(my_values)
maxi=max(my_values)
norm = matplotlib.colors.Normalize(vmin=mini, vmax=maxi)
colors = [cmap(norm(value)) for value in my_values]
 
# Change color
squarify.plot(sizes=my_values, alpha=.8, color=colors )
plt.axis('off')
plt.show()

Contact & Edit


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

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!