In data visualization Treemap is a pictorial representation of hierarchical data using nested rectangle figures. Treemap uses colours to distinguish between different data dimensions being visualized. To divide each region into sub-regions Treemap uses a tiling algorithm. Python provides us with library called squirify that we can use to create treemaps. In this post we will look at treemaps, when to use them and how to create Treemap in Matplotlib. ## When to Use Treemap

1. Treemaps are used to visualize hierarchical data.
2. They are suitable in showing the part-to-whole relationship for large groups.
3. Treemaps are ideal in cases where there is no enough space for multiple visualizations.

## When to Use Treemap

1. Works well when there are fewer categories.
2. The values needs to be positive.
3. Select colours that blend well with each group and other dashboard components.

## Treemap in Matplotlib

Before we start creating Treemaps we need to install a Python library called squarify. Use the command below to install squarify.

```                    ```
pip install squarify
```
```

Create the DataFrame

```                    ```
import pandas as pd
import numpy as np
import squarify

plt.style.use('ggplot') # Other sytles to use; fivethirtyeight

salary_df = pd.DataFrame(
{
"Staff": ["Tom", "Peter","Simon", "Mary", "Jane"],
"Salary": [90000.00, 57000.00,40000.00, 34000.00, 12000.00]
},
)

salary_df
```
``` Create Treemap

```                    ```
plt.figure(figsize=(20,15)) # Set figure size
color = ['red', 'green', 'skyblue', 'orange','magenta']
squarify.plot(salary_df['Salary'], label = salary_df['Staff'], color=color, pad = True, alpha=.8)
plt.axis('off')
plt.show()
```
``` For complete code check the jupyter notebook here.

## Conclusion

Treemaps have many use-cases in data analytics. They are primarily used to visualize hierarchical data. Treemaps are suitable to present data in cases where there is no space for many visualizations but need to communicate insights to users. In this post we have looked at Treemaps, when to use them and how to create them in Matplotlib. In the next post we will look at how to export and share visualisations created in Matplotlib. To learn about Heatmap check our previous post here.

Treemap in Matplotlib 