# How to plot Python Treemap

In this tutorial, we’ll see How to plot Python Treemap, as well as their functionality, using a simple piece of code.

## Introduction

There are a plethora of visualizations available to show how individual values combine to form a whole. Some are highly sophisticate and specific, while others, such as the one we’ll look at today, are fairly simple and straightforward. The idea of illustrating the proportions of a whole is frequently a wonderful starting point for storytelling, from pies to waffles charts. You can start with composition and then look at the different values one by one, or you can compare different structures from different entities or periods.

Ben Shneiderman, an American computer scientist, and professor at the University of Maryland was the first to apply the visualization in the 1990s. His visualizations were quite popular, and numerous implementations and techniques for making them can be found in a variety of tools and languages, including Tableau, PowerBi, Python, R, and many others.

## Treemap in Python

In Python, a treemap is a data visualization that divides a rectangle into sub-parts. Each component is proportional in size to the data it represents. It’s similar to a pie chart. Treemaps, on the other hand, can depict far more intricate data than a pie chart.

A treemap is a group of nested rectangles that displays hierarchical data. Each group is symbolized by a rectangle with a proportional area to its value. The squarify package in Python allows you to compute and visualize rectangle locations.

It can assist you in visualizing how individual values combine to form a whole. Treemap charts also use layered rectangles to depict hierarchical data.

We’ll learn how to plot treemaps in Python with the Squarify package in this tutorial.

## Python Treemap using the Squarify

Let’s start by installing Squarify.

``pip install squarify``

Using Squarify to Plot a Treemap in Python. We may begin by importing Squarify into our notebook once it has been installed. Let’s also add matplotlib to the mix.

``````import matplotlib.pyplot as plt
import squarify
``````

To plot a very basic treemap, we just need the values for each rectangle. After plotting the treemap, the rectangles would be in proportion to these values.

To generate and design our treemaps, we’ll utilize Python with Matplotlib, Squarify, and Pandas. We’ll take a brief look at what Plotly can do later.

``````import matplotlib.pyplot as plt
import squarify
import pandas as pd
``````

Squarify is simple to use; all we have to do is give it a list of integers and it will figure out the arrangement.

### Simple Example to Plot Python Treemap

``````import matplotlib.pyplot as plt
import squarify
import pandas as pd

square_size = [10, 20, 30, 40, 50]
squarify.plot(square_size)
plt.show()
``````

Output:

In Python, use the following lines of code to add labels to the treemap:

``````labels=["30", "40", "10", "60"]
squarify.plot(sizes=square_sizes, label=labels, alpha=0.6 ) ``````

Now, glancing at these random squares won’t provide you with much information. You might be able to deduce some information about the overall composition, but you won’t be able to recognize the squares unless you’re familiar with the data.

Let’s make our treemap a little more particular by adding some labels. We can also get rid of the axis and make the colors a little softer.

``````import matplotlib.pyplot as plt
import squarify
import pandas as pd

square_sizes=[30, 40, 10, 60]
labels=["30", "40", "10", "60"]
squarify.plot(sizes=square_sizes, label=labels, alpha=0.6 )
plt.axis('off')
plt.show()
``````

Output:

Have you noticed how the colors have shifted? We lowered the alpha, which is why they aren’t as bright, but they’ve also moved around. The way the colors are distributed to the squares is a little random – don’t ask me why. Even though randomization can be useful and lead to some happy accidents, it isn’t always the best technique to come up with a color scheme that works. To circumvent this, we can create a color palette.

``````import matplotlib.pyplot as plt
import squarify
import pandas as pd

square_sizes=[10, 40, 20, 70]
labels=["10", "40", "20", "70"]
colors=['purple','orange','blue','green']
squarify.plot(sizes=square_sizes, label=labels, color=colors, alpha=0.6 )
plt.axis('off')
plt.show()
``````

Output:

One part of it is to define our colors in order to acquire a clearer picture.

Colors can add another encoding; for example, if we had a corporation with multiple locations, we might want to know how much each one contributes to our total assets; the size of the squares in our treemap would reflect this.

We could then colormap those sites based on profit, production numbers, costs, or whatever criteria we wanted.

Let’s use a more accessible example to demonstrate the concept: let’s imagine we have four businesses spread across two locations. We can use various colors for different sections and comparable colors for places that are close together.

If you run the same piece of code again, you will get the following output :

You can see that the color scheme of our treemap is different each time we run it. The colors for rectangles are picked randomly. Treemap also gives you the option to mention the colors along with sizes and labels. We will learn how to change the colors of a treemap next.

### Example To Turn-off the plot axis

To plot the treemap without the plot-axis, use:

``plt.axis('off')``

The plot axis will be turned off using this line of code. The following is the whole code:

``````import matplotlib.pyplot as plt
import squarify
import pandas as pd

sqaure_sizes=[10, 30, 50, 200]
labels=["A", "B", "C", "D"]
colors=['red','blue','green','yellow']
squarify.plot(sizes=sqaure_sizes, label=labels, color=colors, alpha=0.7 )
plt.axis('off')
plt.show()
``````

Output:

## Conclusion

We learned how to use Squarify to plot a treemap in Python in this tutorial. I hope you enjoyed your time with us.