如何在python pyplot动画

时间:2016-12-21 11:27:37

标签: python animation matplotlib

我想在我的图表上同时动画两组补丁。例如椭圆和箭头。我可以单独执行此操作但不能立即执行此操作。我使用pyplot动画,FuncAnimation。

我收到以下错误:

文件“/usr/lib/python3/dist-packages/matplotlib/animation.py”,第1199行,在_init_draw中     a.set_animated(self._blit) AttributeError:'list'对象没有属性'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)

1 个答案:

答案 0 :(得分:1)

我认为你的问题是你的animate函数返回了2个列表的元组(因为self.pedestriansself.arrows都是List)。

animate函数应该返回一个艺术家列表。

你应该尝试类似的东西:

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