简体   繁体   中英

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. 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.

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. They all use the matplotlib.animation API, so I'd suggest you read through that for some ideas. From the examples, here's a simple animated sine curve using 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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