簡體   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