2017-05-04 56 views
1

創建時使用matplotlib.animation動畫並保存它,嘗試通過plt.close關閉窗口身影出現時,一個錯誤:Python的動畫人物窗口不能自動關閉

Python版本:

的Python 2.7.12 | Anaconda自定義(64位)| (默認情況下,2016年7月2日,17點42分40秒)
IPython的4.1.2 - 增強的交互式Python

目前我切換到使用PyCharm 2017.1社區版。該錯誤消息既可以直接在IPython中和內PyCharm從%cpaste%paste在IPython中運行或使用Shift + Alt鍵+ E在PyCharm的交互式控制檯上運行時進行復制。使用的電影編碼器是mencoder作爲集成在mplayer中,因爲這是安裝在我工作地點的默認編碼器。

注:

  • 在IPython中使用plt.ion()第一開啓交互模式中的IPython(已經在PyCharm接通默認情況下)
  • 代碼退出而不錯誤粘貼時使用鼠標中鍵直接進入IPython的屏幕
  • 無錯誤分別鍵入的所有命令時,並且還粘貼( %cpaste%paste)的所有命令時,除了 plt.close()然後鍵入 plt.close()馬努
  • 代碼退出在IPython的或PyCharm烯丙基用plt.clf()替換plt.close()
  • 代碼退出沒有錯誤,但我需要plt.close(),例如用於在具有不同參數的環路,其中所述圖中運行的腳本從開始到結束(非交互模式)的代碼時需要從頭開始重新創建
  • 代碼退出,而不錯誤
  • 代碼退出,而不錯誤時創建動畫用鼠標關閉圖窗口點擊窗口按鈕
  • 插入time.sleep(1)plt.close()之前時,所以問題可能不涉及時間衝突中的代碼的代碼退出錯誤

甲最小的,完整的和(跳躍efully)可驗證例子下面給出:

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):

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' 

雖然手動關閉車窗時寫在上面的列表中的程序繼續而不會出現錯誤,它是一種惱人的錯誤(想想循環中的多個動畫)。該錯誤也出現在例如一維線圖。感謝您的任何幫助(並澄清這個錯誤信息究竟意味着什麼)!

回答

1

這個錯誤來自同時關閉數字仍在運行的動畫。雖然在大多數情況下,在關閉圖形時會自動停止動畫,但在交互模式下似乎不是這種情況。

一個解決方案可以是明確地停止動畫並關閉數字之前將其刪除。

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

有趣的是退出程序,'ani.event_source.stop()'直接輸入時,它停在交互模式的動畫,但不能粘貼時。如果之後使用了del ani,則在粘貼動畫的情況下停止在特定的幀處。還要考慮到,如果沒有'del ani','plt.close()'仍然會像以前一樣提供相同的錯誤,但是包括兩條線都可以。 – bproxauf

+0

評論已被接受爲答案,因爲所提議的方法用於防止錯誤消息。然而,我仍然不明白爲什麼在關閉窗口之前停止動畫是不夠的。 – bproxauf

0

在這種情況下,你可以只是exit()

+0

正如問題指定的那樣,退出將不允許循環中的多個動畫。 – ImportanceOfBeingErnest