2017-06-16 38 views
0

我有一個對象A實例化一個對象B,並在其構造方法的對象B什麼導致FuncAnimation func被調用?

self.ani = animation.FuncAnimation(
      self.figure, func=self.gen_data, fargs=[self.data_dict], 
      blit=True, repeat=False) 

然而,self.gen_method不會被調用。我該如何確保事實並非如此?

我最終什麼事做是把上面的代碼在獲取對象A,然後調用對象B一個refresh()方法引發的回調,但我不希望保留重建動畫。應該有一種方法來構建它只有一次,但如何?

那麼什麼是解決方案,我如何確保func被定期調用?

+0

哪裏是你'self.gen_method'? – Ding

+0

它需要一個數據字典self.data_dict,並在圖上創建圖。 它的簽名是 def gen_data(self,_,data_dict)。 _用於框架的索引,該框架未使用,但由FuncAnimation默認通過參數傳遞。 –

+0

所有人都可以用這裏提供的少量信息說,FuncAnimation是正確構建的。您可能想要將幀數或生成器添加爲幀參數,例如'frames = 40',但如果這是必要的將取決於gen_data方法。 – ImportanceOfBeingErnest

回答

0

這裏是一個Minimal, Complete, and Verifiable example使用正是你的代碼和工作得很好:

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
import numpy as np 

class A(): 
    def __init__(self): 
     self.b = B() 

class B(): 
    def __init__(self): 
     self.figure = plt.figure() 
     plt.axis((0,1,0,1)) 
     self.l1, = plt.plot([],[], color="r") 
     self.data_dict = np.random.rand(40,10) 
     self.ani = animation.FuncAnimation(
      self.figure, func=self.gen_data, fargs=[self.data_dict], 
      blit=True, repeat=False) 

    def gen_data(self, _, data): 
     self.l1.set_data(np.linspace(0,1,10), data[_%40,:]) 
     return self.l1, 

a = A() 
plt.show() 
+0

實際上,A在A的構造函數中創建B,B在它自己的構造函數中創建動畫。你可以試試類似的東西嗎? –

+0

沒有理由爲什麼這不應該工作,但我更新答案有A創造B. – ImportanceOfBeingErnest

相關問題