'Poltly Express `scatter_mapbox` with `animation_frame` and `color`
If have data with columns : 'e_lat', 'e_lon', 'Année' and 'Expéditeur'. I'm trying to make a scatter_mapbox, with frames animated by 'Année", and color based on 'Expéditeur' (which is categorical, the names of the persons).
# I created a row df['_color'] = df['Expéditeur'] because in the end I want to change the way _color is defined.
fig = px.scatter_mapbox(df,
lat='e_lat',
lon='e_lon',
hover_name='Ville Expéditeur',
hover_data=['Expéditeur', 'Destinataire', 'Ville Expéditeur', 'Année'],
animation_frame='Année',
mapbox_style='carto-positron',
zoom=4,
color='_color',
)
fig.show()
The trouble is I get something like this :
(only the points that have the first color are showing)

If I comment out color='_color',, I get :
If I comment out animation_frame='Année'',, I get :

I did a lot of research but it always seems to me that people are doing exactly the same and have different results ([this tutorial for example](https://towardsdatascience.com/how-to-animate-scatterplots-on-mapbox-using-plotly-express-3bb49fe6a5d, this stackoverflow issue, ...)
My plotly lib is up-to-date.
I tried a lot of different things with `animation group, color_discrete_map, color_discrete_sequence etc. but without any success).
What am I doing wrong here ? Thx a lot.
[EDIT] In fact it's exaclty the same problem than here (not resolved). This seems to be because of a documented bug of Plotly. "If all colors are used for the first frame, the problem disappears."
I solved it by adding "fake data" (outside of the scope of the map) : one for each color, in the first frame of the animation.
new_rows = []
for color in df['_color'].unique():
new_row = {'_color': color, 'Année': df['Année'].min(), 'lat': 0, 'lon': 0}
new_rows.append(new_row)
df = df.append(new_rows, ignore_index=True)
Hope this will help someone.
Solution 1:[1]
In fact it's exaclty the same problem than here (not resolved). This seems to be because of a documented bug of Plotly. "If all colors are used for the first frame, the problem disappears."
I solved it by adding "fake data" (outside of the scope of the map) : one for each color, in the first frame of the animation.
new_rows = []
for color in df['_color'].unique():
new_row = {'_color': color, 'Année': df['Année'].min(), 'lat': 0, 'lon': 0}
new_rows.append(new_row)
df = df.append(new_rows, ignore_index=True)
Hope this will help someone.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sugnavurze |
