Hierarchical category exploration
Exploring nested categories such as regions, countries, and products.
Plotly · Function
px.treemap creates interactive treemap charts for hierarchical data. Supply explicit names and parents, or use path to derive levels from dataframe columns.
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.
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)| Name | Type | Description |
|---|---|---|
data_frame | DataFrame or array-like | Dataframe or array-like source containing the hierarchical records. |
path | list of str or array-like | Ordered dataframe columns defining hierarchy levels. |
values | str or array-like | Numeric values used to size sectors. |
names | str or array-like | Node labels for an explicit hierarchy. |
parents | str or array-like | Parent labels for an explicit hierarchy. |
color | str or array-like | Column or values used to determine node coloring. |
A Plotly figure containing a treemap visualization.
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.
Exploring nested categories such as regions, countries, and products.
Showing each group’s contribution to a total with area.
Adding a continuous metric to reveal patterns across branches.
Navigating large hierarchies interactively.
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.
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.
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.sunburstplotly.express.icicleplotly.graph_objects.Treemap