简体   繁体   中英

plotly animation on scattermapbox frame

I actually had this working for me for a minute, but now I can't figure out how to get back to working form.

For the animation (Plotly/Python), my question is around the frame . What is the correct format for each frame when it should be rendered as an animation when it is a 'scattermapbox'? I think that's where stuff is going wrong for me.

I'm borrowing code from here - https://plot.ly/~empet/14825/scattermapbox-animation-forum-question/#/

This same code works fine with just running this figure without the animation. After set the data and layout variables - this works


fig = dict(data=data, layout=layout)
py.iplot(fig, filename='MultipleMapbox')

However, when I run the code below, I get an error. Here's the full code -

import pandas as pd

import plotly.plotly as py
from plotly.graph_objs import *
#from plotly.grid_objs import Grid, Column
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
mapbox_access_token = ''

filename = "https://raw.githubusercontent.com/spencerlawrence36/basic/master/places.csv"
df = pd.read_csv(filename, encoding='utf-8')
df = df.head(100)
df.columns
data = [dict(type='scattermapbox',
               lat=[33.49],
               lon=[-112.05],
               mode='markers',
               marker=dict(size=15, color='#000000')
            )
        ]

layout = dict(
    autosize=True,
    hovermode='closest',
    mapbox=dict(accesstoken=mapbox_access_token,
                bearing=0,
                center=dict(lat=33.49,
                            lon=-112.05
                            ),
                pitch=0,
                zoom=10,
                style='light'
                )
            )
lats=list(df['lat'])
lons=list(df['lng'])
frames=[dict(data= [dict(lat=lats[:k+1],
                         lon=lons[:k+1])
                   ],
             traces= [0],
             name='frame{}'.format(k)       
            ) for k  in  range(1, len(df))]

sliders=[dict(steps= [dict(method= 'animate',
                           args= [[ 'frame{}'.format(k) ],
                                  dict(mode= 'immediate',
                                  frame= dict( duration=200, redraw= False ),
                                           transition=dict( duration= 0)
                                          )
                                    ],
                            label='{:d}'.format(k)
                             ) for k in range(len(df))], 
                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)#slider length)
           ]
layout.update(updatemenus=[dict(type='buttons', showactive=False,
                                y=0,
                                x=1.05,
                                xanchor='right',
                                yanchor='top',
                                pad=dict(t=0, r=10),
                                buttons=[dict(label='Play',
                                              method='animate',
                                              args=[None, 
                                                    dict(frame=dict(duration=200, 
                                                                    redraw=False),
                                                         transition=dict(duration=0),
                                                         fromcurrent=True,
                                                         mode='immediate'
                                                        )
                                                   ]
                                             )
                                        ]
                               )
                          ],
              sliders=sliders)
fig=dict(data=data, layout=layout, frames=frames)
plot(fig)

The error value I'm getting is

ValueError: Invalid properties specified for object of type plotly.graph_objs.Scatter: ('lat', 'lon')

Looks like the Frame element doesn't like the 'lat' 'lon', and that's where I'm stuck.

我的问题的解决方法可以在这里找到-https : //community.plot.ly/t/issue-with-scattermap-box-animation/19399

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM