2017-01-05 79 views
0

我已经创造了一些动画后就正常了 - 尽管 - 缓凝时关键字:matplotlib动画块传输=真正原因KeyError异常

blit=False 

然而,在我的努力通过设定块传输,以加快动画=真我在运行我的测试时遇到了一些奇怪的KeyError异常。

最后,我有一个预感,它可能不会有一个编码错误,但也许有一个设置,甚至一个错误。

因此我从here导入了simple_anim.py脚本,发现发生了同样的错误。

我测试过一些例子,他们都给出了相同的异常.... :(

任何人可以帮助我,并给上发生了什么事情的一些信息?

的代码是以下:

""" 
A simple example of an animated plot 
""" 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

fig, ax = plt.subplots() 

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


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


# Init only required for blitting to give a clean slate. 
def init(): 
    line.set_ydata(np.ma.array(x, mask=True)) 
    return line, 

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init, 
           interval=25, blit=True) 
plt.show() 

凸起的例外是:

Traceback (most recent call last): 
    File "/home/dj754/anaconda3/lib/python3.5/site-packages/matplotlib/backend_bases.py", line 1305, in _on_timer 
    ret = func(*args, **kwargs) 
    File "/home/dj754/anaconda3/lib/python3.5/site-packages/matplotlib/animation.py", line 1021, in _step 
    still_going = Animation._step(self, *args) 
    File "/home/dj754/anaconda3/lib/python3.5/site-packages/matplotlib/animation.py", line 827, in _step 
    self._draw_next_frame(framedata, self._blit) 
    File "/home/dj754/anaconda3/lib/python3.5/site-packages/matplotlib/animation.py", line 845, in _draw_next_frame 
    self._pre_draw(framedata, blit) 
    File "/home/dj754/anaconda3/lib/python3.5/site-packages/matplotlib/animation.py", line 858, in _pre_draw 
    self._blit_clear(self._drawn_artists, self._blit_cache) 
    File "/home/dj754/anaconda3/lib/python3.5/site-packages/matplotlib/animation.py", line 898, in _blit_clear 
    a.figure.canvas.restore_region(bg_cache[a]) 
KeyError: <matplotlib.axes._subplots.AxesSubplot object at 0x7fb3b9d3a198> 
+1

我只有'interval'低于'60'的问题 - 我的错误表明问题始于用于显示窗口的Tkinter。当然,程序启动两个线程(或者更确切的说,使用Tkinter'after()'后周期性地执行'animate()',一些元素的启动速度会比其他元素快,然后是第一个元素所需的其他元素 – furas

+0

当我在家运行这个代码时 - 认为是 - 相同的软件配置:一切正常但是... 我已经能够重新创建相同的KeyError的东西,当我插入一个'plt.show()'语句之前创建动画与'FuncAnimation ....)'电话 ...我有一种感觉,我正在进行一些事情。 –

+0

..经过一番进一步的调查,我发现在代码的开头添加'plt.ion()'语句会导致它给出这个'KeyError'异常,**只有当'blit = True'被设置。当我将其更改为'blit = False'时,它仍然工作。 不幸的是,我无法在工作中测试我的代码(根据我提出的这个问题),但我很快就会做到这一点。 –

回答

0

经过这么我在家里调试,我已经找到了我自己的问题的答案。我怀疑这是ipython中的一个错误,但我不想肯定地说明这一点,因为99 +%的时间,错误在屏幕和屏幕前面的椅子背面之间。

事实证明,它运行我提供我自己的剧本源于我做了什么前:通过plt.ion()

开启互动这是一个新手的错误,我已经因为纠正。然而,我在simple_anim.py之前运行的脚本是通过run simple_anim.py' (I prefer developing with IPython.. legacy of my MATLAB experiences). I've forgotten to mention this, but this turned out to be critical. If I had tried running the code *the normal way* via在IPython3中调用的,python3 simple_anim.py'一切都会正常工作!

但事实证明,将交互模式设置为打开,而IPython是异常的原因。

我非常清楚,通过plt.ion()在IPython中启用交互模式非常愚蠢,但是,它发生在我身上,我怀疑更多的人遭受了这种痛苦。因此,它认为这种行为(引起这种异常)至少是不必要的行为,并且可能是Ipython或matplotlib.pyplot.ion()函数中的一个错误。

有没有人有任何想法,如果我应该提及这个也许 -bug?