[英]How can I animate my graph with matplotlib so that it looks like the data points are moving?
I have two 2D arrays and I want to display the data in a scatter graph so it will look like the points are moving. 我有两个2D数组,我想在散点图中显示数据,以便看起来像点在移动。 So I want the first set of x and y data to be plotted then disappear to be replaced by the next set of x and y data etc.
所以我想绘制第一组x和y数据,然后消失以替换为下一组x和y数据,等等。
the code I have currently just plots all the data points and joins them up, effectively tracing out the paths of the data points. 我目前使用的代码只是绘制所有数据点并将它们连接起来,从而有效地找出数据点的路径。
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()
any help is much appreciated. 任何帮助深表感谢。
The matplotlib
docs contain some animation examples that may be useful. matplotlib
文档包含一些可能有用的动画示例 。 They all use the matplotlib.animation
API, so I'd suggest you read through that for some ideas. 他们都使用
matplotlib.animation
API,因此建议您通读一下以获取一些想法。 From the examples, here's a simple animated sine curve using FuncAnimation
: 从示例中,这是一个使用
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.