最佳答案
如何删除 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.
那么有没有办法从轴上删除一行然后恢复内存呢? 这个潜在的解决方案 也不起作用。