Python plt:关闭或清除图不起作用 [英] Python plt: close or clear figure does not work

查看:888
本文介绍了Python plt:关闭或清除图不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用脚本生成了大量图形,这些脚本我不显示而是存储到硬盘中.过了一会我收到消息

I generate a lots of figures with a script which I do not display but store to harddrive. After a while I get the message

/usr/lib/pymodules/python2.7/matplotlib/pyplot.py:412: RuntimeWarning: 打开了20多个图.通过 pyplot 接口 (matplotlib.pyplot.figure) 创建的图会保留直到明确关闭,并且可能会消耗太多内存.(要控制此警告,请参阅rcParam figure.max_num_figures ).max_open_warning,RuntimeWarning)

/usr/lib/pymodules/python2.7/matplotlib/pyplot.py:412: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (matplotlib.pyplot.figure) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam figure.max_num_figures). max_open_warning, RuntimeWarning)

因此,我尝试在存储后关闭或清除数字.到目前为止,我尝试了以下所有方法,但没有一个有效.我仍然收到来自上面的消息.

Thus, I tried to close or clear the figures after storing. So far, I tried all of the followings but no one works. I still get the message from above.

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

此外,我尝试通过以下方式限制未结数字的数量

And furthermore I tried to restrict the number of open figures by

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

以下是一段示例代码,其行为类似于上述内容.我在我尝试过的地方添加了我尝试过的不同选项作为评论.

Here follows a piece of sample code that behaves like described above. I added the different options I tried as comments at the places I tried them.

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 时得到的错误:

To be complete, that is the error I get when using plt.close:

无法调用事件"命令:应用程序已被破坏在执行事件生成 $w <>"(程序ttk::ThemeChanged"第 6 行)从ttk::ThemeChanged"内调用

can't invoke "event" command: application has been destroyed while executing "event generate $w <>" (procedure "ttk::ThemeChanged" line 6) invoked from within "ttk::ThemeChanged"

推荐答案

关闭图形的正确方法是使用 plt.close(fig),如下面的编辑所示最初发布的代码.

The correct way to close your figures would be to use plt.close(fig), as can be seen in the below edit of the code you originally posted.

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

The error that you describe at the end of your question suggests to me that your problem is not with matplotlib, but rather with another part of your code (such as ttk).

这篇关于Python plt:关闭或清除图不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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