The result is:
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 code is based on the Animate a rotating 3D graph in matplotlib - stackoverflow -
See also:
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 shows how to generate an animation of 3D surface plot using plot_surface and animation.Funcanimation in python and matplotlib.pyplot.
In [1]:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
In [2]:
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
# Create a figure and a 3D Axes
fig = plt.figure()
ax = Axes3D(fig)
def init():
# Plot the surface.
ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
return fig,
def animate(i):
# azimuth angle : 0 deg to 360 deg
ax.view_init(elev=10, azim=i*4)
return fig,
# Animate
ani = animation.FuncAnimation(fig, animate, init_func=init,
frames=90, interval=50, blit=True)
Save the animation in the mp4 and gif format using ffmpeg and imagemagick, respectively.
In [3]:
fn = 'rotate_azimuth_angle_3d_surf'
ani.save(fn+'.mp4',writer='ffmpeg',fps=1000/50)
ani.save(fn+'.gif',writer='imagemagick',fps=1000/50)
Reduce the size of the GIF file using imagemagick.
In [4]:
import subprocess
cmd = 'magick convert %s.gif -fuzz 5%% -layers Optimize %s_r.gif'%(fn,fn)
subprocess.check_output(cmd)
Out[4]:
Finaly, show the animation in the jupyter notebook.
In [5]:
plt.rcParams['animation.html'] = 'html5'
ani
Out[5]: