remove artist from axes?

Hi,

I don't see an obvious way to remove a line from an axes object.

Shall I add remove_child(h) which searches through all the lists
of containers and removes the one that I don't want?

For now I will rerender the graph without the missing child.

  - Paul

I think that would be very useful. TVTK has something similar, and I use
it often. Something to remove the last object added we be great too (I
don't know where the reference to the last object added should be stored,
but there are many places, and perhaps maybe simply as an attribute of
the "remove_last" function. And I suggest that this should not be a
method of axes or a figure, but a pylab function.

Cheers,

Ga�l

···

On Sun, Jul 15, 2007 at 10:49:13PM -0400, Paul Kienzle wrote:

I don't see an obvious way to remove a line from an axes object.

Shall I add remove_child(h) which searches through all the lists
of containers and removes the one that I don't want?

That's one way to do it, but you might consider something along the
lines -- every time someone adds an Artist anywhere in the figure,
they add an entry into a Figure remove dictionary that maps the artist
to a function to remove it

class Figure:
  def register_remove(self, artist, func):
      'register a function to remove an artist'
      self.removemap[artist] = func
      self.lastArtist = artist # this can be used to handle Gael's request

  def remove_artist(self, artist):
    'remove the artist'
    func = self.removemap.get(artist, None)
    if func is None: return
    func() # poof, it's gone
    del self.removemap(artist)

  def remove_last(self):
    'remove the most recently registered artist'
    self.remove_artist(self.lastArtist)
    self.lastArtist = None
class Axes:
  def add_line(self, line):
      self.figure.register_remove(line, lambda x: self.lines.remove(line))
      self.lines.append(line)

Then the user won't need to know whether the artist is stored by the
Axes, Axis, XTick, etc... This is likely more efficient and easier to
implement than recursively searching....

Users can then:

line, = ax.plot(something)
fig.remove_artist(line)

or

ax.fill(something)
fig.remove_last()

and a pylab interface will be trival

def remove_last():
    gcf().remove_last()

JDH

···

On 7/15/07, Paul Kienzle <pkienzle@...537...> wrote:

Hi,

I don't see an obvious way to remove a line from an axes object.

Shall I add remove_child(h) which searches through all the lists
of containers and removes the one that I don't want?