在散点图框上绘制动画

时间:2019-02-12 06:19:22

标签: python animation maps plotly

我实际上已经为我工作了一分钟,但是现在我不知道如何恢复工作状态。

对于动画(Plotly / Python),我的问题是关于框架的。当每个帧为“散点图框”时应将其呈现为动画时,正确的格式是什么?我认为那是我出问题的地方。

我要从这里借用代码-https://plot.ly/~empet/14825/scattermapbox-animation-forum-question/#/

在没有动画的情况下运行该图形也可以使用相同的代码。设置完数据和布局变量后,此方法有效


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

但是,当我运行下面的代码时,出现错误。这是完整的代码-

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)

我得到的错误值是

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

看起来Frame元素不喜欢'lat''lon',这就是我被困住的地方。

1 个答案:

答案 0 :(得分:0)

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

相关问题