Treemap API#
Bases: AnyWidget
Zoomable hierarchical treemap.
Click a rectangle to zoom into its subtree; click the breadcrumb bar
above the chart to zoom back out. Parent rectangles reserve a header
strip for their name and aggregated value; children render nested
inside, up to max_depth levels below the current view.
Values on each leaf can be a single number, or a dict of {column: number}
for multi-column data. In the multi-column case, pass value_col to pick
which column drives rectangle sizing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Mapping[str, Any] | None
|
Hierarchy dict. |
None
|
width
|
int | str
|
Chart width. Either an integer in pixels, or a CSS length
string like |
600
|
height
|
int
|
Chart height in pixels. |
400
|
max_depth
|
int
|
How many nesting levels to render below the current
zoom (default |
3
|
value_col
|
str | None
|
When the tree has dict values, the column that drives rectangle sizing. Ignored for scalar values. |
None
|
format
|
Callable[[float], str] | None
|
Optional callable |
None
|
Examples:
from wigglystuff import Treemap
Treemap.from_paths(
{
"analytics/cluster/Agg": {"hours": 10, "count": 5},
"analytics/graph/Shortest": {"hours": 6, "count": 2},
"animate/Easing": {"hours": 4, "count": 8},
},
value_col="hours",
)
Source code in wigglystuff/treemap.py
from_dataframe
classmethod
#
from_dataframe(
df: Any,
*,
path_cols: Sequence[str],
value_cols: str | Sequence[str] | None = None,
root_name: str = "root",
**kwargs: Any
) -> "Treemap"
Build a Treemap from a pandas or polars dataframe.
Examples:
import pandas as pd
from wigglystuff import Treemap
df = pd.DataFrame(
{
"team": ["analytics", "analytics", "animate"],
"project": ["cluster", "graph", "easing"],
"hours": [10, 6, 4],
}
)
Treemap.from_dataframe(
df,
path_cols=["team", "project"],
value_cols="hours",
)
Source code in wigglystuff/treemap.py
from_paths
classmethod
#
from_paths(
mapping: Mapping[str, Any],
*,
sep: str = "/",
root_name: str = "root",
**kwargs: Any
) -> "Treemap"
Build a Treemap from a mapping of path strings to leaf values.
Examples:
from wigglystuff import Treemap
Treemap.from_paths(
{
"analytics/cluster/Agg": {"hours": 10, "count": 5},
"analytics/graph/Shortest": {"hours": 6, "count": 2},
"animate/Easing": {"hours": 4, "count": 8},
},
value_col="hours",
)
Source code in wigglystuff/treemap.py
from_records
classmethod
#
from_records(
records: Iterable[Mapping[str, Any]],
*,
path_cols: Sequence[str],
value_cols: str | Sequence[str] | None = None,
root_name: str = "root",
**kwargs: Any
) -> "Treemap"
Build a Treemap from an iterable of record dicts.
Examples:
from wigglystuff import Treemap
Treemap.from_records(
[
{"team": "analytics", "project": "cluster", "hours": 10},
{"team": "analytics", "project": "graph", "hours": 6},
{"team": "animate", "project": "easing", "hours": 4},
],
path_cols=["team", "project"],
value_cols="hours",
)
Source code in wigglystuff/treemap.py
Synced traitlets#
| Traitlet | Type | Notes |
|---|---|---|
data |
dict |
Hierarchy {name, value?, children?}. Leaf value is a number or {column: number}. |
width |
int \| str |
Chart width in pixels, or a CSS length like "100%". |
height |
int |
Chart height in pixels. |
max_depth |
int |
How many levels below the current zoom to draw. |
value_col |
str |
When leaves carry dicts, the column that drives rectangle sizing. |
selected_path |
list[str] |
Breadcrumb path of the currently-zoomed node. |
clicked_path |
list[str] |
Path of the most recently clicked node (fires for leaves too). |
Alternate constructors#
Treemap.from_paths(mapping, sep="/", root_name="root")— build from{path_string: value}.Treemap.from_records(records, path_cols, value_cols=None, root_name="root")— build from a list of record dicts.value_colsacceptsstr, a list of names (controlling column order), orNoneto auto-detect every numeric field.Treemap.from_dataframe(df, path_cols, value_cols=None, root_name="root")— build from a pandas or polars dataframe. Samevalue_colssemantics.