How to remove lines in a Matplotlib plot
Asked Answered
C

6

103

How can I remove a line (or lines) of a matplotlib axes in such a way as it actually gets garbage collected and releases the memory back? The below code appears to delete the line, but never releases the memory (even with explicit calls to 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.

So is there a way to just delete one line from an axes and get the memory back? This potential solution also does not work.

Castigate answered 13/2, 2011 at 1:19 Comment(0)
R
78

I'm showing that a combination of lines.pop(0) l.remove() and del l does the trick.

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)

I checked your large dataset and the release of the memory is confirmed on the system monitor as well.

Of course the simpler way (when not trouble-shooting) would be to pop it from the list and call remove on the line object without creating a hard reference to it:

lines.pop(0).remove()
Rippy answered 13/2, 2011 at 1:47 Comment(4)
I ran your code and I got: [8:37pm]@flattop:~/Desktop/sandbox>python delete_lines.py <weakref at 0x8dd348c; to 'Line2D' at 0x8dd43ec> <weakref at 0x8dd348c; to 'Line2D' at 0x8dd43ec> <weakref at 0x8dd348c; to 'Line2D' at 0x8dd43ec> I'm using matplotlib version 0.99.1.1 in ubuntu 10.04Castigate
@David Morton I just downgraded to 0.99.1 and now I reproduce your problem. I guess I can only recommend upgrading to 1.0.1. There were a lot of bugfixes since 0.99.xRippy
The issue here is likely an issue of references hanging around when they shouldn't be. I would bet that the OP was using IPython to test things out. See my answer.Roughhouse
I think the behavior has been updated. In Matplotlib 3.5.3, the line gets removed after either poping it from ax.lines or finding it and calling l.remove(). Attempting to call remove() on the line after poping raises ValueError: list.remove(x): x not in listAelber
R
95

This is a very long explanation that I typed up for a coworker of mine. I think it would be helpful here as well. Be patient, though. I get to the real issue that you are having toward the end. Just as a teaser, it's an issue of having extra references to your Line2D objects hanging around.

WARNING: One other note before we dive in. If you are using IPython to test this out, IPython keeps references of its own and not all of them are weakrefs. So, testing garbage collection in IPython does not work. It just confuses matters.

Okay, here we go. Each matplotlib object (Figure, Axes, etc) provides access to its child artists via various attributes. The following example is getting quite long, but should be illuminating.

We start out by creating a Figure object, then add an Axes object to that figure. Note that ax and fig.axes[0] are the same object (same 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

This also extends to lines in an axes object:

>>> #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)

If you were to call plt.show() using what was done above, you would see a figure containing a set of axes and a single line:

A figure containing a set of axes and a single line

Now, while we have seen that the contents of lines and ax.lines is the same, it is very important to note that the object referenced by the lines variable is not the same as the object reverenced by ax.lines as can be seen by the following:

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

As a consequence, removing an element from lines does nothing to the current plot, but removing an element from ax.lines removes that line from the current plot. So:

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

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

So, if you were to run the second line of code, you would remove the Line2D object contained in ax.lines[0] from the current plot and it would be gone. Note that this can also be done via ax.lines.remove() meaning that you can save a Line2D instance in a variable, then pass it to ax.lines.remove() to delete that line, like so:

>>> #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

All of the above works for fig.axes just as well as it works for ax.lines

Now, the real problem here. If we store the reference contained in ax.lines[0] into a weakref.ref object, then attempt to delete it, we will notice that it doesn't get garbage collected:

>>> #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>

The reference is still live! Why? This is because there is still another reference to the Line2D object that the reference in wr points to. Remember how lines didn't have the same ID as ax.lines but contained the same elements? Well, that's the problem.

>>> #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>

So, the moral of the story is, clean up after yourself. If you expect something to be garbage collected but it isn't, you are likely leaving a reference hanging out somewhere.

Roughhouse answered 27/11, 2012 at 0:24 Comment(4)
Exactly what I needed. I'm plotting thousands of maps, each with a scatter plot over top of a world map projection. They were taking 3 seconds each! By reusing the figure with the map already drawn and popping the resulting collection from ax.collections I got it down to 1/3 of a second. Thanks!Evonneevonymus
I think this is no longer necessary in current versions of mpl. The artist have a remove() function which will clean them out of the mpl side of things, and then you only need to keep track of your references.Violate
Huh, any idea what version of matplotlib this change same out in?Roughhouse
Found this to be useful when using a bunch of plots in a matplotlib animation. Otherwise, you end up with a very large amount of memory in use. Now to make this thing faster.Digest
R
78

I'm showing that a combination of lines.pop(0) l.remove() and del l does the trick.

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)

I checked your large dataset and the release of the memory is confirmed on the system monitor as well.

Of course the simpler way (when not trouble-shooting) would be to pop it from the list and call remove on the line object without creating a hard reference to it:

lines.pop(0).remove()
Rippy answered 13/2, 2011 at 1:47 Comment(4)
I ran your code and I got: [8:37pm]@flattop:~/Desktop/sandbox>python delete_lines.py <weakref at 0x8dd348c; to 'Line2D' at 0x8dd43ec> <weakref at 0x8dd348c; to 'Line2D' at 0x8dd43ec> <weakref at 0x8dd348c; to 'Line2D' at 0x8dd43ec> I'm using matplotlib version 0.99.1.1 in ubuntu 10.04Castigate
@David Morton I just downgraded to 0.99.1 and now I reproduce your problem. I guess I can only recommend upgrading to 1.0.1. There were a lot of bugfixes since 0.99.xRippy
The issue here is likely an issue of references hanging around when they shouldn't be. I would bet that the OP was using IPython to test things out. See my answer.Roughhouse
I think the behavior has been updated. In Matplotlib 3.5.3, the line gets removed after either poping it from ax.lines or finding it and calling l.remove(). Attempting to call remove() on the line after poping raises ValueError: list.remove(x): x not in listAelber
C
18

I've tried lots of different answers in different forums. I guess it depends on the machine your developing. But I haved used the statement

ax.lines = []

and works perfectly. I don't use cla() cause it deletes all the definitions I've made to the plot

Ex.

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

but I've tried deleting the lines many times. Also using the weakref library to check the reference to that line while I was deleting but nothing worked for me.

Hope this works for someone else =D

Charkha answered 27/3, 2012 at 19:29 Comment(2)
The issue here is likely an issue of references hanging around when they shouldn't be. I would bet that the OP was using IPython to test things out. See my answer.Roughhouse
it is not possible to set an attribute in matplotlib 3.5.3Aelber
T
10

Hopefully this can help others: The above examples use ax.lines. With more recent mpl (3.3.1), there is ax.get_lines(). This bypasses the need for calling ax.lines=[]

for line in ax.get_lines(): # ax.lines:
    line.remove()
# ax.lines=[] # needed to complete removal when using ax.lines
Tolbutamide answered 23/8, 2020 at 15:21 Comment(1)
This should be up further in the list because the other answers are really out of date and confusing.Puccoon
A
6

(using the same example as the guy above)

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()
Absent answered 4/11, 2011 at 14:5 Comment(0)
S
2

while you can remove any line with specific index in an axes as following:

import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 1)

axes = fig.axes  # = [ax1, ax2]

# add two lines to ax1
ax1.plot([0, 1, 2, 3, 4], [10, 1, 20, 3, 40], lw=2, color='k', label='2 Hz')
ax1.plot([0, 1, 2, 3, 4], [15, 6, 25, 8, 45], lw=2, color='r', label='4 Hz')

# remove one line from ax1
ax1.lines[0].remove()  # remove the first line in ax1
# axes[0].lines[0].remove()  # equivalent to the line above

Sugihara answered 28/10, 2021 at 8:1 Comment(1)
for the fifth line you can call the method fig.get_axes() alternatively.Sugihara

© 2022 - 2024 — McMap. All rights reserved.