2016-11-20 143 views
0

我的代碼三點運動:語法繪製使用FuncAnimation

fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 

def animate(i): 
    ax.set_data(ax.scatter(ptx1, pty1, ptz1, c='red'), 
     ax.scatter(ptx2, pty2, ptz2, c='blue'), 
     ax.scatter(ptx3, pty3, ptz3, c='green')) 

ani = FuncAnimation(fig, animate, frames=10, interval=200) 

plt.show() 

我試圖繪製三個點的運動。每個ptx/y/z/1/2/3是一個給出點座標的浮點列表。我只是不確定如何使用FuncAnimation來激活我的觀點。任何幫助將不勝感激!

+0

運行此錯誤時是否出現錯誤? – Inconnu

+0

'FuncAnimations'(有'frames = 10')調用'animate' 10次。每次調用時都必須更改數據。這樣你就可以獲得動畫。 'i'是當前幀號,所以你可以用它從列表中獲取不同的數據。 – furas

+0

那麼我應該嘗試一個for循環遍歷動畫中的我的列表? – Monica

回答

0

簡單的例子。多次調用animate,並且每次您必須使用不同的數據才能看到動畫。

import matplotlib.pyplot as plt 
from matplotlib.animation import FuncAnimation 
import random 

# create some random data  
ptx1 = [random.randint(0,100) for x in range(20)] 
pty1 = [random.randint(0,100) for x in range(20)] 

fig = plt.figure() 
ax = fig.add_subplot(111) 

def animate(i): 
    # use i-th elements from data 
    ax.scatter(ptx1[:i], pty1[:i], c='red') 

    # or add only one element from list 
    #ax.scatter(ptx1[i], pty1[i], c='red') 

ani = FuncAnimation(fig, animate, frames=20, interval=500) 

plt.show() 
+0

非常感謝你@furas!這正是我所需要的。我不是從第i個元素中選取的,所以它不起作用 – Monica