2017-08-29 91 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")