Surface plot animation

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!

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) 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). Your year_changed function would call your set_surface function.

-Jon

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.