2013-12-19 16 views
0

我有兩個二維數組,我想在散點圖中顯示數據,所以它看起來像點正在移動。所以我想要繪製第一組x和y數據,然後消失,以被下一組x和y數據取代。如何使用matplotlib動畫我的圖形,使其看起來像數據點正在移動?

我目前只是繪製了所有數據點並將它們連接起來,有效地追蹤數據點的路徑。

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

任何幫助,非常感謝。

回答

1

matplotlib文檔包含一些可能有用的animation examples。他們都使用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() 
+0

我見過此之前,沒有任何的例子似乎適用於我的計劃。有沒有什麼辦法可以得到我想要的,而不必重寫所有其他設置x和y數組中數據的函數? – MaunaKea

相關問題