【问题标题】:pyplot rectangle animation leaves initial rectangle displayedpyplot 矩形动画显示初始矩形
【发布时间】:2016-04-06 10:31:32
【问题描述】:

我是 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) 的矩形始终停留在屏幕上,而动画矩形的行为符合预期。我想不出为什么。我尝试了一些小的更改,但找不到解决方案。我做错了什么?

【问题讨论】:

    标签: python animation matplotlib rectangles


    【解决方案1】:

    您需要在 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()
    

    【讨论】:

    • 解决了这个问题。使用 init 方法的解决方案适用于我(适用于 Windows 的 Anaconda Python 3.4),blit 为 True 或 False。第二种解决方案确实需要将 blit 设置为 False。
    • 很好,很高兴我能帮上忙。我认为blit必须在第二种情况下设置为False,因为blit必须首先delete改变元素;在没有init 方法的情况下,没有要删除的元素,它会引发错误。
    【解决方案2】:

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

    【讨论】:

    • 这确实是我困惑的根源。我不记得在文档或教程中阅读过那个重要的评论。
    猜你喜欢
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2012-10-28
    • 2017-03-08
    • 1970-01-01
    • 2016-12-03
    相关资源
    最近更新 更多