简体   繁体   English

plt.close('all') 没有摆脱“太多数字”警告

[英]plt.close('all') doesn't get rid of 'too many figures' warning

I have a script that runs repeatedly and in the process it saves a figure into a folder.我有一个重复运行的脚本,在此过程中它将一个图形保存到一个文件夹中。 After a while I start getting warnings about too many open figures in memory.过了一会儿,我开始收到有关 memory 中打开数字过多的警告。

I checked other questions on the topic, for example, this one and added plt.close('all') to my code so now it looks like this:我检查了有关该主题的其他问题,例如, 这个问题并将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')

And yet I keep getting figures piled up in memory and warnings after a while.然而,我在 memory 和一段时间后不断收到警告。 Where did I go wrong?我在哪里 go 错了?

Here's the warning:这是警告:

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.

When reading in the official documentation I would assume that plt.close('all') only closes the windows without removing the figures ( https://matplotlib.org/1.3.0/api/pyplot_api.html#matplotlib.pyplot.close ).在阅读官方文档时,我会假设plt.close('all')仅关闭 windows 而不删除数字( https://matplotlib.org/1.3.0/api/pyplot_api.html#matplotlib.pyplot.close )。

As I understand you would need to clear the figure as follows:据我了解,您需要按如下方式清除该图:

fig.clf()
plt.close()

Source: ( How can I release memory after creating matplotlib figures )来源:( 在创建 matplotlib 数字后,我如何发布 memory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM