2016-12-21 47 views
0

我想在我的圖表上同時創建兩組修補程序的動畫。例如橢圓和箭頭。我可以分開做,但不能一次。我使用pyplot動畫,FuncAnimation。如何在Python pyplot動畫中同時動畫兩個修補程序

我得到如下錯誤:

文件 「/usr/lib/python3/dist-packages/matplotlib/animation.py」,線1199,在_init_draw a.set_animated(self._blit) AttributeError的:「名單」對象有沒有「set_animated」

的代碼如下屬性:

def initiate_chart(self, axes, title='Layout'): 
    self.title = title 
    plt.title = self.title 
    self.ax = plt.axes(xlim=(axes[0]['xmin'], axes[0]['xmax']), ylim=(axes[0]['ymin'], axes[0]['ymax'])) 
    elipses = [Ellipse(i, width=0.6, height=0.3, angle=0) for i in self.trajectory[0]] 
    arrows = [Arrow(x, y, self.velocities[0][i][0], self.velocities[0][i][1]) 
       for (i, (x, y)) in enumerate(self.trajectory[0])] 
    [self.pedestrians.append(self.ax.add_patch(elipses[i])) for i in range(len(elipses))] 
    [self.arrows.append(self.ax.add_patch(arrows[i])) for i in range(len(arrows))] 

def init_animation(self): 
    [self.pedestrians[i].set_visible(True) for i in range(len(self.pedestrians))] 
    [self.arrows[i].set_visible(True) for i in range(len(self.arrows))] 
    return self.pedestrians, self.arrows 

def animate(self, i): 
    self.arrows = [] 
    for j in range(len(self.pedestrians)): 
     angle = degrees(atan2(self.trajectory[i][j][0], self.trajectory[i][j][1])) 
     self.pedestrians[j].center = self.trajectory[i][j] 
     self.pedestrians[j].angle = angle 
     self.arrows.append(self.ax.add_patch(Arrow(self.trajectory[i][j][0], self.trajectory[i][j][1], 
           self.velocities[i][j][0], self.velocities[i][j][1], width=0.5))) 
    return self.pedestrians, self.arrows 

def do_animation(self, n_frames, n_interval): 
    anim = animation.FuncAnimation(self.fig, self.animate, init_func=self.init_animation, frames=n_frames, 
            interval=n_interval, blit=True) 

回答

0

我覺得你的問題是,你的animate函數返回的2只列出一個元組(因爲self.pedestriansself.arrows各自是一個列表)。

animate函數應返回一個藝術家列表。

你應該嘗試這樣的:

def animate(self, i): 
    (...) 
    artists = self.pedestrians + self.arrows # concatenate lists of artists 
    return artists 
+0

謝謝你,這就是問題所在。工作正常。 –

+0

不客氣。如果您的問題現在得到解答,請考慮通過點擊左側的複選標記來接受答案 –