From: <jd...@us...> - 2009-08-04 16:48:17
|
Revision: 7347 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7347&view=rev Author: jdh2358 Date: 2009-08-04 16:48:04 +0000 (Tue, 04 Aug 2009) Log Message: ----------- enabled legend picking Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/legend.py Added Paths: ----------- trunk/matplotlib/examples/event_handling/legend_picking.py Added: trunk/matplotlib/examples/event_handling/legend_picking.py =================================================================== --- trunk/matplotlib/examples/event_handling/legend_picking.py (rev 0) +++ trunk/matplotlib/examples/event_handling/legend_picking.py 2009-08-04 16:48:04 UTC (rev 7347) @@ -0,0 +1,36 @@ +""" +Enable picking on the legend to toggle the legended line on and off +""" +import numpy as np +import matplotlib.pyplot as plt + +t = np.arange(0.0, 0.2, 0.1) +y1 = 2*np.sin(2*np.pi*t) +y2 = 4*np.sin(2*np.pi*2*t) + +fig = plt.figure() +ax = fig.add_subplot(111) + +line1, = ax.plot(t, y1, lw=2, color='red', label='1 hz') +line2, = ax.plot(t, y2, lw=2, color='blue', label='2 hz') + +leg = ax.legend(loc='upper left', fancybox=True, shadow=True) +leg.get_frame().set_alpha(0.4) + + +lines = [line1, line2] +lined = dict() +for legline, realine in zip(leg.get_lines(), lines): + legline.set_picker(5) # 5 pts tolerance + lined[legline] = realine + +def onpick(event): + legline = event.artist + realline = lined[legline] + vis = realline.get_visible() + realline.set_visible(not vis) + fig.canvas.draw() + +fig.canvas.mpl_connect('pick_event', onpick) + +plt.show() Modified: trunk/matplotlib/lib/matplotlib/legend.py =================================================================== --- trunk/matplotlib/lib/matplotlib/legend.py 2009-08-04 13:20:29 UTC (rev 7346) +++ trunk/matplotlib/lib/matplotlib/legend.py 2009-08-04 16:48:04 UTC (rev 7347) @@ -177,6 +177,10 @@ propnames=['numpoints', 'markerscale', 'shadow', "columnspacing", "scatterpoints"] + self.texts = [] + self.legendHandles = [] + self._legend_title_box = None + localdict = locals() for name in propnames: @@ -240,6 +244,7 @@ if isinstance(parent,Axes): self.isaxes = True + self.set_axes(parent) self.set_figure(parent.figure) elif isinstance(parent,Figure): self.isaxes = False @@ -313,10 +318,8 @@ set the boilerplate props for artists added to axes """ a.set_figure(self.figure) - - for c in self.get_children(): - c.set_figure(self.figure) - + if self.isaxes: + a.set_axes(self.axes) a.set_transform(self.get_transform()) @@ -432,6 +435,7 @@ textbox = TextArea(l, textprops=label_prop, multilinebaseline=True, minimumdescent=True) text_list.append(textbox._text) + labelboxes.append(textbox) handleboxes = [] @@ -688,6 +692,13 @@ children = [] if self._legend_box: children.append(self._legend_box) + children.extend(self.get_lines()) + children.extend(self.get_patches()) + children.extend(self.get_texts()) + children.append(self.get_frame()) + + if self._legend_title_box: + children.append(self.get_title()) return children def get_frame(self): This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |