Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
5 changes: 5 additions & 0 deletions plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"] = (
Expand Down
44 changes: 44 additions & 0 deletions tests/test_optional/test_px/test_px_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down