A Visual Feast: Creating Stunning Treemaps in Python using Squarify

Luchiana Dumitrescu
Women in Technology
5 min readDec 3, 2023

In the large and wide data visualization realm, a suitable tool is necessary to handle hierarchies. For this crucial aspect, we have treemaps. This formidable graph not only captures the complexity of the hierarchical structure but also offers a stunning way to visualize and interpret the data.

🕵️‍♀️ Without delay, let’s plunge into the world of treemaps — a potent visual representation that wastes no time in unraveling complex hierarchies.

What is a treemap?

A treemap is an alternative way of visualizing the hierarchical structure and displaying, at the same time, the quantities for each category using the area size.

The representation consists of a series of rectangles that symbolize various categories within a specific dimension; each rectangle represents categories in a dimension that helps us compare patterns and display them in a limited space in our dashboards.

When to use a Treemap?

Treemaps, a data visualization technique recommended by Tableau, are particularly useful when we need to represent a substantial number of interrelated categories. By using treemaps, we can create an informative and visually compelling representation of complex data with ease.

Unlike traditional bar charts or pie graphs, treemaps utilize nested rectangles to represent levels of hierarchy, offering a comprehensive and intuitive overview of complex relationships within your data.

Now that we know what is and when to use a treemap, let's explore Squarify, a Python library, together, and create a stunning treemap 🌳

Who is this Squarify? 🤔

Squarify: Hi! I heard my name, so what’s up?

Me: Well, the people want to meet you and get to know you better. Would you like to introduce yourself, please?

Squarify: Sure! So I’ll take the wheel from here 😁

Hello again! My name is Squarify and i’m one of the most powerful Python libraries when it comes to creating treemaps. I heard that Luchiana already told you what treemaps are, so let’s dive into the magic of creating treemaps with me. I’ll show you just how easy and powerful it is to visualize hierarchical data. Ready? Let’s roll!

Last week, Luchiana had her hands full with an EDA project in Python, and, of course, she called me to lend a hand. It was about a crime dataset she obtained from LA police (she got it from Kaggle, but please don’t tell her that I told you that, she put a lot of effort into coming up with a scenario for her project).

We worked together to find the relation between the highest number of reported cases and the area where they occurred; in just a few lines of code, i created an amazing treemap (see it👇)

Curious about how i did it? Let me explain then:

  1. Import my friends & me — this way i can put my skills in your hands
import matplotlib.pyplot as plt
import seaborn as sns
import squairfy as sqrf

2. Give me your data — we must start somewhere, right?

sqrf.plot(sizes=df_cases_by_area['Reported Cases'])

3. Want something more customizable? — That’s why i’m here

To help you create something eye-catching, i provide you few arguments to achieve what you need to impress:

  • no axis — first of all, I don’t need an axis to work, so if you’d like, you can get help from my friend Matplotlib to remove it by writing the following line of code:
plt.axis("off")
  • padding — adding some space between the tiles will make your treemap more readable; all you have to do is add pad argument to the line above, like so:
sqrf.plot(sizes=df_cases_by_area['Reported Cases'],
pad = 0.25)

In her project, Luchiana didn’t use this argument, but here’s what it would look like

  • border — it’s better when you delimit things, right? Use the ec attribute to do so:
sqrf.plot(sizes=df_cases_by_area['Reported Cases'],
ec= 'black')
  • axis scaling — by default i come with a size of 100x100, but you can change this by using norm_x and norm_y arguments:
sqrf.plot(sizes=df_cases_by_area['Reported Cases'], 
norm_x = 200,
norm_y = 20)

To see the axes, remember to remove plt.axis(“off”).

  • color palettes — what’s life without some colors? I can provide you with some gorgeous color pallets; just add color argument (my friend Seaborn give me those beautiful colors)
sqrf.plot(sizes=df_cases_by_area['Reported Cases'],
color=sns.color_palette('viridis',n_colors=len(df_cases_by_area['Area'])))
  • labels — making your treemap intuitive and easy to understand is still crucial. You can achieve this by using the label argument:
labels = [f'{area}\n{reported_cases}' for area, 
reported_cases in zip(df_cases_by_area.Area,
df_cases_by_area['Reported Cases'])]
sqrf.plot(sizes=df_cases_by_area['Reported Cases'],
label=labels)

In her case, Luchiana wanted to display both the name of the area and the number of reported cases for each area.

Moreover, you can customize the labels by playing with fontsize, color, fontweight; just add text_kwargs argument and the magic will happen:

sqrf.plot(sizes=df_cases_by_area['Reported Cases'],
text_kwargs={'color': 'white','fontweight': 'bold', 'fontsize':8}

In Luchiana’s case the final script looks like the one below:

df_cases_by_area = df.groupby('Area Name')['ID'].count().reset_index()
df_cases_by_area.columns=['Area', 'Reported Cases']
df_cases_by_area = df_cases_by_area.sort_values(by='Reported Cases', ascending= False)

labels = [f'{area}\n{reported_cases}' for area, reported_cases in zip(df_cases_by_area.Area, df_cases_by_area['Reported Cases'])]

sqrf.plot(sizes=df_cases_by_area['Reported Cases'], label=labels ,text_kwargs={'color': 'white','fontweight': 'bold', 'fontsize':8},color=sns.color_palette('viridis', n_colors=len(df_cases_by_area['Area'])))

plt.axis('off')
plt.title('Reported Crime Incidents by Area')
plt.show()

and the treemap looks like this:

Stunning, right?

That was all, my friends! I hope you enjoyed this meeting and don’t forget if you ever find yourself in need of a treemap maestro, you know where to find me! 👋

Conclusion

Treemaps, with their nested rectangles representing categories within dimensions, offer a nuanced perspective on data relationships that go beyond traditional charts.

Keep Squarify in mind as a companion ready to transform your datasets into visually compelling narratives. Whether you’re exploring crime data or any other domain, the combination of treemaps and Squarify opens doors to a deeper understanding of your data.

Till next time, me and Squarify wish you happy coding, and may your data visualizations be as stunning as the treemap we just crafted, or even more than this. Cheers!

P.S.: If you found this article interesting and helpful, you can buy me a magic elixir named coffee here.

P.P.S.: The full version of the Python project about LA Crimes can be found here.

Thank you a latte for your magic beans! 🤗☕💓

--

--

Luchiana Dumitrescu
Women in Technology

I'm a BI Developer, bookworm, writer, and pet lover with a huge passion for coffee and data. Let's have fun exploring the world of data together! 🔎📈😊