Removing picked line

Hi again,

when right-clicking on one or more lines in a plot, I want to present
the user a context menu where he can select to remove these lines.

Preferrably I want to do this not with a 'pick_event' connection but
with the (Qt) backend methods since the context menu should contain more
options (which shall be shown also when right-clicking on the diagram
background).

Unfortunately, I have no idea, how to do this. At first, I found only
the pick_event() which responds to a left mouse click. Is there a way to
get the Line2D instances that would be picked by specifying the mouse
position?

At second, if I have a Line2D instance: how can I remove it from the
axes?

Best regards

Ole

The pick_event is fired off with any mouse press. See the following
example: you can select the line with the left or right mouse button::

     import numpy as np
    import matplotlib.pyplot as plt

    fig = plt.figure()
    ax = fig.add_subplot(111)
    x = np.arange(10)
    line1, = ax.plot(x, x, picker=5, label='x')
    line2, = ax.plot(x, x*2, picker=5, label='x^2')

    def on_pick(pickevent):
        thisline = pickevent.artist
        print 'you picked line=%s with button=%s'%(
            thisline.get_label(), pickevent.mouseevent.button)

    fig.canvas.mpl_connect('pick_event', on_pick)

    plt.show()

If for some reason the built in pick_event is unsuitable, you can
create your own matplotlib.backend_bases.MouseEvent and call
line.contains(event) for each line you want to hit test.

http://matplotlib.sourceforge.net/api/artist_api.html#matplotlib.lines.Line2D.contains

http://matplotlib.sourceforge.net/api/backend_bases_api.html#matplotlib.backend_bases.MouseEvent

JDH

ยทยทยท

On Wed, Jun 10, 2009 at 6:55 AM, Ole Streicher<ole-usenet-spam@...361...> wrote:

Hi again,

when right-clicking on one or more lines in a plot, I want to present
the user a context menu where he can select to remove these lines.

Preferrably I want to do this not with a 'pick_event' connection but
with the (Qt) backend methods since the context menu should contain more
options (which shall be shown also when right-clicking on the diagram
background).

Unfortunately, I have no idea, how to do this. At first, I found only
the pick_event() which responds to a left mouse click. Is there a way to
get the Line2D instances that would be picked by specifying the mouse
position?