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()

🚨 Grab the Data To Viz poster!


Do you know all the chart types? Do you know which one you should pick? I made a decision tree that answers those questions. You can download it for free!

    dataviz decision tree poster