Delete Line & legend from graph using matplotlib & wxPython

Hello,
I wrote a little program to display the spectral data of varoius filters. Here is a part of the code:

def openex(self, event):
dlg = wx.FileDialog(self, “Choose a Excitation Filter”, os.getcwd(), “”, “.ex”, wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
pathex = dlg.GetPath()
mypathex = os.path.basename(pathex)
self.SetStatusText(“Selected EX: %s” % mypathex)
ex = load(pathex)
ex = normspec_filter(ex)
self.gex = self.axes.plot(ex[:,0],ex[:,1],‘b’, lw=2,label = mypathex)
self.axes.axis([xmin,xmax,ymin,ymax])
self.axes.legend(loc=4)
self.plotPanel.draw()
dlg.Destroy()

def delex(self, event):
   
    **self.axes.lines.remove(self.gex[-1])**
    self.plotPanel.draw()

So I already figure out how to delete the last drawn line, but this is not a very good solution. What I actually would need, is a selection which line & legend the users whats to remove from the graph (perfect would be interactivly directly from the graph). But so far, I could not figure this out. Has anyone a good ides how to achieve this?

Cheers,

Sebi

···


Dr. Sebastian Rhode
Grünwalder Str. 103a
81547 München
Tel: +49 89 4703091
sebrhode@…982…

You may try to get the line to remove with
  self.gex, = self.axes.plot(ex[:,0],ex[:,1],'b', lw=2,label = mypathex)
in openex (note the comma)
To remove the line, put
  self.gex.remove()
in delex function.
An alternative is
  self.axes.get_lines()[-1].remove()
so that you don't need self.gex for this.

Now, you need a function to interactively select the line. I don't have
a solution for that, but I'm interested too!

···

Le mercredi 02 septembre 2009 à 17:41 +0200, Sebastian Rhode a écrit :

So I already figure out how to delete the last drawn line, but this is
not a very good solution. What I actually would need, is a selection
which line & legend the users whats to remove from the graph (perfect
would be interactivly directly from the graph). But so far, I could
not figure this out. Has anyone a good ides how to achieve this?

--
Fabrice Silva
Laboratory of Mechanics and Acoustics - CNRS
31 chemin Joseph Aiguier, 13402 Marseille, France.

All the matplotlib plot commands return artists created by that plot
command. Unless you want those permanently removed from the current
plot, it is suffice to just make them invisible
(http://matplotlib.sourceforge.net/api/artist_api.html?highlight=set_visible#matplotlib.artist.Artist.set_visible).

For interactive stuff, the following example may be a good starting
point (well, the example is in the svn trunk and I'm not sure if it
will run with older version of matplotlib).

http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/matplotlib/examples/event_handling/legend_picking.py?revision=7351&view=markup

Regards,

-JJ

···

On Wed, Sep 2, 2009 at 11:41 AM, Sebastian Rhode<sebrhode@...982...> wrote:

So I already figure out how to delete the last drawn line, but this is not a
very good solution. What I actually would need, is a selection which line &
legend the users whats to remove from the graph (perfect would be
interactivly directly from the graph). But so far, I could not figure this
out. Has anyone a good ides how to achieve this?