Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: custom category order was hard-coded #5000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
emilykl merged 2 commits into plotly:master from MarcoGorelli:hardcoded-var
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/python/plotly/plotly/express/_core.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2147,6 +2147,8 @@ def process_dataframe_timeline(args):


def process_dataframe_pie(args, trace_patch):
import numpy as np

names = args.get("names")
if names is None:
return args, trace_patch
Expand All @@ -2159,12 +2161,12 @@ def process_dataframe_pie(args, trace_patch):
uniques = df.get_column(names).unique(maintain_order=True).to_list()
order = [x for x in OrderedDict.fromkeys(list(order_in) + uniques) if x in uniques]

# Sort args['data_frame'] by column 'b' according to order `order`.
# Sort args['data_frame'] by column `names` according to order `order`.
token = nw.generate_temporary_column_name(8, df.columns)
args["data_frame"] = (
df.with_columns(
nw.col("b")
.replace_strict(order, range(len(order)), return_dtype=nw.UInt32)
nw.col(names)
.replace_strict(order, np.arange(len(order)), return_dtype=nw.UInt32)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcoGorelli What's the reason for using np.arange here rather than range? Is there a performance difference?

Copy link
Contributor Author

@MarcoGorelli MarcoGorelli Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, @FBruzzesi had noted that it was generally more performant, and numpy is still required in plotly express anyway - no objections to changing this back of course!

emilykl reacted with thumbs up emoji
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, makes sense to me, thanks!

.alias(token)
)
.sort(token)
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,39 @@ def test_pie_like_px():
_compare_figures(trace, fig)


def test_pie_custom_category_order(constructor):
# https://github.com/plotly/plotly.py/issues/4999
data = {
"status": ["On Route", "Pending", "Waiting Result", "Delivered"],
"count": [28, 10, 73, 8],
}
df = constructor(data)
custom_order = ["Pending", "Waiting Result", "On Route", "Delivered"]
result = px.pie(
data_frame=df,
values="count",
names="status",
category_orders={"status": custom_order},
)
assert list(result.to_dict()["data"][0]["labels"]) == [
"Pending",
"Waiting Result",
"On Route",
"Delivered",
]
values_ = np.array(
[
x[0]
for x in sorted(
zip(data["count"], data["status"]),
key=lambda t: custom_order.index(t[1]),
)
]
)
trace = go.Pie(values=values_, labels=custom_order)
_compare_figures(trace, result)

Copy link
Contributor

@FBruzzesi FBruzzesi Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was doing a similar fix 🙈

You could also add the comparison with go.Pie:

 values_ = np.array([
 x[0] for x in sorted(zip(data["count"], data["status"]), key=lambda t: custom_order.index(t[1]))
 ])
 trace = go.Pie(
 values=values_,
 labels=custom_order,
 )
 _compare_figures(trace, fig)

MarcoGorelli reacted with thumbs up emoji MarcoGorelli reacted with heart emoji
Copy link
Contributor Author

@MarcoGorelli MarcoGorelli Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, thanks for reviewing!

FBruzzesi reacted with heart emoji

def test_sunburst_treemap_colorscales():
labels = ["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"]
parents = ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"]
Expand Down

AltStyle によって変換されたページ (->オリジナル) /