Skip to content

Instantly share code, notes, and snippets.

@lordlycastle
Forked from empet/bug-animate-offline
Last active September 22, 2017 09:48
Show Gist options
  • Save lordlycastle/d384d0324eb0d0598b4cda2f5815f76d to your computer and use it in GitHub Desktop.
Save lordlycastle/d384d0324eb0d0598b4cda2f5815f76d to your computer and use it in GitHub Desktop.
# coding: utf-8
# In[14]:
import numpy as np
import pandas as pd
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot, plot
init_notebook_mode(connected=True)
# In[2]:
u = np.linspace(-8, 8, 100)
x, y = np.meshgrid(u, u)
r = np.sqrt(x**2+y**2)
# In[16]:
t = np.linspace(0, np.pi*2, 37)
z = np.array([(np.cos(r + s) * np.exp(-r/5)) for s in t])
zmin=np.min(z)
zmax=np.max(z)
# In[4]:
pl_ice=[[0.0, 'rgb(3, 5, 18)'],#cmocean
[0.11, 'rgb(27, 26, 54)'],
[0.22, 'rgb(48, 46, 95)'],
[0.33, 'rgb(60, 66, 136)'],
[0.44, 'rgb(62, 93, 168)'],
[0.56, 'rgb(66, 122, 183)'],
[0.67, 'rgb(82, 149, 192)'],
[0.78, 'rgb(106, 177, 203)'],
[0.89, 'rgb(140, 203, 219)'],
[1, 'rgb(188, 227, 235)']
]
# In[5]:
data=[dict(type='surface',
x=x,
y=y,
z=np.cos(r) * np.exp(-r/5),
colorscale=pl_ice,
showscale=False,
)]
# In[6]:
axis = dict(showbackground=True,
backgroundcolor="rgb(230, 230,230)",
gridcolor="rgb(255, 255, 255)",
zerolinecolor="rgb(255, 255, 255)",
)
# In[18]:
frames=[dict(data= [ dict(z=z[k])],
traces= [0],
name='frame{}'.format(k),
layout=dict(scene=dict(camera = dict(eye=dict(x=0, y=0, z=1), up=dict(x=0, y=0, z=-1))))
) for k in range(t.shape[0])]
# In[8]:
sliders=[dict(steps= [dict(method= 'animate',#Sets the Plotly method to be called when the
#slider value is changed.
args= [[ 'frame{}'.format(k) ],#Sets the arguments values to be passed to
#the Plotly method set in method on slide
dict(mode= 'immediate',
frame= dict( duration=50, redraw= False ),
transition=dict( duration= 0)
)
],
label='{:.2f}'.format(t[k])
) for k in range(t.shape[0])],
transition= dict(duration= 0 ),
x=0,#slider starting position
y=0,
currentvalue=dict(font=dict(size=12),
prefix='Time: ',
visible=True,
xanchor= 'center'
),
len=1.0)#slider length)
]
# In[19]:
layout = dict(title='Animating a 2d wave',
autosize=False,
width=600,
height=600,
showlegend=False,
scene=dict(camera = dict(eye=dict(x=0, y=0, z=1), up=dict(x=0, y=0, z=-1)),
aspectratio=dict(x=1, y=1, z=0.5),
xaxis=dict(axis),
yaxis=dict(axis),
zaxis=dict(axis, **{'range':[zmin, zmax]}),
),
updatemenus=[dict(type='buttons', showactive=False,
y=0,
x=1.15,
xanchor='right',
yanchor='top',
pad=dict(t=0, r=10),
buttons=[dict(label='Play',
method='animate',
args=[None,
dict(frame=dict(duration=30,
redraw=False),
transition=dict(duration=0),
fromcurrent=True,
mode='immediate'
)
]
)
]
)
],
sliders=sliders
)
# In[20]:
fig=dict(data=data, layout=layout, frames=frames)
# plot(fig) #if you want use iplot uncomment the line below:
iplot(fig, validate=False)#validate=False is needed since there is an issue in offline.iplot:
#https://github.com/plotly/plotly.py/issues/702
# In[ ]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment