2017-08-29 88 views
1

我生成了一個腳本,我不顯示,但存儲硬盤驅動器的很多數字。過了一會兒,我得到的消息蟒蛇plt:關閉或清除圖不起作用

/usr/lib/pymodules/python2.7/matplotlib/pyplot.py:412:RuntimeWarning:超過20倍的數字已被打開。通過pyplot接口創建的數字(matplotlib.pyplot.figure)會一直保留,直到顯式關閉並可能消耗太多內存。 (要控制此警告,請參閱rcParam figure.max_num_figures)。 max_open_warning,RuntimeWarning)

因此,我試圖關閉或清除存儲後的數字。到目前爲止,我嘗試了以下所有方面,但沒有人能夠工作。我仍然從上面得到消息。

plt.figure().clf() 
plt.figure().clear() 
plt.clf() 
plt.close() 
plt.close('all') 
plt.close(plt.figure()) 

並且此外我試圖通過

plt.rcParams.update({'figure.max_num_figures':1}) 

限制的開放數字的數目在這裏如下一塊,其行爲類似於上述樣本代碼。我在我嘗試過的地方添加了我嘗試的不同選項。

from pandas import DataFrame 
from numpy import random 
df = DataFrame(random.randint(0,10,40)) 

import matplotlib.pyplot as plt 
plt.ioff() 
#plt.rcParams.update({'figure.max_num_figures':1}) 
for i in range(0,30): 
    fig, ax = plt.subplots() 
    ax.hist([df]) 
    plt.savefig("/home/userXYZ/Development/pic_test.png") 
    #plt.figure().clf() 
    #plt.figure().clear() 
    #plt.clf() 
    #plt.close() # results in an error 
    #plt.close('all') # also error 
    #plt.close(plt.figure()) # also error 

是完整的,即使用plt.close,當我得到的錯誤:

不能援引「事件」命令:執行應用程序已經被破壞 「事件產生$ W <> 「 (程序 」TTK :: ThemeChanged正確的方法TTK :: ThemeChanged 「

+0

爲了確保您總是工作在同一個圖上,只是使用這個'plt.figure(1).clf()' – Julien

+2

plt.close(fig)should do it – Y0da

+0

@Julien這也會產生RuntimeWarning – AnnetteC

回答

0

」 第6行) 從內部調用的「 關閉您的數字會可以使用plt.close(fig),這可以在您最初發布的代碼的下面編輯中看到。

from pandas import DataFrame 
from numpy import random 
df = DataFrame(random.randint(0,10,40)) 

import matplotlib.pyplot as plt 
plt.ioff() 
for i in range(0,30): 
    fig, ax = plt.subplots() 
    ax.hist(df)   
    name = 'fig'+str(i)+'.png' # Note that the name should change dynamically 
    plt.savefig(name) 
    plt.close(fig)    # <-- use this line 

的錯誤,你描述你的問題的最後建議,我認爲你的問題是不是與matplotlib,而是與您的代碼(如ttk)的另一部分。

+0

我運行了你的代碼,但它也導致ttk錯誤 - 不知道原因。我不想有一個情節,但直方圖... – AnnetteC

+0

你可以將'plt.plot(df)'行與你生成直方圖的方式交換,我只是沒有把你的'ax.hist([df])'修改成'ax.hist(df)',我有修改了現在的答案。但是,你的問題是關於如何關閉這個數字,這正是我所關注的。 –

+0

'ax.hist(df)'產生混亂 - 我沒有得到酒吧,但只有一個實心方形。仍然:'plt.close(fig)'產生'ttk'錯誤,我沒有找到任何解決方案。 – AnnetteC

-1

這並不能真正解決我的問題,但它是一個變通處理我所面臨的高內存消耗,我沒有得到任何的錯誤消息像以前一樣:

from pandas import DataFrame 
from numpy import random 
df = DataFrame(random.randint(0,10,40)) 

import matplotlib.pyplot as plt 
plt.ioff() 
for i in range(0,30): 
    plt.close('all') 
    fig, ax = plt.subplots() 
    ax.hist([df]) 
    plt.savefig("/home/userXYZ/Development/pic_test.png")