Skip to content

Line plot with category_orders draws the line in the wrong order #3920

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

Closed
ghost opened this issue Oct 13, 2022 · 2 comments
Closed

Line plot with category_orders draws the line in the wrong order #3920

ghost opened this issue Oct 13, 2022 · 2 comments

Comments

@ghost
Copy link

ghost commented Oct 13, 2022

I am trying to plot a pandas DataFrame with a column that contains the month as a string:

df = pd.DataFrame({"month": ["feb", "mar", "jan", "apr"], "value": [20, 3, 10, 4]})
df
month value
0 feb 20
1 mar 3
2 jan 10
3 apr 4

If I just feed this to px.line the x-axis is in alphabetical order (feb, mar, jan, apr):

px.line(df, x="month", y="value")

newplot

Luckily px.line has a category_orders argument:

px.line(df, x="month", y="value", category_orders={"month": ["jan", "feb", "mar", "apr"]})

It changes the x-axis order, but it draws the line in the original order: from feb to mar to jan to apr

newplot (1)

Should I call the px.line in another way when I want to use that argument? Because the current behaviour seems incorrect.

I can work around it by first sorting my DataFrame and then plotting, but it would be more convenient if the category_orders argument could be able to take care of it.

monthdict = {"jan": 1, "feb": 2, "mar": 3, "apr": 4}
df.sort_values(by="month", inplace=True, key=lambda x: x.map(monthdict))
px.line(df, x="month", y="value")

newplot (2)

I founde #3645, but that seems to be in combination with the facet_col argument. I am experiencing this issue with a "plain" line.

Version of plotly is 5.10.0.

@nicolaskruchten
Copy link
Contributor

The current behaviour is as designed: lines connect points in the order they are given in the input data frame, and the category_orders controls the axis order only, independent of the data frame order.

@ghost
Copy link
Author

ghost commented Oct 13, 2022

Ok, that's odd, but consistent I guess. In that case this ticket can be closed.

@ghost ghost closed this as completed Oct 13, 2022
This issue was closed.
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

No branches or pull requests

1 participant