From c79a09d483a45c2d83c3b5c7a092c2cc96302560 Mon Sep 17 00:00:00 2001 From: Avalon2222 Date: Tue, 24 Nov 2026 20:08:13 +0700 Subject: [PATCH 1/2] Fix px hierarchy charts with a categorical color column --- plotly/express/_core.py | 5 +++ .../test_px/test_px_functions.py | 44 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/plotly/express/_core.py b/plotly/express/_core.py index 3b447f7c02..81c057c419 100644 --- a/plotly/express/_core.py +++ b/plotly/express/_core.py @@ -2042,6 +2042,11 @@ def process_dataframe_hierarchy(args): if args["color"]: if discrete_color: + # max() is not defined for unordered categoricals, and "(?)" is not + # one of the categories, so aggregate categorical colors as strings. + color_dtype = df.collect_schema()[args["color"]] + if isinstance(color_dtype, (nw.Categorical, nw.Enum)): + df = df.with_columns(nw.col(args["color"]).cast(nw.String())) discrete_aggs.append(args["color"]) agg_f[args["color"]] = nw.col(args["color"]).max() agg_f[f"{args['color']}{n_unique_token}"] = ( diff --git a/tests/test_optional/test_px/test_px_functions.py b/tests/test_optional/test_px/test_px_functions.py index 84a451733b..64b04a8527 100644 --- a/tests/test_optional/test_px/test_px_functions.py +++ b/tests/test_optional/test_px/test_px_functions.py @@ -375,6 +375,50 @@ def test_sunburst_treemap_with_path_color(constructor): assert np.all(np.sort(colors[:8]) == sorted(calls)) +@pytest.mark.parametrize("px_fn", [px.sunburst, px.treemap, px.icicle]) +def test_sunburst_treemap_with_path_categorical_color(constructor, px_fn): + if _pandas_version_at_least("3.0.0") and constructor == pandas_pyarrow_constructor: + pytest.skip( + "known issue with pandas 3 + pandas_pyarrow_constructor + px hierarchy charts (https://github.com/plotly/plotly.py/issues/5571)" + ) + + data = dict( + regions=["North", "North", "South", "South"], + vendors=["A", "B", "C", "D"], + sectors=["Tech", "Tech", "Finance", "Tech"], + values=[1, 2, 3, 4], + ) + df = nw.from_native(constructor(data)) + expected = px_fn( + df.to_native(), path=["regions", "vendors"], values="values", color="sectors" + ).data[0] + + # A categorical color column used to raise on max() / "(?)" in the group_by + df = df.with_columns(nw.col("sectors").cast(nw.Categorical())) + fig = px_fn( + df.to_native(), path=["regions", "vendors"], values="values", color="sectors" + ) + assert list(fig.data[0].ids) == list(expected.ids) + assert list(fig.data[0].marker.colors) == list(expected.marker.colors) + + +def test_sunburst_treemap_with_path_enum_color(): + pl = pytest.importorskip("polars") + df = pl.DataFrame( + dict( + regions=["North", "North", "South", "South"], + vendors=["A", "B", "C", "D"], + sectors=["Tech", "Tech", "Finance", "Tech"], + ) + ) + expected = px.sunburst(df, path=["regions", "vendors"], color="sectors").data[0] + + df = df.with_columns(pl.col("sectors").cast(pl.Enum(["Tech", "Finance"]))) + fig = px.sunburst(df, path=["regions", "vendors"], color="sectors") + assert list(fig.data[0].ids) == list(expected.ids) + assert list(fig.data[0].marker.colors) == list(expected.marker.colors) + + def test_sunburst_treemap_column_parent(constructor): if _pandas_version_at_least("3.0.0") and constructor == pandas_pyarrow_constructor: pytest.skip( From e1687881d8fa993623c69caf90baa2d0ec3825f4 Mon Sep 17 00:00:00 2001 From: Avalon2222 Date: Tue, 24 Nov 2026 20:15:23 +0700 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56bb878e29..8c867a0f03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Fix `trendline_scope="overall"` drawing the overall trendline on the `marginal_x`/`marginal_y` subplots as well as the main plot; it is now drawn only on the main subplots [[#5780](https://github.com/plotly/plotly.py/pull/5780)], with thanks to @jayzhou2309 for the contribution! - Fix `add_hline`, `add_vline`, `add_hrect` and `add_vrect` dropping the shape on an empty subplot when `row` and `col` select a single subplot [[#5871](https://github.com/plotly/plotly.py/pull/5871)], with thanks to @LukeTheoJohnson for the contribution! - Fix `mpl_to_plotly` conversion of matplotlib contour lines and line collections: close contour rings ending with `Z` codes, support dash styles, convert date x-axes, separate disjoint subpaths with `None` separators, hide line collection traces from the legend by default, and group consecutive same-style lines into single traces [[#5770](https://github.com/plotly/plotly.py/pull/5770)], with thanks to @robertoffmoura for the contribution! +- Fix `px.sunburst`, `px.treemap` and `px.icicle` raising an error when `path` is used and `color` is a categorical column (pandas `category`, Polars `Enum` or PyArrow dictionary) [[#5901](https://github.com/plotly/plotly.py/pull/5901)], with thanks to @Avalon2222 for the contribution! - Fix `plotly.io.write_images` ignoring `layout.width`/`layout.height` (and the template's) and exporting every figure at the default 700x500 size; it now uses the same width/height priority as `write_image` and `to_image` [[#5784](https://github.com/plotly/plotly.py/pull/5784)], with thanks to @breken-ai for the contribution! - Fix color utilities mutating the list of colors passed to them [[#5836](https://github.com/plotly/plotly.py/pull/5836)], with thanks to @Enesuygurs for the contribution! - Fix Plotly Express (including `px.imshow`) ignoring `facet_row_spacing=0` and `facet_col_spacing=0` and using the default spacing between facets instead [[#5881](https://github.com/plotly/plotly.py/pull/5881)], with thanks to @rishi02102017 for the contribution!