【问题标题】:Matplotlib FuncAnimation frame not rendering when a new artist added添加新艺术家时,Matplotlib FuncAnimation 框架未渲染
【发布时间】:2020-05-18 07:34:55
【问题描述】:

我正在 Matplotlib 中制作动画,每隔几帧就会添加新的艺术家(特别是补丁),但是当我运行它时,添加新艺术家的每一帧都是完全空白的。我知道 blitting 存在一些问题,因为当我关闭它时它可以工作,但我需要它。我返回每个帧中创建或修改的每个形状,就像文档要求的那样。我正在使用 MacOSX 后端。

我的代码如下所示:

from random import random
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig = plt.figure()
axe = fig.add_axes([0, 0, 1, 1], frameon=False)
circles = []

def update(i):
    if not i % 10:
        new_circle = plt.Circle((random(), random()), 0.05, color='black')
        axe.add_patch(new_circle)
        circles.append(new_circle)

    for circle in circles:
        circle.center = (random(), random())

    return circles

animation = FuncAnimation(fig, update, frames=60, interval=1000/30, repeat=False, blit=True)
plt.show()

【问题讨论】:

  • 您的代码对我来说似乎按预期工作。我正在使用 Python 3.7.4、matplotlib v.3.1.3、Jupyter notebook 6.0.3(带有笔记本后端)。我无法尝试 MacOS 后端 atm。
  • add_patch 可能会导致轴范围的更新,因此这可能是您在 blitting 时遇到冲突的原因。尝试改用add_artist(没有那种副作用)。
  • 我设置的帧速率为 30 时有点难以看出错误,但它始终存在于 MacOSX 后端。使用 add_artist 会产生同样的问题。但是,切换到不同的后端(在我的例子中是 TkAgg)解决了这个问题。我会将此错误添加到 matplotlib 的 github 页面。

标签: python matplotlib matplotlib-animation


【解决方案1】:

这似乎是 MacOSX 后端中 matplotlib 的一个错误,因此解决方案只是通过使用不同的后端或尽可能不使用 blitting 来解决它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-14
    • 2017-02-19
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多