繁体   English   中英

如何使用matplotlib为我的图形制作动画,以便看起来数据点在移动?

[英]How can I animate my graph with matplotlib so that it looks like the data points are moving?

我有两个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文档包含一些可能有用的动画示例 他们都使用matplotlib.animation API,因此建议您通读一下以获取一些想法。 从示例中,这是一个使用FuncAnimation的简单动画正弦曲线:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM