Python 如何使用matplotlib设置图形动画,使其看起来像数据点在移动?

Python 如何使用matplotlib设置图形动画,使其看起来像数据点在移动?,python,animation,matplotlib,plot,scatter,Python,Animation,Matplotlib,Plot,Scatter,我有两个2D阵列,我想在散点图中显示数据,这样看起来像点在移动。所以我希望第一组x和y数据被绘制,然后消失,被下一组x和y数据替换,等等 我目前使用的代码只是绘制所有数据点并将它们连接起来,有效地跟踪数据点的路径 pyplot.figure() for i in range(0,N): pyplot.plot(x[i,:],y[i,:],'r-') pyplot.xlabel('x /m') pyplot.ylabel('y /m') pyplot.show() 非常感

我有两个2D阵列,我想在散点图中显示数据,这样看起来像点在移动。所以我希望第一组x和y数据被绘制,然后消失,被下一组x和y数据替换,等等

我目前使用的代码只是绘制所有数据点并将它们连接起来,有效地跟踪数据点的路径

pyplot.figure()
    for i in range(0,N):
        pyplot.plot(x[i,:],y[i,:],'r-')
pyplot.xlabel('x /m')
pyplot.ylabel('y /m')
pyplot.show()

非常感谢您的帮助。

matplotlib文档中包含一些可能有用的内容。它们都使用API,所以我建议您通读一下,了解一些想法。根据示例,下面是一条简单的动画正弦曲线,使用:


我以前见过这个,没有一个例子适用于我的程序。有没有什么方法可以让我得到我想要的,而不必重写设置x和y数组中数据的所有其他函数?
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)        # x-array
line, = ax.plot(x, np.sin(x))

def animate(i):
    line.set_ydata(np.sin(x+i/10.0))  # update the data
    return line,

#Init only required for blitting to give a clean slate.
def init():
    line.set_ydata(np.ma.array(x, mask=True))
    return line,

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
    interval=25, blit=True)
plt.show()