Skip to content

Commit 2bc6a0f

Browse files
authored
Merge pull request #4877 from MarcoGorelli/fix-category-orders
fix: Setting category_orders was leading to missing data
2 parents 5898816 + b8b4d29 commit 2bc6a0f

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

Diff for: packages/python/plotly/plotly/express/_core.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -2422,7 +2422,6 @@ def get_groups_and_orders(args, grouper):
24222422
# figure out orders and what the single group name would be if there were one
24232423
single_group_name = []
24242424
unique_cache = dict()
2425-
grp_to_idx = dict()
24262425

24272426
for i, col in enumerate(grouper):
24282427
if col == one_group:
@@ -2440,27 +2439,28 @@ def get_groups_and_orders(args, grouper):
24402439
else:
24412440
orders[col] = list(OrderedDict.fromkeys(list(orders[col]) + uniques))
24422441

2443-
grp_to_idx = {k: i for i, k in enumerate(orders)}
2444-
24452442
if len(single_group_name) == len(grouper):
24462443
# we have a single group, so we can skip all group-by operations!
24472444
groups = {tuple(single_group_name): df}
24482445
else:
2449-
required_grouper = list(orders.keys())
2446+
required_grouper = [group for group in orders if group in grouper]
24502447
grouped = dict(df.group_by(required_grouper, drop_null_keys=True).__iter__())
2451-
sorted_group_names = list(grouped.keys())
24522448

2453-
for i, col in reversed(list(enumerate(required_grouper))):
2454-
sorted_group_names = sorted(
2455-
sorted_group_names,
2456-
key=lambda g: orders[col].index(g[i]) if g[i] in orders[col] else -1,
2457-
)
2449+
sorted_group_names = sorted(
2450+
grouped.keys(),
2451+
key=lambda values: [
2452+
orders[group].index(value) if value in orders[group] else -1
2453+
for group, value in zip(required_grouper, values)
2454+
],
2455+
)
24582456

24592457
# calculate the full group_names by inserting "" in the tuple index for one_group groups
24602458
full_sorted_group_names = [
24612459
tuple(
24622460
[
2463-
"" if col == one_group else sub_group_names[grp_to_idx[col]]
2461+
""
2462+
if col == one_group
2463+
else sub_group_names[required_grouper.index(col)]
24642464
for col in grouper
24652465
]
24662466
)

Diff for: packages/python/plotly/plotly/tests/test_optional/test_px/test_px.py

+21
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,27 @@ def test_orthogonal_orderings(backend, days, times):
289289
assert_orderings(backend, days, days, times, times)
290290

291291

292+
def test_category_order_with_category_as_x(backend):
293+
# https://github.com/plotly/plotly.py/issues/4875
294+
tips = nw.from_native(px.data.tips(return_type=backend))
295+
fig = px.bar(
296+
tips,
297+
x="day",
298+
y="total_bill",
299+
color="smoker",
300+
barmode="group",
301+
facet_col="sex",
302+
category_orders={
303+
"day": ["Thur", "Fri", "Sat", "Sun"],
304+
"smoker": ["Yes", "No"],
305+
"sex": ["Male", "Female"],
306+
},
307+
)
308+
assert fig["layout"]["xaxis"]["categoryarray"] == ("Thur", "Fri", "Sat", "Sun")
309+
for trace in fig["data"]:
310+
assert set(trace["x"]) == {"Thur", "Fri", "Sat", "Sun"}
311+
312+
292313
def test_permissive_defaults():
293314
msg = "'PxDefaults' object has no attribute 'should_not_work'"
294315
with pytest.raises(AttributeError, match=msg):

0 commit comments

Comments
 (0)