【问题标题】:How to clear matplotlib graphs from memory如何从内存中清除 matplotlib 图形
【发布时间】:2020-04-01 04:01:53
【问题描述】:

所以我之前运行代码并收到以下警告:

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()f.clear()plt.close(f)
  4. plt.close('all')
  5. matplotlib.pyplot.figure().clear() matplotlib.pyplot.close()

我该如何解决这个问题?

【问题讨论】:

标签: python pandas matplotlib


【解决方案1】:
import matplotlib._pylab_helpers as pylhelp

pylhelp.Gcf().destroy_all()

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

【讨论】:

  • 哦,等一下,对不起,这行得通。我在某处犯了一个错误
猜你喜欢
  • 2015-04-29
  • 1970-01-01
  • 2017-06-20
  • 2017-12-05
  • 2018-01-30
  • 2015-11-21
  • 2015-07-12
  • 2017-03-11
  • 2019-05-08
相关资源
最近更新 更多