5 Best Ways to Visualize a Treemap in Python Using Pygal

Rate this post

πŸ’‘ Problem Formulation: You need to represent hierarchical data visually, highlighting the relative sizes of the data points. For example, you have a dataset depicting sales figures for different product categories and subcategories, and you want to showcase these figures in an easy-to-understand treemap that emphasizes the proportions of each category to the overall sales.

Method 1: Creating a Basic Treemap with Pygal

Pygal offers a simple syntax for creating treemaps, which are visual representations showing hierarchical data with nested rectangles. Its Treemap() only requires you to pass a data series with nested items representing different hierarchy levels to illustrate the relative weight or size of these items.

Here’s an example:

import pygal

treemap = pygal.Treemap()
treemap.add('Product A', [2, 1, 12])
treemap.add('Product B', [4, 8, 3])

Output of this code snippet would be a treemap rendered in your browser, with differently sized rectangles representing the data for ‘Product A’ and ‘Product B’.

This code snippet demonstrates creating a simple treemap in Pygal by initializing a Treemap object and adding data to it. Each call to add() specifies a label for the data category and a list of values. The render_in_browser() function displays the treemap directly in your web browser.

Method 2: Using Custom Options for Styling

Custom styling in a Pygal treemap can significantly enhance readability and aesthetics, allowing you to set colors, labels, and tooltips for individual rectangles. You can pass a style argument to the Treemap object and use the built-in Style class to define colors and tooltip properties.

Here’s an example:

from pygal import Treemap, Style

custom_style = Style(colors=('#E853A0', '#E8537A', '#E95355'))
treemap = Treemap(style=custom_style)
treemap.add('Electronics', [2, 3, 7, 2])
treemap.add('Apparel', [5, 12])

Output of this script will be a stylized treemap with custom color palettes, which helps differentiate categories visually.

The example extends the visualization by implementing custom styles. By importing the Style class from Pygal and passing a tuple of hex color codes, we define a custom color palette. The resulting treemap is more visually engaging and can be used to match specific styling guidelines or themes.

Method 3: Adding Tooltips for Enhanced Interactivity

Pygal’s treemaps can include interactive features like tooltips that provide more information on hover. This is useful for presenting additional details without cluttering the visual space. Tooltip content can be customized with data labels and values.

Here’s an example:

import pygal
from pygal.style import CleanStyle

treemap = pygal.Treemap(tooltip_fancy_mode=False, style=CleanStyle)
treemap.add('Fruits', [{'value': 30, 'label': 'Banana'}, {'value': 21, 'label': 'Apple'}])
treemap.add('Vegetables', [{'value': 12, 'label': 'Carrot'}])

The tooltip will pop up with the label you have provided when you hover over each segment of the treemap.

This snippet shows how you can add interactive tooltips to a Pygal treemap. Each value is a dictionary with a ‘value’ and ‘label’, which Pygal uses to create tooltips that appear when hovering over each segment. This feature aids in the presentation of detailed information in an interactive format.

Method 4: Handling Multiple Levels of Hierarchy

For datasets with multiple levels of hierarchy, Pygal enables the creation of nested treemaps by accepting nested data lists. This is ideal for representing complex hierarchical structures, like organizational data or file systems.

Here’s an example:

import pygal

treemap = pygal.Treemap()
treemap.add('Food', [{'value': 35, 'label': 'Fruits', 'children': [{'value': 20, 'label': 'Apple'}, {'value': 15, 'label': 'Orange'}]},
                     {'value': 15, 'label': 'Vegetables', 'children': [{'value': 8, 'label': 'Tomato'}, {'value': 7, 'label': 'Pepper'}]}])

Output of this code will be a treemap with rectangles nested within rectangles, each representing a level in the hierarchy.

This code snippet covers the visualization of a hierarchical data structure that is more than one level deep. By using the ‘children’ attribute within the data dictionaries, Pygal renders a treemap with nested elements, making it possible to represent complex hierarchical information fluidly.

Bonus One-Liner Method 5: Quick Treemap with Inline Data

For swift visualization tasks, Pygal allows inline data specification in a one-liner format to quickly generate a treemapβ€”handy for simple datasets or preliminary data assessments.

Here’s an example:

import pygal; pygal.Treemap().add('Quick Data', [10, 20, 30, 40]).render_in_browser()

A simple treemap will immediately render in your browser, showing four rectangles of varying sizes.

This one-liner is the fastest way to get a visual representation of a dataset in Pygal. Despite its simplicity, it creates a fully-functional treemap, proving Pygal’s ease of use and versatility for quick tasks.


  • Method 1: Basic Treemap creation. Strengths: Easy to implement, fitting for simple representations. Weaknesses: Limited to basic styling and interactivity.
  • Method 2: Custom Styling. Strengths: Improved aesthetics, Enhanced legibility. Weaknesses: Requires more coding for styles customization.
  • Method 3: Adding Tooltips. Strengths: Provides additional information without clutter, enhances user interaction. Weaknesses: Tooltip setup might be complex for deeply nested data.
  • Method 4: Multiple Hierarchy Levels. Strengths: Capable of representing complex datasets, detailed visualization. Weaknesses: Can get visually complicated with too many nested levels.
  • Method 5: Quick Inline Data Treemap. Strengths: Excellent for rapid visualization. Weaknesses: Not suitable for detailed or hierarchical data.