I am trying to animate a 3D surface over an initial 3D plot. But am struggling with keeping the initial 3D plot on the animation. I have to call the clafunction to be able to plot the new surface and this will delete my initial state.
As an example, I have the animation of a surface while keeping the x,y,z axis. The end goal would be to plot the initial state once, and animate the surface.
Is it possible to create plot layers? Send the initial state to one layer and the animation to another?
import matplotlib.pyplot as plt import numpy as np from matplotlib.animation import FuncAnimation
fig = plt.figure(figsize=(7, 7))
ax = fig.add_subplot(111, projection='3d')
def initialState(ax):
s=1.15
x_axis = np.array([s*5, 0, 0])
y_axis = np.array([0, s*5, 0])
z_axis = np.array([0, 0, s*5])
ax.quiver(0,0,0,*x_axis,color = 'k', alpha =1, arrow_length_ratio=0.05)
ax.text(*(s*x_axis),'x',fontsize=10)
ax.quiver(0,0,0,*y_axis, color = 'k', alpha =1, arrow_length_ratio=0.05)
ax.text(*(s*y_axis),'y',fontsize=10)
ax.quiver(0,0,0,*z_axis,color = 'k', alpha =1, arrow_length_ratio=0.05)
ax.text(*(s*z_axis),'z',fontsize=10)
ax.set_xlim3d(-10, 10)
ax.set_ylim3d(-5, 5)
ax.set_zlim3d(-5, 5)
def plot(i):
X = np.arange(-5+i, 5+i, 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)
return [X,Y,Z]
def anime(i, ax, stepFactor):
ax.cla()
points = plot(i*stepFactor)
ax.plot_surface(*points, alpha=0.5)
ax.set_xlim3d(-10, 10)
ax.set_ylim3d(-5, 5)
ax.set_zlim3d(-5, 5)
animation = FuncAnimation(
fig,
anime,
init_func=initialState(ax),
frames=range(100),
fargs=(ax, 0.1)
)
Thanks for the help!