2011-02-13 609 views
54

如何删除matplotlib轴的一行(或多行),方法如下:实际上它会收集垃圾并释放内存?下面的代码似乎删除行,但从来没有释放内存(甚至显式调用GC.Collect的())如何删除Matplotlib图中的线

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也不起作用。

回答

40

我正在展示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) 

我检查了您的大型数据集,并在系统监视器上也确认了内存的释放。

。当然,更简单的方法(如果不是故障排除)会从列表中弹出并调用行对象remove,而无需创建一个硬参考吧:

lines.pop(0).remove() 
+0

我运行了你的代码,得到了: [8:37 pm] @flattop:〜/ Desktop/sandbox> python delete_lines.py 我在ubuntu 10.04中使用matplotlib版本0.99.1.1 – 2011-02-13 02:31:28

+1

@David Morton我刚降级到0.99.1,现在我重现了你的问题。我想我只能推荐升级到1.0.1。自0.99.x以来有很多错误修正。 – Paul 2011-02-13 03:31:03

+0

谢谢!非常感谢你的帮助。 – 2011-02-13 05:07:56

2

(使用相同的作为上面的家伙的例子)

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() 
9

我已经在不同论坛尝试了很多不同的答案。我想这取决于你正在开发的机器。但我已经使用的声明

ax.lines = [] 

和完美的作品。我不使用cla()导致它删除我对剧情所做的所有定义。

Ex。

pylab.setp(_self.ax.get_yticklabels(), fontsize=8) 

但我试过多次删除这些行。当我正在删除时,还使用weakref库检查对该行的引用,但没有任何工作适合我。

希望这个作品给别人= d

46

这是我输入了我的一个同事很长的解释。我认为这也会有所帮助。不过,请耐心等待。我接触到最后的真正问题。就像一个传情,这是一个问题,有额外的参考你的Line2D对象。

警告:另一个需要注意的地方是,如果你使用IPython来测试这个,IPython会保留它自己的引用,而不是所有的都是weakrefs。因此,在IPython中测试垃圾收集不起作用。它只是混淆了事情。

好的,我们走吧。每个matplotlib对象(FigureAxes等)通过各种属性提供对其子艺术家的访问。下面的例子变得相当长,但应该照亮。

我们首先创建一个Figure对象,然后将一个Axes对象添加到该图中。请注意,axfig.axes[0]是相同的对象(相同id())。

>>> #Create a figure 
>>> fig = plt.figure() 
>>> fig.axes 
[] 

>>> #Add an axes object 
>>> ax = fig.add_subplot(1,1,1) 

>>> #The object in ax is the same as the object in fig.axes[0], which is 
>>> # a list of axes objects attached to fig 
>>> print ax 
Axes(0.125,0.1;0.775x0.8) 
>>> print fig.axes[0] 
Axes(0.125,0.1;0.775x0.8) #Same as "print ax" 
>>> id(ax), id(fig.axes[0]) 
(212603664, 212603664) #Same ids => same objects 

这也延伸到在轴线对象:

>>> #Add a line to ax 
>>> lines = ax.plot(np.arange(1000)) 

>>> #Lines and ax.lines contain the same line2D instances 
>>> print lines 
[<matplotlib.lines.Line2D object at 0xce84bd0>] 
>>> print ax.lines 
[<matplotlib.lines.Line2D object at 0xce84bd0>] 

>>> print lines[0] 
Line2D(_line0) 
>>> print ax.lines[0] 
Line2D(_line0) 

>>> #Same ID => same object 
>>> id(lines[0]), id(ax.lines[0]) 
(216550352, 216550352) 

如果你使用的是什么上面做打电话plt.show(),你会看到一个包含组轴和一个线图:

A figure containing a set of axes and a single line

现在,虽然我们已经看到的linesax.lines内容是

>>> id(lines), id(ax.lines) 
(212754584, 211335288) 

作为结果,从移除元素:同样,必须指出,由lines变量引用的对象是不一样的由ax.lines尊崇的对象作为可以由以下可以看出是很重要lines对当前绘图没有任何影响,但从ax.lines中删除元素会从当前绘图中删除该线条。所以:

>>> #THIS DOES NOTHING: 
>>> lines.pop(0) 

>>> #THIS REMOVES THE FIRST LINE: 
>>> ax.lines.pop(0) 

所以,如果你运行代码的第二行,你会要从当前情节ax.lines[0]包含Line2D对象,它会消失。请注意,这也可以通过ax.lines.remove()意义来做到这一点,你可以在一个变量保存Line2D实例,然后将它传递给ax.lines.remove()删除线,就像这样:

>>> #Create a new line 
>>> lines.append(ax.plot(np.arange(1000)/2.0)) 
>>> ax.lines 
[<matplotlib.lines.Line2D object at 0xce84bd0>, <matplotlib.lines.Line2D object at 0xce84dx3>] 

A figure containing a set of axes and two lines

>>> #Remove that new line 
>>> ax.lines.remove(lines[0]) 
>>> ax.lines 
[<matplotlib.lines.Line2D object at 0xce84dx3>] 

A figure containing a set of axes and only the second line

以上所有作品都适用于fig.axes以及它适用于ax.lines

现在,真正的问题在这里。如果我们存储包含在ax.lines[0]weakref.ref对象的引用,然后尝试删除它,我们会发现,收集它不会垃圾:

>>> #Create weak reference to Line2D object 
>>> from weakref import ref 
>>> wr = ref(ax.lines[0]) 
>>> print wr 
<weakref at 0xb758af8; to 'Line2D' at 0xb757fd0> 
>>> print wr() 
<matplotlib.lines.Line2D at 0xb757fd0> 

>>> #Delete the line from the axes 
>>> ax.lines.remove(wr()) 
>>> ax.lines 
[] 

>>> #Test weakref again 
>>> print wr 
<weakref at 0xb758af8; to 'Line2D' at 0xb757fd0> 
>>> print wr() 
<matplotlib.lines.Line2D at 0xb757fd0> 

基准仍然活着!为什么?这是因为wr中的参考指向Line2D对象还有其他参考。请记住linesax.lines没有相同的ID,但包含相同的元素?那是这个问题。

>>> #Print out lines 
>>> print lines 
[<matplotlib.lines.Line2D object at 0xce84bd0>, <matplotlib.lines.Line2D object at 0xce84dx3>] 

To fix this problem, we simply need to delete `lines`, empty it, or let it go out of scope. 

>>> #Reinitialize lines to empty list 
>>> lines = [] 
>>> print lines 
[] 
>>> print wr 
<weakref at 0xb758af8; dead> 

因此,故事的寓意是,自己清理。如果你期望有东西被垃圾收集,但事实并非如此,那么你可能会在某处留下一个参考。