# Surface plot animation

**URL:** <https://community.plotly.com/t/surface-plot-animation/23738>\
**Category:** 📊 Plotly Python\
**Created:** [May 22, 2019, 5:37pm UTC](https://community.plotly.com/t/surface-plot-animation/23738 "2019-05-22T17:37:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielgafni](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@danielgafni](https://community.plotly.com/u/danielgafni)\
**Post date:** [May 22, 2019, 5:37pm UTC](https://community.plotly.com/t/surface-plot-animation/23738/1 "2019-05-22T17:37:45Z")

</div>

Hello!  
I’ve been trying to solve this my self for a while and did some research, but wasn’t able to find the answer.  
I need to animate a 3D surface plot. I have a 3-dimentional array of data for u(x, y, t), so I want to plot u(x, y) for each t and then observe the animation. I found a work around that I don’t really like. Do you guys know is there a proper way of doing the same in plotly? Here is my solution:

from plotly.offline import iplot  
import ipywidgets as widgets  
import time  
from IPython.display import display

fig = go.FigureWidget(layout = layout)  
surface = fig.add\_surface(x = solver.x, y = solver.y, z=solver.u[0].T, colorscale = ‘YlGnBu’)

fig.layout.scene.zaxis.range = [np.min(solver.u), np.max(solver.u)]  
fig.layout.scene.yaxis.range = [solver.Y\_START, solver.Y\_END]  
fig.layout.scene.xaxis.range = [solver.X\_START, solver.X\_END]

def set\_surface(i=0):  
surface.z = solver.u[i].T

display(fig)

for i in range(solver.J):  
set\_surface(i)  
time.sleep(0.2)

as you can see it will only work once when running the last loop.  
solver is an atribute of my class with all the data inside.

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![jmmease](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jmmease/32/3706_2.png) [@jmmease](https://community.plotly.com/u/jmmease)\
**Post date:** [May 24, 2019, 10:29am UTC](https://community.plotly.com/t/surface-plot-animation/23738/2 "2019-05-24T10:29:55Z")

</div>

Hi @danielgafni,

You could try controlling the animation with an ipywidget `Play` widget (See [https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20List.html#Play-(Animation)-widget](https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20List.html#Play-(Animation)-widget)) instead of the for loop.

I don’t know if there’s a plotly `FigureWidget` example that does this, but here’s an example notebook from the bqplot project that uses the Play button and a slider to animate a plot ([https://nbviewer.jupyter.org/github/bloomberg/bqplot/blob/master/examples/Applications/Wealth%20of%20Nations.ipynb](https://nbviewer.jupyter.org/github/bloomberg/bqplot/blob/master/examples/Applications/Wealth%20of%20Nations.ipynb)). Your `year_changed` function would call your `set_surface` function.

-Jon

---

<div class="post-metadata">

**Author:** ![danielgafni](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@danielgafni](https://community.plotly.com/u/danielgafni)\
**Post date:** [May 24, 2019, 8:03pm UTC](https://community.plotly.com/t/surface-plot-animation/23738/3 "2019-05-24T20:03:19Z")

</div>

Thank you for your answer!

I’ve already managed to find the solution, basically it’s the same thing you advised me to do.

Prorblem solved.
