The result is:
This page shows how to generate an animation of 3D surface plot using plot_surface and animation.Funcanimation in python and matplotlib.pyplot.
See also:
This page shows how to generate animation with rotating azimuth angle in the 3D surface plot using python, matplotlib.pyplot, and matplotlib.animation.FuncAnimation.
This page shows how to generate animation with rotating elevation angle in the 3D surface plot using python, matplotlib.pyplot, and matplotlib.animation.FuncAnimation.
This page is refferring the following web page:
- Updating z data on a surface_plot in Matplotlib animation - stackoverflow -, URL: https://stackoverflow.com/questions/45712099/updating-z-data-on-a-surface-plot-in-matplotlib-animation
import numpy as np
print('numpy: '+np.version.full_version)
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
import matplotlib
print('matplotlib: '+matplotlib.__version__)
Data to show in animation using plot_surface
N = 150 # Meshsize
fps = 10 # frame per sec
frn = 50 # frame number of the animation
x = np.linspace(-4,4,N+1)
x, y = np.meshgrid(x, x)
zarray = np.zeros((N+1, N+1, frn))
f = lambda x,y,sig : 1/np.sqrt(sig)*np.exp(-(x**2+y**2)/sig**2)
for i in range(frn):
zarray[:,:,i] = f(x,y,1.5+np.sin(i*2*np.pi/frn))
In order to animate the 3D surface graph, animation.FuncAnimation is suitable. This is because the animation.Artistanimation takes VERY LONG TIME to generate 3D animations. See the discussion in Github: https://github.com/matplotlib/matplotlib/issues/10207
def update_plot(frame_number, zarray, plot):
plot[0].remove()
plot[0] = ax.plot_surface(x, y, zarray[:,:,frame_number], cmap="magma")
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plot = [ax.plot_surface(x, y, zarray[:,:,0], color='0.75', rstride=1, cstride=1)]
ax.set_zlim(0,1.1)
ani = animation.FuncAnimation(fig, update_plot, frn, fargs=(zarray, plot), interval=1000/fps)
Save the animation in the mp4 and gif format using ffmpeg and imagemagick, respectively.
fn = 'plot_surface_animation_funcanimation'
ani.save(fn+'.mp4',writer='ffmpeg',fps=fps)
ani.save(fn+'.gif',writer='imagemagick',fps=fps)
Reduce the size of the GIF file using imagemagick.
import subprocess
cmd = 'magick convert %s.gif -fuzz 5%% -layers Optimize %s_r.gif'%(fn,fn)
subprocess.check_output(cmd)
Finaly, show the animation in the jupyter notebook.
plt.rcParams['animation.html'] = 'html5'
ani