Python动画人物窗口无法自动关闭 [英] Python animation figure window cannot be closed automatically

查看:165
本文介绍了Python动画人物窗口无法自动关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 matplotlib.animation 创建动画并保存时,尝试播放时会出现错误通过 plt.close

When creating an animation using matplotlib.animation and saving it, an error appears when trying to close the figure window via plt.close:

Python版本:

Python 2.7.12 | Anaconda自定义(64位)| (默认值,2016年7月2日,17:42:40)

IPython 4.1.2-增强的交互式Python

Python 2.7.12 |Anaconda custom (64-bit)| (default, Jul 2 2016, 17:42:40)
IPython 4.1.2 -- An enhanced Interactive Python

当前,我切换到使用PyCharm 2017.1社区版在IPython或%cpaste %paste 中运行时,可以直接在IPython和PyCharm中重现该错误消息。使用Shift + Alt + E在PyCharm的交互式控制台中运行。影片编码器是整合在mplayer中的mencoder,因为这是我工作地点安装的默认编码器。

Currently I switched to using PyCharm 2017.1 Community Edition. The error message can be reproduced both directly in IPython and within PyCharm when running from %cpaste or %paste in IPython or running in PyCharm's interactive console using Shift+Alt+E. The movie encoder used is mencoder as integrated in mplayer, since this is the default one installed at my work place.

注意:


  • 在IPython中首先使用 plt.ion()打开交互模式(默认情况下已在PyCharm中打开)

  • 使用鼠标中键直接粘贴到IPython屏幕时,代码在IPython中无错误退出

  • 在键入所有命令时,在IPython或PyCharm中代码均无错误退出分别粘贴时(%cpaste %paste ),除了 plt以外的所有命令。 close(),然后手动键入 plt.close()

  • 替换 plt.close() plt.clf(),但我需要 plt.close( ),例如用于在具有不同参数的循环中创建动画,其中需要从头开始重新创建图形

  • 在脚本中从头到尾运行代码时,代码均无错误退出(非交互模式)

  • 代码在关闭图形窗口时无错误退出,用鼠标在窗口按钮上单击

  • 代码退出错误在 plt.close()之前插入 time.sleep(1)时,问题可能与时间无关代码冲突

  • in IPython use plt.ion() first to turn on interactive mode (already switched on in PyCharm by default)
  • code exits without error in IPython when pasted using the middle mouse button directly into the IPython screen
  • code exits without error in IPython or PyCharm when typing all commands separately and also when pasting (%cpaste, %paste) all commands except for plt.close() and then typing plt.close() manually
  • code exits without error when replacing plt.close() with plt.clf(), but I need plt.close(), e.g. for creating animations in a loop with different parameters where the graph needs to be recreated from scratch
  • code exits without error when running the code in a script from start to end (non-interactive mode)
  • code exits without error when closing the figure window clicking with the mouse on the window button
  • code exits with error when inserting time.sleep(1) before plt.close(), so the problem likely does not relate to time conflicts in the code

下面给出了一个最小,完整和(希望如此)可验证的示例:

A minimal, complete and (hopefully) verifiable example is given below:

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

# animation function for random image data
def animate_random_data(i):
    new_data = np.random.rand(10, 10)
    # update the data
    im.set_data(new_data)

# initialize the graph
first_data = np.random.rand(10,10)
im = plt.imshow(first_data,interpolation='none')
myfig = plt.gcf()

# create the animation and save it
ani = animation.FuncAnimation(myfig, animate_random_data, range(10), 
                              interval=100)
ani.save('animation_random_data.mpg', writer='mencoder')
plt.close()

错误回溯(来自PyCharm):

Error traceback (from PyCharm):

Traceback (most recent call last):  
  File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/backends/backend_qt5agg.py", line 176, in __draw_idle_agg
    FigureCanvasAgg.draw(self)  
  File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 474, in draw
    self.figure.draw(self.renderer)  
  File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)  
  File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/figure.py", line 1165, in draw
    self.canvas.draw_event(renderer)  
  File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 1809, in draw_event
    self.callbacks.process(s, event)  
  File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/cbook.py", line 563, in process
    proxy(*args, **kwargs)  
  File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/cbook.py", line 430, in __call__
    return mtd(*args, **kwargs)  
  File "/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib/animation.py", line 652, in _start
    self.event_source.add_callback(self._step)  
AttributeError: 'NoneType' object has no attribute 'add_callback'

尽管在关闭时程序会继续执行而不会出错按照上面列表中的说明手动操作Windows,这是一个令人讨厌的错误(想像一个循环中有多个动画)。错误也会出现在例如一维线图。感谢您的任何帮助(并弄清楚此错误消息的含义)!

Although the program continues without error when closing the windows manually as written in the list above, it is an annoying bug (think of multiple animations in a loop). The error appears also for e.g. 1D line plots. Thanks for any help (and clarification on what this error message exactly means)!

推荐答案

该错误来自动画仍在运行结束图。虽然在大多数情况下,关闭图形时会自动停止动画,但在交互模式下似乎并非如此。

The error comes from the animation still running while closing the figure. While in most cases it is automatically taken care of to stop the animation when closing the figure, it seems not to be the case in interactive mode.

解决方案可以是

ani = animation.FuncAnimation(...)
ani.save(...)
ani.event_source.stop()
del ani
plt.close()

这篇关于Python动画人物窗口无法自动关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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