Skip to content

Commit d6ddc2a

Browse files
committed
Scope default category order to x-axis only
Address review on #5776: y categoricals stay untouched so the autorange=reversed pattern from doc/python/gantt.md keeps listing tasks top-down instead of double-reversing. Adds guard test that px.timeline and horizontal px.bar with color emit no y categoryarray, while x data-order fix for #3198/#3291 is kept.
1 parent 85cb798 commit d6ddc2a

3 files changed

Lines changed: 39 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77
### Fixed
88
- Fix concurrent first access to lazily initialized graph object properties, which could raise `ValueError("Invalid value")` [[#3441](https://github.com/plotly/plotly.py/issues/3441)], with thanks to @hb1915 for the contribution!
99
- Fix `px.sunburst`, `px.treemap` and `px.icicle` listing sectors in a different order on every run when `path` is used with a Polars DataFrame; sectors now follow their order of first appearance for all dataframe backends [[#5765](https://github.com/plotly/plotly.py/issues/5765)], with thanks to @Irahan2 for the contribution!
10-
- Fix Plotly Express scrambling the order of categorical axes when the data is split into several traces (e.g. by `color`); the axis now follows the order of first appearance in the underlying data [[#3198](https://github.com/plotly/plotly.py/issues/3198), [#3291](https://github.com/plotly/plotly.py/issues/3291)]
10+
- Fix Plotly Express scrambling the order of the categorical x-axis when the data is split into several traces (e.g. by `color`); the x-axis now follows the order of first appearance in the underlying data [[#3198](https://github.com/plotly/plotly.py/issues/3198), [#3291](https://github.com/plotly/plotly.py/issues/3291)]
1111

1212

1313
## [7.1.0] - 2026-09-15

‎plotly/express/_core.py‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2531,31 +2531,36 @@ def get_groups_and_orders(args, grouper):
25312531

25322532
def _default_category_orders_for_axes(args, groups, orders):
25332533
"""
2534-
Add data-appearance orders for categorical x/y columns.
2534+
Add data-appearance order for the categorical x column.
25352535
25362536
When the data is split into several traces (e.g. by `color`), plotly.js
25372537
orders categories by first appearance across traces, which can differ from
25382538
the order in the underlying data. Recording the data-appearance order here
25392539
lets `set_cartesian_axis_opts` emit an explicit `categoryarray`, matching
25402540
the documented behavior that Plotly Express lays out categorical data in
25412541
the order in which it appears in the data. See #3198.
2542+
2543+
Only the x axis is handled: y categoricals are intentionally left alone
2544+
because `set_cartesian_axis_opts` reverses y orders for top-down display
2545+
and figures such as `px.timeline` / horizontal `px.bar` pair that with
2546+
`autorange="reversed"` (see `doc/python/gantt.md`), so an explicit y
2547+
`categoryarray` would double-reverse and flip the documented order.
25422548
"""
25432549
if len(groups) <= 1:
25442550
return orders
25452551
df = args["data_frame"]
2546-
for letter in ("x", "y"):
2547-
col = args.get(letter)
2548-
if not isinstance(col, str) or col in orders or col not in df.columns:
2549-
continue
2550-
uniques = df.get_column(col).unique(maintain_order=True).to_list()
2551-
categories = [
2552-
value
2553-
for value in uniques
2554-
if value is not None
2555-
and not (isinstance(value, float) and math.isnan(value))
2556-
]
2557-
if categories and all(isinstance(value, str) for value in categories):
2558-
orders[col] = categories
2552+
col = args.get("x")
2553+
if not isinstance(col, str) or col in orders or col not in df.columns:
2554+
return orders
2555+
uniques = df.get_column(col).unique(maintain_order=True).to_list()
2556+
categories = [
2557+
value
2558+
for value in uniques
2559+
if value is not None
2560+
and not (isinstance(value, float) and math.isnan(value))
2561+
]
2562+
if categories and all(isinstance(value, str) for value in categories):
2563+
orders[col] = categories
25592564
return orders
25602565

25612566

‎tests/test_optional/test_px/test_px.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,25 @@ def test_no_default_category_array_for_single_trace_or_numeric():
393393
assert fig["layout"]["xaxis"]["categoryorder"] is None
394394

395395

396+
def test_no_default_category_array_for_y_with_color():
397+
# https://github.com/plotly/plotly.py/pull/5776#pullrequestreview-5478271667
398+
# y categoricals must stay untouched so the `autorange="reversed"`
399+
# pattern from doc/python/gantt.md keeps listing tasks top-down.
400+
df = pd.DataFrame(
401+
[
402+
dict(Task="Job A", Start="2009-01-01", Finish="2009-02-28", Resource="Alex"),
403+
dict(Task="Job B", Start="2009-03-05", Finish="2009-04-15", Resource="Alex"),
404+
dict(Task="Job C", Start="2009-02-20", Finish="2009-05-30", Resource="Max"),
405+
]
406+
)
407+
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color="Resource")
408+
assert fig["layout"]["yaxis"]["categoryorder"] is None
409+
assert fig["layout"]["yaxis"]["categoryarray"] is None
410+
fig = px.bar(x=[1, 2, 3, 4], y=list("abcd"), color=list("ppqq"), orientation="h")
411+
assert fig["layout"]["yaxis"]["categoryorder"] is None
412+
assert fig["layout"]["yaxis"]["categoryarray"] is None
413+
414+
396415
def test_permissive_defaults():
397416
msg = "'PxDefaults' object has no attribute 'should_not_work'"
398417
with pytest.raises(AttributeError, match=msg):

0 commit comments

Comments
 (0)