Skip to content

Fix px scrambling categorical axis order when data is split into traces - #5776

Open
vineoy wants to merge 1 commit into
plotly:mainfrom
vineoy:px-category-order-3198
Open

vineoy wants to merge 1 commit into
plotly:mainfrom
vineoy:px-category-order-3198

Conversation

@vineoy

@vineoy vineoy commented Sep 27, 2026

Copy link
Copy Markdown

Link to issue

Fixes #3198, fixes #3291.

Description of change

When Plotly Express splits data into several traces (e.g. via \color), plotly.js orders categories by first appearance across traces (default \categoryorder). Because each trace only carries its own rows (None-padded), that order can differ from the order in the underlying data — contradicting the documented behavior that Plotly Express lays out categorical data in the order in which it appears in the data.

This change records the data-appearance order of string categorical \x/\y\ columns in \make_figure\ (new _default_category_orders_for_axes\ helper in \plotly/express/_core.py), but only when the data is actually split into more than one trace and the user did not supply an explicit \category_orders\ entry. The existing \set_cartesian_axis_opts\ then emits \categoryorder=array\ + \categoryarray, so:

Explicit user \category_orders, single-trace figures, and numeric/datetime axes are untouched.

Testing strategy

  • New regression tests in \ ests/test_optional/test_px/test_px.py: data-order assertion for Bug when using categorical axes and colors in plotly.express #3198 and Plotly Express doesn't force category orders on axes #3291 repros, plus a guard test that single-trace and numeric-axis figures get no \categoryarray.
  • \CHANGELOG.md\ entry added under Unreleased/Fixed.
  • Local runs: new tests pass; full \ est_px.py\ (331 passed; 3 \ est_render_mode\ failures are pre-existing missing-\statsmodels\ env gaps, verified identical on clean main); \ est_px_functions/\ est_facets/\ est_px_wide/\ est_px_input\ failure sets byte-identical to clean main (pre-existing env gaps: pie category order, pyarrow timezones, wide-mode cases); pinned
    uff==0.11.12 check\ clean.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Plotly Express doesn't force category orders on axes Bug when using categorical axes and colors in plotly.express

1 participant