如何“清洁石板"? [英] How to "clean the slate"?

查看:56
本文介绍了如何“清洁石板"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要绘制一系列绘图,并将每个绘图保存到文件中.但我不知道该如何清除先前的地块.也许我每次都需要创建一些新对象,但我不知道是哪个对象.这是我的代码,请注意注释.这是我的代码:

I want to make a series of plots, and save each to a file. But I don't know how to wipe previous plots off. Maybe I need to create some new object for each time, but I don't which object that would be. Here is my code, notice the comment. This is my code:

import matplotlib.pyplot as plt
ind = (1,2,3,4)
groups=(
  (1, (1.1,1.2,1.3,1.4)),
  (2, (2.2,2.2,1.2,2.4)),
)

for group in reversed(groups):
  #clean the slate ?
  plt.bar(ind   ,group[1])
  plt.xticks([i+0.5 for i in ind],ind)
  plt.savefig('%d.png' % group[0])

推荐答案

DO NOT 每次使用 plt.figure() 创建一个新图形,你会绕很快就会耗尽内存.而是使用(分别用于图形和轴):

DO NOT create a new figure each time with plt.figure(), you'll wind up running out of memory rather quickly. Instead use (for the figure and the axes respectively):

plt.clf()
plt.cla()

您可以运行 plt.close()释放分配,但是过去有一些讨论认为此方法导致了内存泄漏.快速测试表明,在 1.1.1rc 版本中,此方法没有问题,请随时使用它作为替代.一个有用的相关问题讨论了方法之间的差异.

You can run plt.close() to free up the allocation, however there has been some discussion that this method has lead to memory leaks in the past. A quick test shows that in version 1.1.1rc this works without problems, so feel free to use it as an alternative. A useful related question discuses the differences between the methods.

这篇关于如何“清洁石板"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆