avatarRichard

Summary

The website provides a tutorial on creating a treemap visualization in Python using the Squarify, Matplotlib, and Seaborn libraries.

Abstract

The tutorial outlines the process of building a Python treemap visualization, starting with the installation of necessary libraries, including Squarify, Matplotlib, and Seaborn. It guides users through importing these libraries into a Python IDE, creating a basic treemap with dynamic colors, and then customizing the colors and labels for a more consistent and informative visualization. The tutorial also demonstrates how to use the Seaborn library to load and visualize datasets, using the Titanic dataset as an example. It concludes with references to further resources and a call to action for readers to engage with additional content and community platforms.

Opinions

  • The author emphasizes the importance of a consistent color scheme in treemap visualizations by demonstrating how to fix the color map.
  • The use of labels is suggested as a method to enhance the interpretability of the treemap rectangles.
  • The tutorial is designed for individuals who may not be familiar with the libraries, as it includes basic installation and import instructions.
  • The inclusion of a real-world dataset (Titanic) suggests that the author values practical application of the tutorial's content.
  • The references provided at the end of the tutorial indicate a supportive stance towards further learning and community engagement.

Build a Python Treemap Visualization by Using Squarify, Matplotlib & Seaborn

A tutorial on building a Python treemap visualization by using Squarify, Matplotlib, and Seaborn.

Photo by Hitesh Choudhary on Unsplash

Install the library

pip install squarify matplotlib seaborn

Once the installation is complete, open your Python IDE and import the library:

import squarify
import matplotlib.pyplot as plt
import seaborn as sns

If you do not encounter any error when importing the library, you’ve successfully installed and imported the Squarify library in Python. Now, you can move on to visualizing a treemap in Python.

Create a Treemap

import squarify

import matplotlib.pyplot as plt


squarify.plot(sizes=[60, 50, 6, 30])

plt.show()

output

Note that every time you re-run the code, you will get different colors for your plot. So, to fix the color map of your plot, please make sure to use the color parameter in the plot() method. You can pass a list of colors in the color parameter as shown below,

import squarify

import matplotlib.pyplot as plt


squarify.plot(sizes=[60, 50, 6, 30], color=["Red", "Blue", "Yellow", "Green"])

plt.show()

output

You can also assign different labels to the rectangles in your plot by using the label parameter in the plot() method. To do this, simply pass in a list of labels in the label parameter as shown below,

import squarify

import matplotlib.pyplot as plt


squarify.plot(sizes=[60, 50, 6, 30], color=["Red", "Blue", "Yellow", "Green"], label=["A", "B", "C", "D"])

plt.show()

output

Plotting a Python Treemap using the Seaborn library

If you do not have the library already installed, you can install it in your command terminal.

pip install seaborn

Now, let us start off by reading the Titanic dataset using the seaborn library

import seaborn as sns
import matplotlib.pyplot as plt
titanic = sns.load_dataset('titanic')
titanic.head()

output

References:

https://www.theclickreader.com/python-treemap-plot-a-treemap-using-python/

https://matplotlib.org/

https://github.com/laserson/squarify

https://seaborn.pydata.org/

Thanks for reading!

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Check out our Community Discord and join our Talent Collective.

Python
Programming
Coding
Python3
Data Science
Recommended from ReadMedium