Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Scatter with animation_frame is plotted out of order #23

Closed
dvduvix opened this issue Mar 22, 2019 · 22 comments
Closed

Scatter with animation_frame is plotted out of order #23

dvduvix opened this issue Mar 22, 2019 · 22 comments

Comments

@dvduvix
Copy link

dvduvix commented Mar 22, 2019

I tried to plot a scatter with plotly express and I got a very weird behavior

Any thoughts?

@nicolaskruchten
Copy link
Contributor

If you're using Python 2, then order is not guaranteed unfortunately and you'll have to use the category_orders argument to specify the order for now.

@dvduvix
Copy link
Author

dvduvix commented Mar 22, 2019

@nicolaskruchten I am using Python 3. But for the sake of trying: how do I use category_orders?

@nicolaskruchten
Copy link
Contributor

If you're using Python 3 then this is a bug :) Can you post a minimal reproducible example in source here please?

the category_orders argument is a dict where the keys are column names and the values are lists containing the values you want to see in the order you want to see them. So in this case you'd have category_orders={"frame": [0,1,2,3,...]}

@nicolaskruchten
Copy link
Contributor

Ah, sorry, I should be clearer: if you're using Python below 3.7 (i.e. not just 2.*) then order is not guaranteed. As of Python 3.7 iterating over dict keys is guaranteed to be in insertion order

@dvduvix
Copy link
Author

dvduvix commented Mar 22, 2019

@nicolaskruchten Then that must be it. I have Python 3.5.2

@kmader
Copy link
Contributor

kmader commented Mar 22, 2019

Thanks for that insight about Python 3.5.2 vs 3.7 I hadn't thought of that. Here is the minimal code/environment to run it https://gist.github.com/kmader/7a182744194a5330008b6e40d60aea6b and the link to just run it on mybinder: https://mybinder.org/v2/gist/kmader/7a182744194a5330008b6e40d60aea6b/master?filepath=example.ipynb

As Kaggle Kernels use Python >3.5 it works fine on their site: https://www.kaggle.com/kmader/plotly-express-bug

@dvduvix
Copy link
Author

dvduvix commented May 2, 2019

@nicolaskruchten So I finally decided to test the theory and installed Python 3.7.3. The out of order problem remains.

The only thing that worked to fix this was using the category_orders.

@nicolaskruchten
Copy link
Contributor

@dvduvix this is very puzzling... are you able to share a CSV of your data or some other way I could reproduce this locally?

@nicolaskruchten
Copy link
Contributor

(also, perhaps try with v0.1.8 which just came out with a subtle change that might just shake this problem loose)

@MengyinZhong
Copy link

@dvduvix
Hi, I met the same issue and I attempted to fix it, but it doesn't seem to work, so could you please post your code? Thx a lot~😃

@dvduvix
Copy link
Author

dvduvix commented May 9, 2019

@MengyinZhong
All I did is added a category_orders to scatter.

px.scatter(data, animation_frame='frame', animation_group='col_name', category_orders={0: range(1000)})

where 0 is the column name (use quotes if your column has an actual name) and range can be replaced with the list of all the items of that particular column but in the order you want.

@nicolaskruchten I cannot share the file right now, but it's just a CSV file in the following format"
index,value1x,value1y,value2x,value2y....

I will try the latest version as soon as I can

@nicolaskruchten
Copy link
Contributor

I'm really struggling to replicate this bug on this end with Python 3.7. In fact, I'm struggling to get the frames to render out of order at all no matter what I do to the data frame :)

@MengyinZhong
Copy link

@dvduvix Thank you for your reply, and I tried the solution you provided, but the timeline is still out of order, shown as below
scatter

@nicolaskruchten
Copy link
Contributor

@MengyinZhong what version of Python and plotly_express are you using?

@nicolaskruchten
Copy link
Contributor

OK, I've just pushed what I believe is a fix for this in version 0.2.1, such that animation frames by default now should always render in the order the values are encountered in the input data frame. Please let me know if you're still encountering issues with this latest version!

@MengyinZhong
Copy link

MengyinZhong commented May 13, 2019

@nicolaskruchten
Python 3.7.0
plotly_express 0.2.1
Sorry for my tardy reply, but the order still cannot show as expected:
scatter

@nicolaskruchten
Copy link
Contributor

Wow, that's really bizarre. Can you share your data as well so that I can replicate this locally? What happens if you don't set category_orders at all with v0.2.1?

@MengyinZhong
Copy link

@nicolaskruchten I got the same result without setting category_orders. Here is my data https://github.com/MengyinZhong/test/blob/master/test.csv

@nicolaskruchten
Copy link
Contributor

nicolaskruchten commented May 13, 2019

OK, thanks for sharing this data! I can reproduce your problem on this end and I'll try to push out another fix shortly. I really appreciate the patience and follow-through: this makes the library better for everyone 😄 🙏

@nicolaskruchten
Copy link
Contributor

OK, I think I've got it for real this time! Please try v0.2.2, with or without category_orders, on any version of Python :)

@nicolaskruchten
Copy link
Contributor

Closing for now, please reopen if this happens again :)

@ariewjy
Copy link

ariewjy commented Jul 18, 2023

Hi, @nicolaskruchten ,

I have the same problem, the year animation frame is out of order, and I am having the same issue in my laptop and also in Kaggle notebook.
CleanShot 2023-07-19 at 01 16 38

Here is the link to the notebook to replicate the issue.
https://www.kaggle.com/ariewijaya/plotly-animation-bug

I am using latest plotly, same as in Kaggle Notebook.
CleanShot 2023-07-19 at 01 17 00

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants