如何删除matplotlib轴的一行(或多行),因为它实际上被垃圾回收并释放回内存?下面的代码似乎删除了该行,但从未释放内存(即使显式调用gc.collect()
)
from matplotlib import pyplot
import numpy
a = numpy.arange(int(1e7))
# large so you can easily see the memory footprint on the system monitor.
fig = pyplot.Figure()
ax = pyplot.add_subplot(1, 1, 1)
lines = ax.plot(a) # this uses up an additional 230 Mb of memory.
# can I get the memory back?
l = lines[0]
l.remove()
del l
del lines
# not releasing memory
ax.cla() # this does release the memory, but also wipes out all other lines.
那么,有没有一种方法可以从一个轴上删除一行,然后重新获得内存呢?This potential solution也不起作用。
发布于 2011-02-13 09:47:12
我要展示的是lines.pop(0)
l.remove()
和del l
的组合可以达到这个效果。
from matplotlib import pyplot
import numpy, weakref
a = numpy.arange(int(1e3))
fig = pyplot.Figure()
ax = fig.add_subplot(1, 1, 1)
lines = ax.plot(a)
l = lines.pop(0)
wl = weakref.ref(l) # create a weak reference to see if references still exist
# to this object
print wl # not dead
l.remove()
print wl # not dead
del l
print wl # dead (remove either of the steps above and this is still live)
我检查了你的大型数据集,内存的释放也在系统监视器上得到了确认。
当然,更简单的方法(当不是故障排除时)是从列表中弹出它,并在line对象上调用remove
,而不创建对它的硬引用:
lines.pop(0).remove()
发布于 2012-03-28 03:29:37
我在不同的论坛上尝试了很多不同的答案。我猜这取决于你开发的机器。但是我用了这个语句
ax.lines = []
而且工作得很完美。我不使用cla()
,因为它删除了我对绘图所做的所有定义
例如。
pylab.setp(_self.ax.get_yticklabels(), fontsize=8)
但我已经多次尝试删除这些行了。当我在删除的时候,我也使用弱引用库来检查对该行的引用,但是对我来说什么都不起作用。
希望这对其他人有效=D
发布于 2011-11-04 22:05:17
(使用与上面的人相同的示例)
from matplotlib import pyplot
import numpy
a = numpy.arange(int(1e3))
fig = pyplot.Figure()
ax = fig.add_subplot(1, 1, 1)
lines = ax.plot(a)
for i, line in enumerate(ax.lines):
ax.lines.pop(i)
line.remove()
https://stackoverflow.com/questions/4981815
复制相似问题