Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过移动通过散点绘制的点来设置python pyplot的动画_Python_Animation_Matplotlib - Fatal编程技术网

通过移动通过散点绘制的点来设置python pyplot的动画

通过移动通过散点绘制的点来设置python pyplot的动画,python,animation,matplotlib,Python,Animation,Matplotlib,我在用Python制作动画时遇到困难。我的问题是设置沿特定轨迹移动的3D点的动画。我可以通过使用动画模块来实现这一点,并在每一帧重新制作情节(请参见我脚本中的第一个选项)。相反,我希望只移动每个帧上的点位置,而不重新生成所有轴(请参见脚本中的第二个选项)。这是我的剧本: import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as an from mpl_toolkits.mplot3d imp

我在用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并取消注释第二个FuncAnimation),则仅在z方向上移动我的点。关于如何在x和y方向上移动它的任何建议?

仅沿z轴移动的原因是因为
set\u 3d\u properties
仅适用于第三个轴。因此,前两个
set_3d_properties
调用是无影响的。请参阅工作修改代码:

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()