pyplot 矩形动画显示初始矩形

pyplot rectangle animation leaves initial rectangle displayed

我是 matplotlib 的新手,我正在尝试了解动画的工作原理。

我编写了以下 Python 代码来围绕矩形移动:

import matplotlib.pyplot as plt
import matplotlib.animation as animation

class AnimRect(object):
    '''Animate a rectangle'''
    def __init__(self):
        self.fig = plt.figure(figsize = (5,5))
        # create the axes
        self.ax = plt.axes(xlim=(0,100), ylim=(0,100), aspect='equal')
        # create rectangle
        self.rect = plt.Rectangle((70,20), 10, 10, 
                                  fill=True, color='gold', ec='blue')
        self.ax.add_patch(self.rect)

    # initialization function
    def init(self):
        self.rect.set_xy((10.0, 10.0))
        return self.rect, 

    # animation function
    def animate(self, i):
        self.rect.set_xy((i,i))
        return self.rect, 

    def call_animation(self):
        # call the animator function
        animation.FuncAnimation(self.fig, self.animate, frames=91, 
                                init_func= self.init,
                                interval=20, blit=True, repeat=False)

        plt.show()

def main():
    rect = AnimRect()
    rect.call_animation()

main()

当我 运行 代码时,设置在初始位置 (10.0,10.0) 的矩形始终保留在屏幕上,而动画矩形的行为符合预期。我不知道为什么。我尝试了几个小改动,但找不到解决方案。我做错了什么?

初始化函数init_func初始化每一帧,而不是整个动画;也就是说,它在每一帧的开头被调用。把它扔出去,看看会发生什么。

您需要在 init 方法中将补丁的可见性设置为 False,或者完全取消它。

使用 init 方法 - 在某些情况下很有用: 请注意,我设置 blit=false 是因为它在 mac os 后端有一些奇怪的行为。

import matplotlib.pyplot as plt
import matplotlib.animation as animation

class AnimRect(object):
    '''Animate a rectangle'''
    def __init__(self):
        self.fig = plt.figure(figsize = (5,5))
        # create the axes
        self.ax = plt.axes(xlim=(0,100), ylim=(0,100), aspect='equal')
        # create rectangle
        self.rect = plt.Rectangle((70,20), 10, 10, 
                                  fill=True, color='gold', ec='blue')
        self.ax.add_patch(self.rect)
        self.call_animation()
        plt.show()

    # initialization function
    def init(self):
        self.rect.set_xy((10.0, 10.0))
        self.rect.set_visible(False)
        return self.rect, 

    # animation function
    def animate(self, i):
        self.rect.set_xy((i,i))
        self.rect.set_visible(True)
        return self.rect, 

    def call_animation(self):
        # call the animator function
        self.anim = animation.FuncAnimation(self.fig, self.animate, frames=91,
                                init_func= self.init,
                                interval=20, blit=False, repeat=True)


def main():
    rect = AnimRect()

main()

没有初始化:

import matplotlib.pyplot as plt
import matplotlib.animation as animation

class AnimRect(object):
    '''Animate a rectangle'''
    def __init__(self):
        self.fig = plt.figure(figsize = (5,5))
        # create the axes
        self.ax = plt.axes(xlim=(0,100), ylim=(0,100), aspect='equal')
        # create rectangle
        self.rect = plt.Rectangle((70,20), 10, 10, 
                                  fill=True, color='gold', ec='blue')
        self.ax.add_patch(self.rect)
        self.call_animation()
        plt.show()

    # animation function
    def animate(self, i):
        self.rect.set_xy((i,i))
        self.rect.set_visible(True)
        return self.rect, 

    def call_animation(self):
        # call the animator function
        self.anim = animation.FuncAnimation(self.fig, self.animate, frames=91,
                                interval=20, blit=False, repeat=True)

def main():
    rect = AnimRect()

main()