我可以更改以前创建的matplotlib FuncAnimation的间隔吗? - python

我正在尝试找出是否可以更改现有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()

在更改速度功能中,我对这个问题有一个评论性的解决方案。这种解决方案存在两个主要问题:它很可能效率很低(我认为);而且我还没有找到删除前一个动画的方法,该动画会导致闪烁。

参考方案

我不建议删除动画。当然,更复杂的动画的一种选择是手动对其进行编程。实际上,使用计时器重复调用更新功能实际上并不比创建FuncAnimation多得多的代码。

但是,在这种情况下,解决方案非常简单。只需更改基础event_source的间隔:

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

Python GPU资源利用 - python

我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…

Python:图像处理可产生皱纹纸效果 - python

也许很难描述我的问题。我正在寻找Python中的算法,以在带有某些文本的白色图像上创建皱纹纸效果。我的第一个尝试是在带有文字的图像上添加一些真实的皱纹纸图像(具有透明度)。看起来不错,但副作用是文本没有真正起皱。所以我正在寻找更好的解决方案,有什么想法吗?谢谢 参考方案 除了使用透明性之外,假设您有两张相同尺寸的图像,一张在皱纹纸上明亮,一张在白色背景上有深…

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

python:ConfigParser对象,然后再阅读一次 - python

场景:我有一个配置文件,其中包含要执行的自动化测试的列表。这些测试是长期循环执行的。   配置文件的设计方式使ConfigParser可以读取它。由于有两个三个参数,因此我需要通过每个测试。现在,此配置文件由script(s1)调用,并且按照配置文件中的列表执行测试。Script(s1)第一次读取配置,并且在每次测试完成后都会执行。阅读两次的要求:由于可能会…