通过移动通过散点绘制的点来使python pyplot动画化 [英] Animate a python pyplot by moving a point plotted via scatter
问题描述
我无法在Python中制作动画。我的问题是为沿特定轨迹移动的3D点设置动画。我可以通过使用动画模块来做到这一点,并在每一帧都重新制作情节(请参阅脚本中的第一个选项)。相反,我想只在每个帧上移动点位置,而不重新制作所有轴(请参见脚本中的第二个选项)。这是我的脚本:
I am having trouble in producing an animation in Python. My issue is to animate a 3D point moving along a certain trajectory. I am able to do this by using the animation module and remake at every frame the plot (see first option in my script). I would like instead to move only the point position at each frame without remaking the all axes (see second option in my script). Here is my script:
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方向上移动它的任何建议?
If I choose the second option (comment the first FuncAnimation and uncomment the second one), I obtain my point moving only in the z direction. Any suggestion on what should I do to move it also in the x and y directions?
推荐答案
仅沿该方向移动的原因z轴是因为 set_3d_properties
仅用于第三个轴。因此,前两个 set_3d_properties
调用没有影响。请参阅修改后的工作代码:
The reason for the movement only along z-axis is because set_3d_properties
is only for the third axis. Therefore, the first two set_3d_properties
calls were uninfluential. See the working modified code:
from matplotlib import pyplot as plt
import numpy as np
import mpl_toolkits.mplot3d.axes3d as p3
from matplotlib import animation
fig = plt.figure()
ax = p3.Axes3D(fig)
# 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 first plot
point, = ax.plot([x[0]], [y[0]], [z[0]], 'o')
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])
# second option - move the point position at every frame
def update_point(n, x, y, z, point):
point.set_data(np.array([x[n], y[n]]))
point.set_3d_properties(z[n], 'z')
return point
ani=animation.FuncAnimation(fig, update_point, 99, fargs=(x, y, z, point))
plt.show()
这篇关于通过移动通过散点绘制的点来使python pyplot动画化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!