Search code examples
pythonanimationscatter

animation not sorted in px.scatter_geo


I want to display a geographical distribution of events (in this case 'someone wrote a letter') animated over time. I'm using px.scatter_geo for this. I have a dataframe which contains the country (name, ISO-2 and ISO-3) as well as the geocodes (lan/lat) describing its center. And most importantly the count of the events per year resulting from a groupby(). This is my dataframe: dataframe on top of which I'll apply scatter_geo

The following coding then is meant to produce the expected result with column "entstehung_jahr" representing the year over which the slider should work. And stats_country_year_geo_ISO3 is the above dataframe.

fig = px.scatter_geo(stats_country_year_geo_ISO3, locations="Alpha-3 code",
                     hover_name="country", size="counts",
                     animation_frame="entstehung_jahr",
                     projection="natural earth")
fig.show()

In general things look fine.

Map

But I'm getting a slider in which the sequence of years is wrong.

I tried explicitly sorting the dataframe, but no change. Any idea?


Solution

  • found the answer. Problem was that I made too many assumptions on how "smart" the animation would be. As "I knew" it is years and by that numbers, which at the end need to come into a logical sequence, I implied this semantics also to the slider. But essentially the slider only looks at "categories of same things", no more no less. And second issue was that I had done the groupby() in the wrong sequence. Instead of country/year it should be year/country.