这是我输入了我的一个同事很长的解释。我认为这也会有所帮助。不过,请耐心等待。我接触到最后的真正问题。就像一个传情,这是一个问题,有额外的参考你的Line2D
对象。
警告:另一个需要注意的地方是,如果你使用IPython来测试这个,IPython会保留它自己的引用,而不是所有的都是weakrefs。因此,在IPython中测试垃圾收集不起作用。它只是混淆了事情。
好的,我们走吧。每个matplotlib
对象(Figure
,Axes
等)通过各种属性提供对其子艺术家的访问。下面的例子变得相当长,但应该照亮。
我们首先创建一个Figure
对象,然后将一个Axes
对象添加到该图中。请注意,ax
和fig.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()
,你会看到一个包含组轴和一个线图:
现在,虽然我们已经看到的lines
和ax.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>]
>>> #Remove that new line
>>> ax.lines.remove(lines[0])
>>> ax.lines
[<matplotlib.lines.Line2D object at 0xce84dx3>]
以上所有作品都适用于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
对象还有其他参考。请记住lines
与ax.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>
因此,故事的寓意是,自己清理。如果你期望有东西被垃圾收集,但事实并非如此,那么你可能会在某处留下一个参考。
我运行了你的代码,得到了: [8:37 pm] @flattop:〜/ Desktop/sandbox> python delete_lines.py 我在ubuntu 10.04中使用matplotlib版本0.99.1.1 –
2011-02-13 02:31:28
@David Morton我刚降级到0.99.1,现在我重现了你的问题。我想我只能推荐升级到1.0.1。自0.99.x以来有很多错误修正。 – Paul 2011-02-13 03:31:03
谢谢!非常感谢你的帮助。 – 2011-02-13 05:07:56