patch: make legend and its components pickable

Hi, here a message I sent a while ago, but I hanen't seen it on the
ML, so here it is again, in case.

David Douard

pickage_legend.patch (1.19 KB)



Here is a simple patch that allow to make legend and its compnents
(texts, patches and lines) pickable for real.

The goal was to make this work (may be added to examples in mpl):

from pylab import *


props = dict( alpha=0.5, faceted=False )


handles = []
colours = ['red', 'green', 'blue', 'magenta', 'cyan', 'yellow']

for colour in colours:
    x, y = rand(2,N)
    s = 400.0 * rand(N)
    handles.append(scatter(x, y, c=colour, s=s, picker=True, **props))

l = legend(handles, colours, )


for lb in l.get_texts():

def pick(event):
    a = event.artist
    if isinstance(a, matplotlib.text.Text):
        name = event.artist.get_text()
        h = handles[colours.index(name)]
        h.set_visible(not h.get_visible())

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

PS: note that I am not on the mpl-devel mailing-list. Please Cc me
back in case of reply.

David Douard LOGILAB, Paris (France), +33 1 45 32 03 12
Formations Python, Zope, Debian :
Développement logiciel sur mesure :
Informatique scientifique :