【问题标题】:plt.close('all') doesn't get rid of 'too many figures' warningplt.close('all') 没有摆脱“太多数字”警告
【发布时间】:2020-07-17 04:29:01
【问题描述】:

我有一个重复运行的脚本,在此过程中它会将图形保存到文件夹中。过了一会儿,我开始收到有关内存中打开的数字过多的警告。

我检查了有关该主题的其他问题,例如,this one 并将plt.close('all') 添加到我的代码中,所以现在它看起来像这样:

fig, ax = plt.subplots(figsize=(17,8))
plt.hist(results_df['Diff'], bins=100, density=True, histtype='step')
plt.savefig(f'backtester_results/figures/rf_model_{n_days}_data_and_{lag}_lag.png',
                format='png')
plt.close('all')

但一段时间后,我不断在记忆中堆积数字和警告。我哪里做错了?

这是警告:

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.

【问题讨论】:

  • 请包括您收到的警告。

标签: python matplotlib


【解决方案1】:

在阅读官方文档时,我会假设plt.close('all') 只关闭窗口而不删除数字(https://matplotlib.org/1.3.0/api/pyplot_api.html#matplotlib.pyplot.close)。

据我了解,您需要按如下方式清除该数字:

fig.clf()
plt.close()

来源:(How can I release memory after creating matplotlib figures)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    相关资源
    最近更新 更多