Skip to content

Commit b2b0282

Browse files
committed
fix: custom category order was hard-coded
1 parent 5e079e1 commit b2b0282

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2159,11 +2159,11 @@ def process_dataframe_pie(args, trace_patch):
21592159
uniques = df.get_column(names).unique(maintain_order=True).to_list()
21602160
order = [x for x in OrderedDict.fromkeys(list(order_in) + uniques) if x in uniques]
21612161

2162-
# Sort args['data_frame'] by column 'b' according to order `order`.
2162+
# Sort args['data_frame'] by column `names` according to order `order`.
21632163
token = nw.generate_temporary_column_name(8, df.columns)
21642164
args["data_frame"] = (
21652165
df.with_columns(
2166-
nw.col("b")
2166+
nw.col(names)
21672167
.replace_strict(order, range(len(order)), return_dtype=nw.UInt32)
21682168
.alias(token)
21692169
)

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

+23
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,29 @@ def test_pie_like_px():
5656
_compare_figures(trace, fig)
5757

5858

59+
def test_pie_custom_category_order(constructor):
60+
# https://github.com/plotly/plotly.py/issues/4999
61+
df = constructor(
62+
{
63+
"status": ["On Route", "Pending", "Waiting Result", "Delivered"],
64+
"count": [28, 10, 73, 8],
65+
}
66+
)
67+
custom_order = ["Pending", "Waiting Result", "On Route", "Delivered"]
68+
result = px.pie(
69+
data_frame=df,
70+
values="count",
71+
names="status",
72+
category_orders={"status": custom_order},
73+
).to_dict()
74+
assert list(result["data"][0]["labels"]) == [
75+
"Pending",
76+
"Waiting Result",
77+
"On Route",
78+
"Delivered",
79+
]
80+
81+
5982
def test_sunburst_treemap_colorscales():
6083
labels = ["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"]
6184
parents = ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"]

0 commit comments

Comments
 (0)