我在用 Python 制作动画时遇到了麻烦。我的问题是制作沿着特定轨迹移动的 3D 点的动画。我可以通过使用动画模块并在绘图的每一帧重新制作来做到这一点(请参阅我的脚本中的第一个选项)。我想只移动每帧的点位置,而不重新制作所有轴(请参阅我的脚本中的第二个选项)。这是我的脚本:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as an
from mpl_toolkits.mplot3d import Axes3D
# create the parametric curve
t=np.arange(0, 2*np.pi, 2*np.pi/100)
x=np.cos(t)
y=np.sin(t)
z=t/(2.*np.pi)
# create the figure
fig=plt.figure()
ax=fig.gca(projection='3d')
# create the first plot
point=ax.scatter(x[0], y[0], z[0])
line=ax.plot(x, y, z, label='parametric curve')
ax.legend()
ax.set_xlim([-1.5, 1.5])
ax.set_ylim([-1.5, 1.5])
ax.set_zlim([-1.5, 1.5])
# first option - remake the plot at every frame
def update_axes(n, x, y, z, ax):
ax.cla()
ax.set_xlim([-1.5, 1.5])
ax.set_ylim([-1.5, 1.5])
ax.set_zlim([-1.5, 1.5])
point=ax.scatter(x[n], y[n], z[n])
line=ax.plot(x, y, z, label='parametric curve')
ax.legend()
return point
ani=an.FuncAnimation(fig, update_axes, 99, fargs=(x, y, z, ax))
# second option - move the point position at every frame
def update_point(n, x, y, z, point):
point.set_3d_properties(x[n], 'x')
point.set_3d_properties(y[n], 'y')
point.set_3d_properties(z[n], 'z')
return point
#ani=an.FuncAnimation(fig, update_point, 99, fargs=(x, y, z, point))
# make the movie file demo.mp4
writer=an.writers['ffmpeg'](fps=20)
dpi = 100
ani.save('demo.mp4',writer=writer,dpi=dpi)
如果我选择第二个选项(注释第一个 FuncAnimation 并取消注释第二个),我将获得仅在 z 方向移动的点。关于我应该做什么才能在 x 和 y 方向上移动它有什么建议吗?