Matplotlib FuncAnimation,当blit = true时出错. [英] Matplotlib FuncAnimation, error when blit = true.

查看:38
本文介绍了Matplotlib FuncAnimation,当blit = true时出错.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用FuncAnimation和circle.set_radius()制作一个扩大的圆形的动画.但是,动画仅在blit = False时起作用.代码如下:

I am trying to make an animation of an expanding circle using FuncAnimation and circle.set_radius(). However, the animation only works when blit = False. The code is as follow:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation

fig, ax = plt.subplots()
plt.grid(True)
plt.axis([-0.6, 0.6, -0.6, 0.6])
circle1= plt.Circle([0,0],0.01,color="0.",fill=False, clip_on = True)
ax.add_patch(circle1)

dt = 1.0/20
vel = 0.1

def init():
    circle1.set_radius(0.01)

def animate(i):
    global dt, vel
    r = vel * i * dt
    circle1.set_radius(r)
    return circle1,

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval= 50, blit=True)

它返回此错误:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/figure.py", line 1139, in draw
    self.canvas.draw_event(renderer)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 1809, in draw_event
    self.callbacks.process(s, event)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/cbook.py", line 562, in process
    proxy(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/cbook.py", line 429, in __call__
    return mtd(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/animation.py", line 620, in _start
    self._init_draw()
  File "/usr/local/lib/python2.7/site-packages/matplotlib/animation.py", line 1166, in _init_draw
    for a in self._drawn_artists:
TypeError: 'NoneType' object is not utterable

我正在使用Mac OS.当我更改blit = False时,动画将起作用.但是,每当我移动鼠标时,动画就会变慢.这是有问题的,因为我有一个单独的线程来生成声音输出.实际上,圆会撞到一些数据点并发出声音.结果,它们不同步.请帮忙.

I am using mac OS. The animation will work when I changed blit = False. However, whenever I move my mouse there animation slow down. This is problematic because I have a separate thread generating the sound output. In practice, the circle will hit some data points and make sound. As a result they are not in sync. Please help.

推荐答案

从文档

如果blit = True,则func和init_func应该返回可迭代的drawable以清除.

If blit=True, func and init_func should return an iterable of drawables to clear.

因此-您需要将 return circle1 添加到函数 init()中.另一个选择是在调用 FuncAnimation 时根本不指定 init_func -您可能不需要它.没有这些动画,动画可能会做您想要的.

Therefore - you need to add return circle1, to your function init(). The other option is not to specify an init_func at all in your call to FuncAnimation - you may not need it. The animations probably does what you want without it.

注意 circle1 之后的尾部逗号-这意味着将返回一个(1个元素)元组,因此返回值可迭代为必需的.您已经在 animate 函数中使用了此功能.

NOTE the trailing comma after circle1 - this means a (1 element) tuple is returned, so that the return value is iterable as required. You already have this in the animate function.

这篇关于Matplotlib FuncAnimation,当blit = true时出错.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆