Using plotly in Jupyter to create animated chart in off-line mode

半腔热情 提交于 2019-12-04 08:52:30

This issue is addressed in Plotly icreate_animations offline on Jupyter Notebook

According to that answer, the plotly.grid_objs Grid function is not supported offline. He transitions the code to use DataFrames instead, which avoids the issue.

This is old, but if anybody is trying to figure it out, the only difference is the usage of from plotly.grid_objs import Grid, Column as data holders which can be easily replaced by a pandas.DataFrame or a dict.

By using a dict, replace the following code block:

my_columns = []
for k in range(len(appl.index) - 1):
    my_columns.append(Column(appl.index[:k + 1], 'x{}'.format(k + 1)))   
    my_columns.append(Column(appl_price[:k + 1], 'y{}'.format(k + 1)))
grid = Grid(my_columns)

with:

my_columns = {}
for k in range(len(appl.Date) - 1):
    my_columns['x{}'.format(k + 1)] = list(appl.Date)[:k + 1]
    my_columns['y{}'.format(k + 1)] = appl_price[:k + 1]

So whats happening? Plotly's Grid use the method get_column_reference later on which works exactly like a dict, it stores key value pairs that are used for the plot start as well as for its frames.

From there, replace all reference to the Grid to your created dict which is stored as my_columns from our change made above. Finally use iplot from the offline Python API instead of icreate_animations.

The second block of code:

data=[dict(type='scatter',
           x= my_columns['x1'],
           y= my_columns['y1'],
           name='AAPL',
           mode='lines',
           line=dict(color= 'rgb(114, 186, 59)'),
           fill='tozeroy',
           fillcolor='rgba(114, 186, 59, 0.5)')]

axis=dict(ticklen=4,
          mirror=True,
          zeroline=False,
          showline=True,
          autorange=False,
          showgrid=False)

layout = dict(title='AAPL Daily Stock Price',
              font=dict(family='Balto'),
              showlegend=False,
              autosize=False,
              xaxis=dict(axis, **{'nticks':12, 'tickangle':-45,
                                  'range': [to_unix_time(datetime(2015, 2, 17)),
                                            to_unix_time(datetime(2016, 11, 30))]}),
              yaxis=dict(axis, **{'title': '$', 'range':[0,170]}),
              updatemenus=[dict(type='buttons',
                                showactive=False,
                                y=1,
                                x=1.1,
                                xanchor='right',
                                yanchor='top',
                                pad=dict(t=0, r=10),
                                buttons=[dict(label='Play',
                                              method='animate',
                                              args=[None, dict(frame=dict(duration=50, redraw=True), 
                                                               transition=dict(duration=0),
                                                               fromcurrent=True,
                                                               mode='immediate')])])])

frames=[{'data':[{'x': my_columns['x{}'.format(k + 1)],
                  'y': my_columns['y{}'.format(k + 1)]}],
         'traces': [0]
        } for k in range(len(appl.Date) - 1)]


fig=dict(data=data, layout=layout, frames=frames)
iplot(fig,
      show_link=False, config=dict(displaylogo=False, modeBarButtonsToRemove=['sendDataToCloud']))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!