Skip to content

Commit 74726e0

Browse files
PX val_map now respects category_orders
1 parent bca2925 commit 74726e0

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,7 @@ def get_orderings(args, grouper, grouped):
18381838
if col not in orders:
18391839
orders[col] = list(uniques)
18401840
else:
1841+
orders[col] = list(orders[col])
18411842
for val in uniques:
18421843
if val not in orders[col]:
18431844
orders[col].append(val)
@@ -1849,7 +1850,6 @@ def get_orderings(args, grouper, grouped):
18491850
group_names,
18501851
key=lambda g: orders[col].index(g[i]) if g[i] in orders[col] else -1,
18511852
)
1852-
18531853
return orders, group_names, group_values
18541854

18551855

@@ -1877,17 +1877,19 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
18771877

18781878
col_labels = []
18791879
row_labels = []
1880-
18811880
for m in grouped_mappings:
1882-
if m.grouper:
1881+
if m.grouper not in sorted_group_values:
1882+
m.val_map[""] = m.sequence[0]
1883+
else:
1884+
sorted_values = orders[m.grouper]
18831885
if m.facet == "col":
18841886
prefix = get_label(args, args["facet_col"]) + "="
1885-
col_labels = [prefix + str(s) for s in sorted_group_values[m.grouper]]
1887+
col_labels = [prefix + str(s) for s in sorted_values]
18861888
if m.facet == "row":
18871889
prefix = get_label(args, args["facet_row"]) + "="
1888-
row_labels = [prefix + str(s) for s in sorted_group_values[m.grouper]]
1889-
for val in sorted_group_values[m.grouper]:
1890-
if val not in m.val_map:
1890+
row_labels = [prefix + str(s) for s in sorted_values]
1891+
for val in sorted_values:
1892+
if val not in m.val_map: # always False if it's an IdentityMap
18911893
m.val_map[val] = m.sequence[len(m.val_map) % len(m.sequence)]
18921894

18931895
subplot_type = _subplot_type_for_trace_type(constructor().type)
@@ -1943,8 +1945,6 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
19431945

19441946
for i, m in enumerate(grouped_mappings):
19451947
val = group_name[i]
1946-
if val not in m.val_map:
1947-
m.val_map[val] = m.sequence[len(m.val_map) % len(m.sequence)]
19481948
try:
19491949
m.updater(trace, m.val_map[val]) # covers most cases
19501950
except ValueError:

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

+7-10
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ def test_px_defaults():
209209

210210

211211
def assert_orderings(days_order, days_check, times_order, times_check):
212-
symbol_sequence = ["circle", "diamond", "square", "cross"]
213-
color_sequence = ["red", "blue"]
212+
symbol_sequence = ["circle", "diamond", "square", "cross", "circle", "diamond"]
213+
color_sequence = ["red", "blue", "red", "blue", "red", "blue", "red", "blue"]
214214
fig = px.scatter(
215215
px.data.tips(),
216216
x="total_bill",
@@ -229,7 +229,7 @@ def assert_orderings(days_order, days_check, times_order, times_check):
229229
assert days_check[col] in trace.hovertemplate
230230

231231
for row in range(len(times_check)):
232-
for trace in fig.select_traces(row=2 - row):
232+
for trace in fig.select_traces(row=len(times_check) - row):
233233
assert times_check[row] in trace.hovertemplate
234234

235235
for trace in fig.data:
@@ -241,13 +241,10 @@ def assert_orderings(days_order, days_check, times_order, times_check):
241241
assert trace.marker.color == color_sequence[i]
242242

243243

244-
def test_noisy_orthogonal_orderings():
245-
assert_orderings(
246-
["x", "Sun", "Sat", "y", "Fri", "z"], # add extra noise, missing Thur
247-
["Sun", "Sat", "Fri", "Thur"], # Thur is at the back
248-
["a", "Lunch", "b"], # add extra noise, missing Dinner
249-
["Lunch", "Dinner"], # Dinner is at the back
250-
)
244+
@pytest.mark.parametrize("days", permutations(["Sun", "Sat", "Fri", "x"]))
245+
@pytest.mark.parametrize("times", permutations(["Lunch", "x"]))
246+
def test_orthogonal_and_missing_orderings(days, times):
247+
assert_orderings(days, list(days) + ["Thur"], times, list(times) + ["Dinner"])
251248

252249

253250
@pytest.mark.parametrize("days", permutations(["Sun", "Sat", "Fri", "Thur"]))

0 commit comments

Comments
 (0)