我有一个形状为(18,101)
的数组X_trj
要在3D中绘制(它们是三个不同车辆的轨迹),我尝试通过执行以下操作来动画绘制:
#animate the plot:
import matplotlib.animation as animation
# First, create a function that updates the scatter plot for each frame
def update_plot(n,X_trj,scatters):
# Set the data for each scatter plot
scatters[0].set_offsets(np.stack((X_trj[0, :n], X_trj[1, :n], X_trj[2, :n]), axis=1))
scatters[1].set_offsets(np.stack((X_trj[6, :n], X_trj[7, :n], X_trj[8, :n]), axis=1))
scatters[2].set_offsets(np.stack((X_trj[12,:n], X_trj[13, :n], X_trj[14,:n]), axis=1))
return scatters
# Create the figure and axis
fig = plt.figure()
ax = plt.axes(projection='3d')
# Create the scatter plots
scatters = []
scatters.append(ax.scatter(X_trj[0,:], X_trj[1,:], X_trj[2,:]))
scatters.append(ax.scatter(X_trj[6,:], X_trj[7,:], X_trj[8,:]))
scatters.append(ax.scatter(X_trj[12,:], X_trj[13,:], X_trj[14,:]))
# Set the title
ax.set_title('Trajectory from one-shot optimization (human + drones)')
ani = animation.FuncAnimation(fig, update_plot, frames=range(X_trj.shape[1]), fargs=(X_trj, scatters))
plt.show()
ani.save('animation.mp4')
在运行代码后,我得到了下面的图:
然而,当我打开mp4
文件时,我的动画没有移动。它和我得到的静态图完全一样。任何帮助都非常感谢!
1条答案
按热度按时间41zrol4v1#
不清楚您从哪里复制了起始代码。大多数示例使用
ax.plot
而不是ax.scatter
。旧代码可能会在更新的matplotlib版本中过时。无论如何,你在初始化时已经填充了完整的最终轨迹。相反,你应该创建一个空的图,并手动设置x,y和z的限制。