当程序启动时,我想打开一个带有动画的窗口,但它必须从一开始就暂停.

我试着从https://matplotlib.org/stable/gallery/animation/pause_resume.html开始编辑这个例子.


from matplotlib import pyplot as plt

import matplotlib.animation as animation

import numpy as np



class PauseAnimation:
    def __init__(self):
        fig, ax = plt.subplots()

        x = np.linspace(-0.1, 0.1, 1000)

        # Start with a normal distribution
        self.n0 = (1.0 / ((4 * np.pi * 2e-4 * 0.1) ** 0.5) * np.exp(-x ** 2 / (4 * 2e-4 * 0.1)))
        self.p, = ax.plot(x, self.n0)

        self.animation = animation.FuncAnimation(fig, self.update, frames=200, interval=50, blit=True)

        self.animation.pause()  # not working
        self.animation.event_source.stop()  # not working

        self.paused = True

        fig.canvas.mpl_connect('button_press_event', self.toggle_pause)

    def toggle_pause(self, *args, **kwargs):
        if self.paused:
            self.animation.resume()
        else:
            self.animation.pause()
        self.paused = not self.paused

    def update(self, i):
        self.n0 += i / 100 % 5
        self.p.set_ydata(self.n0 % 20)
        return self.p,


pa = PauseAnimation()
plt.show()

我加了self.animation.pause()self.animation.event_source.stop(),但似乎没有效果,动画还在播放.我如何才能实现这一点?

推荐答案

为了能够暂停动画,您需要主循环已经在运行,也就是说,已经调用了show.

一个非常简单的解决方案是,不要在启动时暂停动画,而是不要启动它,例如:

import matplotlib.pyplot as plt
import numpy as np

import matplotlib.animation as animation

class PauseAnimation:
    def __init__(self):
        self.fig, ax = plt.subplots()

        x = np.linspace(-0.1, 0.1, 1000)

        # Start with a normal distribution
        self.n0 = (1.0 / ((4 * np.pi * 2e-4 * 0.1) ** 0.5) * np.exp(-x ** 2 / (4 * 2e-4 * 0.1)))
        self.p, = ax.plot(x, self.n0)
        
        self.paused = True
        self.animation = None
        
        self.fig.canvas.mpl_connect('button_press_event', self.toggle_pause)


    def toggle_pause(self, *args, **kwargs):
        if self.animation is None:
            self.animation = animation.FuncAnimation(
                self.fig, self.update, frames=200, interval=50, blit=True)
        if self.paused:
            self.animation.resume()
        else:
            self.animation.pause()
        self.paused = not self.paused

    def update(self, i):
        self.n0 += i / 100 % 5
        self.p.set_ydata(self.n0 % 20)
        return self.p,


pa = PauseAnimation()
plt.show()

Python相关问答推荐

Python -按第一个条目减go 日期?

Pandas 修改原始excel

Pandas或pyspark跨越列创建

Python-Polars:如何用两个值的平均值填充NA?

如何处理必须存在于环境中但无法安装的Python项目依赖项?

如何让pyparparsing匹配1天或2天,但1天和2天失败?

跟踪我已从数组中 Select 的样本的最有效方法

无法使用equals_html从网址获取全文

如何根据日期和时间将状态更新为已过期或活动?

如何使用Google Gemini API为单个提示生成多个响应?

时间序列分解

ModuleNotFound错误:没有名为Crypto Windows 11、Python 3.11.6的模块

使用索引列表列表对列进行切片并获取行方向的向量长度

切片包括面具的第一个实例在内的眼镜的最佳方法是什么?

pyscript中的压痕问题

在Python argparse包中添加formatter_class MetavarTypeHelpFormatter时, - help不再工作""""

关于Python异步编程的问题和使用await/await def关键字

pandas在第1列的id,第2列的标题,第3列的值,第3列的值?

调用decorator返回原始函数的输出

可以bcrypts AES—256 GCM加密损坏ZIP文件吗?