【问题标题】:Can I change the interval of a previously created matplotlib FuncAnimation?我可以更改以前创建的 matplotlib FuncAnimation 的间隔吗?
【发布时间】:2017-06-12 11:19:21
【问题描述】:

我试图弄清楚是否有任何方法可以更改现有 matplotlib FuncAnimation 的间隔。我希望能够根据用户输入来调整动画的速度。

我发现了一个类似的问题How do I change the interval between frames (python)?,但由于没有答案,我想我还是会问它。

我需要和拥有的一个最小示例是:

"""
Based on Matplotlib Animation Example

author: Jake Vanderplas
https://stackoverflow.com/questions/35658472/animating-a-moving-dot
"""
from matplotlib import pyplot as plt
from matplotlib import animation
import Tkinter as tk
import numpy as np

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg


class AnimationWindow(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.fig = plt.figure(0, figsize=(10, 10))

        self.anim = None

        self.speed = 2

        self.canvas = FigureCanvasTkAgg(self.fig, self)
        self.canvas.show()
        self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        self.canvas.mpl_connect('resize_event', self.on_resize)

        self.bar = tk.Scale(self, from_=0.25, to=10, resolution=0.25, command=self.change_play_speed, orient=tk.HORIZONTAL)
        self.bar.pack(fill=tk.X)

    def start_animation(self):
        ax = plt.axes()

        self.x = np.arange(0, 2 * np.pi, 0.01)
        self.line, = ax.plot(self.x, np.sin(self.x))

        # The return needs to be assigned to a variable in order to prevent the cleaning by the GC
        self.anim = animation.FuncAnimation(self.fig, self.animation_update, frames=100,
                                            interval=100/self.speed, blit=True, repeat=False)

    def animation_update(self, i):
        self.line.set_ydata(np.sin(self.x + i / 10.0))  # update the data
        return self.line,

        return tuple(self.annotation)

    def change_play_speed(self, speed):
        self.speed = float(speed)

        # This works but I think somehow the previous animation remains
        #self.anim = animation.FuncAnimation(self.fig, self.animation_update, frames=100, interval=100/self.speed, blit=True, repeat=False)

    def on_resize(self, event):
        """This function runs when the window is resized.
         It's used to clear the previous points from the animation which remain after resizing the windows."""

        plt.cla()


def main():
    root = tk.Tk()

    rw = AnimationWindow(root)
    rw.pack()

    rw.start_animation()

    root.mainloop()

if __name__ == '__main__':
    main()

在更改速度功能中,我对此问题有一个注释解决方案。该解决方案存在两个主要问题:它很可能非常低效(我认为);而且我还没有想出办法让我删除之前导致闪烁的动画。

【问题讨论】:

    标签: python python-2.7 animation matplotlib


    【解决方案1】:

    我不建议删除动画。更复杂的动画的一种选择当然是手动编程。使用一个重复调用更新函数的计时器实际上并不比创建FuncAnimation多多少代码。

    但是在这种情况下,解决方案非常简单。只需更改底层event_source的区间即可:

    def change_play_speed(self, speed):
        self.speed = float(speed)
        self.anim.event_source.interval = 100./self.speed
    

    【讨论】:

      猜你喜欢
      • 2018-10-05
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      • 1970-01-01
      相关资源
      最近更新 更多