Skip to content

Instantly share code, notes, and snippets.

@empet
Last active September 21, 2017 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save empet/365cf202391bf7a58021388fadd52004 to your computer and use it in GitHub Desktop.
Save empet/365cf202391bf7a58021388fadd52004 to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Surface Animation with slider ##"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from plotly.offline import download_plotlyjs, init_notebook_mode, iplot, plot\n",
"init_notebook_mode(connected=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"u = np.linspace(-8, 8, 100)\n",
"x, y = np.meshgrid(u, u)\n",
"r = np.sqrt(x**2+y**2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"t = np.linspace(0, np.pi*2, 37)\n",
"z = np.array([(np.cos(r + s) * np.exp(-r/5)) for s in t])\n",
"zmin=np.min(z)\n",
"zmax=np.max(z)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"pl_ice=[[0.0, 'rgb(3, 5, 18)'],#cmocean\n",
" [0.11, 'rgb(27, 26, 54)'],\n",
" [0.22, 'rgb(48, 46, 95)'],\n",
" [0.33, 'rgb(60, 66, 136)'],\n",
" [0.44, 'rgb(62, 93, 168)'],\n",
" [0.56, 'rgb(66, 122, 183)'],\n",
" [0.67, 'rgb(82, 149, 192)'],\n",
" [0.78, 'rgb(106, 177, 203)'],\n",
" [0.89, 'rgb(140, 203, 219)'],\n",
" [1, 'rgb(188, 227, 235)']\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data=[dict(type='surface',\n",
" x=x, \n",
" y=y, \n",
" z=np.cos(r) * np.exp(-r/5),\n",
" colorscale=pl_ice, \n",
" showscale=False,\n",
" )]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"axis = dict(showbackground=True, \n",
" backgroundcolor=\"rgb(230, 230,230)\",\n",
" gridcolor=\"rgb(255, 255, 255)\", \n",
" zerolinecolor=\"rgb(255, 255, 255)\", \n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"frames=[dict(data= [ dict(z=z[k])],\n",
" traces= [0],\n",
" name='frame{}'.format(k) \n",
" ) for k in range(t.shape[0])]\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sliders=[dict(steps= [dict(method= 'animate',#Sets the Plotly method to be called when the\n",
" #slider value is changed.\n",
" args= [[ 'frame{}'.format(k) ],#Sets the arguments values to be passed to \n",
" #the Plotly method set in method on slide\n",
" dict(mode= 'immediate',\n",
" frame= dict( duration=50, redraw= False ),\n",
" transition=dict( duration= 0)\n",
" )\n",
" ],\n",
" label='{:.2f}'.format(t[k])\n",
" ) for k in range(t.shape[0])], \n",
" transition= dict(duration= 0 ),\n",
" x=0,#slider starting position \n",
" y=0, \n",
" currentvalue=dict(font=dict(size=12), \n",
" prefix='Time: ', \n",
" visible=True, \n",
" xanchor= 'center'\n",
" ), \n",
" len=1.0)#slider length)\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"layout = dict(title='Animating a 2d wave',\n",
" autosize=False,\n",
" width=600,\n",
" height=600,\n",
" showlegend=False,\n",
" scene=dict(camera = dict(eye=dict(x=1.25, y=0.9, z=1.1)),\n",
" aspectratio=dict(x=1, y=1, z=0.5),\n",
" xaxis=dict(axis),\n",
" yaxis=dict(axis), \n",
" zaxis=dict(axis, **{'range':[zmin, zmax]}),\n",
" \n",
" ),\n",
" updatemenus=[dict(type='buttons', showactive=False,\n",
" y=0,\n",
" x=1.15,\n",
" xanchor='right',\n",
" yanchor='top',\n",
" pad=dict(t=0, r=10),\n",
" buttons=[dict(label='Play',\n",
" method='animate',\n",
" args=[None, \n",
" dict(frame=dict(duration=30, \n",
" redraw=False),\n",
" transition=dict(duration=0),\n",
" fromcurrent=True,\n",
" mode='immediate'\n",
" )\n",
" ]\n",
" )\n",
" ]\n",
" )\n",
" ],\n",
" sliders=sliders\n",
" \n",
")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig=dict(data=data, layout=layout, frames=frames)\n",
"plot(fig) #if you want use iplot uncomment the line below:\n",
"#iplot(fig, validate=False)#validate=False is needed since there is an issue in offline.iplot:\n",
" #https://github.com/plotly/plotly.py/issues/702"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [default]",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment