Python TreeMap: A New Way to Visualize Data Today, we are going to take a look at how Python can be used to create treemaps. Treemaps are an interesting way of visualizing data that is slightly different from the traditional pie charts.This is because it allows for more complex visualization by splitting up rectangular subparts into smaller parts proportionate to the data they represent.There are several libraries in Python that allow you to create these treemap charts! Let’s get started with one now!
Table of Contents
How to Plot Treemaps in Python?
A treemap is a way to visualize hierarchical data. Treemaps display the entire hierarchy as nested rectangles, then they arrange them by size and color each rectangle according to its value.
This makes it easier for us to digest this kind of information from one picture. It can help you visualize how single values compose a whole. Treemap charts also let you visualize hierarchical data using nested rectangles.
In this tutorial, we will learn how to plot treemaps in Python using the Squarify library in python
So, let’s install the Squarify library first. To install the library run the given command in your CMD.
pip install squarify
Importing Squarify and Matplotlib
Now that we have installed Squarify, it’s time to import it so we can start using it. To do this, let’s go to our notebook and type in from squarify import *. Let’s also import matplotlib which will make the graphs nicer looking. We can do this by typing import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
import squarify
Example 01: Creating a Basic Python Treemap
This is a computer program that plots rectangles according to their given values. It’s an easy-to-read alternative to the pie chart for visualizing data, but with less clarity about which part of the whole each rectangle represents.
You can use this tool for any type of categorical data, such as identifying how many people play different sports or what breeds are most popular in your area.
Code: Creating a Treemap
import matplotlib.pyplot as plt
import squarify
sizes = [20, 20, 20, 20]
squarify.plot(sizes)
plt.show()
Output

We can add labels as in following code.
Code: Adding Labels to a Treemap
import matplotlib.pyplot as plt
import squarify
sizes = [20, 20, 20, 20]
label=["One", "Two", "Three", "Four"]
squarify.plot(sizes=sizes, label=label, alpha=0.6 )
plt.show()
Output

import matplotlib.pyplot as plt
import squarify
sizes = [20, 20, 20, 20]
label=["One", "Two", "THree", "Four"]
color=['yellow','orange','blue','green']
squarify.plot(sizes=sizes, label=label, color=color, alpha=0.6 )
plt.show()
Output

Code: Turning off the axis
import matplotlib.pyplot as plt
import squarify
sizes = [20, 20, 20, 20]
label=["One", "Two", "THree", "Four"]
color=['yellow','orange','blue','green']
squarify.plot(sizes=sizes, label=label, color=color, alpha=0.6 )
plt.axis('off')
plt.show()
Output

Conclusion
The Python Treemap is an easy to use data visualization that provides a way to compare parts of large sets of information. It’s perfect for visualizing hierarchical structures like nested folders, or even plays in theater productions.