有时候需要迭代批量地使用matplotlib
生成图片,这个过程中如果不进行处理会导致下面的警告
RuntimeWarning: More than 20 figures have been opened. Figures created thro
ugh 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`).
只需每次在plt.savefig
后调用函数(closePlots
)即可
import time
import matplotlib.pyplot as plt
def closePlots():
plt.clf()
plt.cla()
plt.close("all")
time.sleep(0.5)
while True:
...
plt.savefig(path) # 保存图片
closePlots() # 清理空间