0

Plotly animation not working with Scattermapbox. The following code is heavily based on https://chart-studio.plotly.com/~empet/14825/scattermapbox-animation-forum-question/#/. The Play button does activate the slider but the graph is stuck on the 1st frame. Anyone ran into the same issue and was able to solve it?

fig = go.Figure(data = go.Scattermapbox(
               lat=[__route_gps.loc[0, 'latitude']],
               lon=[__route_gps.loc[0, 'longitude']],
               mode='markers',
               marker=dict(size=10, color='red')
            ))
        

fig.update_layout(mapbox=dict(accesstoken=mapbox_access_token,
                              bearing=0,
                              center=dict(lat=__route_gps['latitude'].mean(),
                                          lon=__route_gps['longitude'].mean()),
                              pitch=0,
                              zoom=8.5,
                              style='carto-positron'))

frames=[dict(data=[dict(type='scattermapbox',
                        lat=__route_gps.iloc[:k+1]['latitude'].values,
                        lon=__route_gps.iloc[:k+1]['longitude'].values)],
             traces= [0],
             name='frame{}'.format(k)       
            ) for k  in  range(1, len(__route_gps))]

fig.update(frames=frames)

sliders = [dict(steps= [dict(method= 'animate',
                             args= [[ f'frame{k}'],
                                    dict(mode= 'immediate',
                                         frame= dict(duration=100, redraw= True ),
                                         transition=dict( duration= 0))
                                   ],
                             label='{:d}'.format(k)
                            ) for k in range(len(__route_gps))], 
                transition= dict(duration= 0 ),
                x=0,#slider starting position  
                y=0, 
                currentvalue=dict(font=dict(size=12), 
                                  prefix='Point: ', 
                                  visible=True, 
                                  xanchor= 'center'),  
                len=1.0)
           ]

fig.update_layout(updatemenus=[dict(type='buttons', showactive=False,
                                    y=0, yanchor='top',
                                    x=1.05, xanchor='right',
                                    pad=dict(t=0, r=10),
                                    buttons=[dict(label='Play',
                                                  method='animate',
                                                  args=[None, 
                                                        dict(frame=dict(duration=100, 
                                                                        redraw=True),
                                                             transition=dict(duration=0),
                                                             fromcurrent=True,
                                                             mode='immediate'
                                                            )
                                                       ]
                                                 )
                                            ]
                                   )
                              ],
                  sliders=sliders)
fig.show();

If I replace Scattermapbox with simple Scatter plot it does work. But it is useless to show GPS data without a map.

f37
  • 1

0 Answers0