Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Plotly · Function

px.treemap

px.treemap creates interactive treemap charts for hierarchical data. Supply explicit names and parents, or use path to derive levels from dataframe columns.

How it works

px.treemap creates an interactive treemap figure from hierarchical data. You can describe the hierarchy explicitly with names and parents, or derive it from dataframe columns with path; values control sector sizing, while color can encode aggregated measurements.

Syntax

plotly.express.treemap(data_frame=None, names=None, values=None, parents=None, ids=None, path=None, color=None, color_continuous_scale=None, color_continuous_midpoint=None, color_continuous能=None, range_color=None, color_discrete_sequence=None, color_discrete_map=None, hover_name=None, hover_data=None, custom_data=None, labels=None, title=None, template=None, width=None, height=None)

Parameters

NameTypeDescription
data_frameDataFrame or array-likeDataframe or array-like source containing the hierarchical records.
pathlist of str or array-likeOrdered dataframe columns defining hierarchy levels.
valuesstr or array-likeNumeric values used to size sectors.
namesstr or array-likeNode labels for an explicit hierarchy.
parentsstr or array-likeParent labels for an explicit hierarchy.
colorstr or array-likeColumn or values used to determine node coloring.

Returns

A Plotly figure containing a treemap visualization.

Key points

  • The chart represents hierarchy through nested rectangular sectors.
  • Dataframe rows are converted into treemap sectors.
  • Use path with an ordered list of dataframe columns to derive hierarchy levels.
  • Color summarizes children using a value-weighted average.
  • Selecting a sector supports zooming and reveals the navigation pathbar.

Common mistakes

  • Do not combine path with id or parent inputs.
  • A path hierarchy works best when its first element identifies one root node.
  • Use None for absent levels in an irregular rectangular dataset.

Example: Build a treemap from parent links

import plotly.express as px

fig = px.treemap(
    names=["Eve", "Cain", "Seth", "Enos"],
    parents=["", "Eve", "Eve", "Seth"]
)
fig.show()

Creates an interactive treemap from explicit node names and parent relationships.

When to use it

Hierarchical category exploration

Exploring nested categories such as regions, countries, and products.

Part-to-whole comparison

Showing each group’s contribution to a total with area.

Metric encoding with color

Adding a continuous metric to reveal patterns across branches.

Drill-down analysis

Navigating large hierarchies interactively.

Interview questions

QWhat does px.treemap produce, and how can a hierarchy be specified?

The function returns a Plotly figure representing hierarchical data as nested rectangles. With explicit data, names define node labels and parents define each node’s parent.

Follow-up: How would you build the hierarchy from dataframe columns? Pass the columns in order through path, rather than constructing names and parents manually.

QHow does path simplify treemaps from tabular data?

Each dataframe row becomes a treemap sector when using px.treemap. The path argument maps successive dataframe columns to hierarchy levels, while values determines the quantitative sizing of sectors.

Follow-up: What restriction applies to path? Do not also provide id or parent when path is used.

QHow is color aggregated in a hierarchical treemap?

When color is supplied, a node’s color is calculated from its children as a value-weighted average of their color values. This lets color summarize a metric across the hierarchy rather than merely label categories.

Follow-up: What data-quality precaution improves the result? Ensure the first path element is a single root node.

  • plotly.express.sunburst
  • plotly.express.icicle
  • plotly.graph_objects.Treemap

Source: Treemap Charts View Tutorial. Checked 2026-10-15.