Animated Scatter plot (Plotly - Python)

Viewed 39

I am trying to create an animated Scattermapbox (plotly - Python) with the option to select different dimensions.

I have a dataframe set up in the following way:

X Y Anno I M F
0 15 47 2004 1 2 5
1 15 47 2005 6 1 0
2 15 47 2006 4 3 2
3 46 50 2004 1 0 3
... ... ... ... ... ... ...

I first tried using plotly.express.scatter_mapbox without success. I then switched to go.Scattermapbox to overcome some limitations regarding the use of buttons.

The problem is that I really wouldn't know how to recreate the animation concerning the passing of years.

My goal is: To visualize, for different coordinates, how dimensions "I", "M" and "F" vary over time.

The documentation was not clear on this and I got stuck. Below is the code I was able to implement. Any help is appreciated.

scale_I = 2.*max(df_long["I"])/(8.**2)
scale_M = 2.*max(df_long["M"])/(8.**2)
scale_F = 2.*max(df_long["F"])/(8.**2)

fig = go.Figure(go.Scattermapbox(lat=df_long["Y"], lon=df_long["X"], mode="markers", marker_size=df_long["I"]/scale_I))

fig.add_trace(go.Scattermapbox(lat=df_long["Y"], lon=df_long["X"], mode="markers", marker_size=df_long["M"]/scale_M, visible=False))

fig.add_trace(go.Scattermapbox(lat=df_long["Y"], lon=df_long["X"], mode="markers", marker_size=df_long["F"]/scale_F, visible=False))



fig.update_layout(
                          title_text="Data",
                          mapbox_style="open-street-map", 
                          updatemenus=[
                              dict(
                                  direction = "down",
                                  buttons=list([
                                      dict(
                                          args=[{"visible":[True, False, False]}],
                                          label="I",
                                          method="update"
                                          
                                      ),
                                      dict(
                                          args=[{"visible":[False, True, False]}],
                                          label="M",
                                          method="update"
                                      ),
                                      dict(
                                          args=[{"visible":[False, False, True]}],
                                          label="F",
                                          method="update"
                                      )
                                  ]),
                                  pad={"r": 0, "t": -10},
                                  showactive=True
                              ),
                              dict(
                                      type = "buttons",
                                      #direction = "down",
                                      buttons=list([
                                          dict(
                                              args=[None],
                                              label="Play",
                                              method="animate",
                                              
                                          )
                                      ]),
                                      pad={"r": 0, "t": -50},
                                      showactive=True
                              ),
                          ],
                          title="Analysis",
                          font_size=18,
                          mapbox_center_lon=0, 
                          mapbox_center_lat=0, 
                          mapbox_zoom = 2,
                          #margin={"r":0,"t":0,"l":0,"b":0}

)
           
fig.show()
0 Answers
Related