如何从内存中清除matplotlib图形

2024-04-23 11:17:36 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我在前面运行代码时收到了以下警告:

Warning (from warnings module):
  File "<string>", line 558
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_open_warning`).

Warning (from warnings module):
  File "<string>", line 665
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_open_warning`).

我不想撒谎,我当时很粗心,不管怎样,我还是在运行它,无视警告。 现在,每当我尝试运行脚本时,我的python shell都不会运行脚本

所以我试图从内存中删除这些图,以便再次运行脚本

我已尝试了以下操作,但仍不允许我运行代码:

  1. plt.clf()
  2. plt.cla()
  3. f = plt.figure(){}{}
  4. plt.close('all')
  5. matplotlib.pyplot.figure().clear()matplotlib.pyplot.close()

如何解决此问题


Tags: the代码from脚本警告stringmatplotlibplt
1条回答
网友
1楼 · 发布于 2024-04-23 11:17:36
import matplotlib._pylab_helpers as pylhelp

pylhelp.Gcf().destroy_all()

尽管这可以满足您的需要,但这不是一个受支持的接口,可能会过时。我不知道还有其他方法可以做到这一点,这是我推荐它的唯一原因

相关问题 更多 >