>
> 1) WIthin this same pick handler function, have another if conditional,
> but for the case when the user is picking the legend. In other words, I
> want to pick the legend artist, not a Line2D artist.
I'm afraid but it is not clear what you want here (at least to me).
Can you just pick up the legend patch?
If what you want is to check if the picked-up line2d artist is one of
the legend handles, you may
explicitly do that (see the attached example code).
>
> 2) Modify the above so the user can pick only the actual points on a line,
> but not the connecting line if I have plotted it like 'o-' style (connected
> points).
You know the data points of the line and the position of the mouse
event, can you just check if the event is nearby the point?
You may do that before the "pick_event" is triggered, i.e., by using
the custom picker (contains) function. See the attached example. The
"contains_points" function is derived from the contain method of the
Line2D class.
Also check the examples under the event_handling directory.
-JJ
>
> I hope this is clear. Any help is appreciated. Thanks.
> Che
>
>
> ------------------------------------------------------------------------------
> Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
> -OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
> -Strategies to boost innovation and cut costs with open source participation
> -Receive a $600 discount off the registration fee with the source code: SFAD
> http://p.sf.net/sfu/XcvMzF8H
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
>
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from matplotlib.text import Text
import numpy as np
def contains_points(self, mouseevent):
pickradius = self.pickradius
# Make sure we have data to plot
if self._invalid:
self.recache()
if len(self._xy)==0: return False,{}
# Convert points to pixels
path, affine = self._transformed_path.get_transformed_points_and_affine()
path = affine.transform_path(path)
xy = path.vertices
xt = xy[:, 0]
yt = xy[:, 1]
pixels = self.figure.dpi/72. * self.pickradius
d = (xt-mouseevent.x)**2 + (yt-mouseevent.y)**2
ind, = np.nonzero(np.less_equal(d, pixels**2))
# Return the point(s) within radius
return len(ind)>0,dict(ind=ind)
if 1:
fig = plt.figure()
ax1 = fig.add_subplot(111)
line, = ax1.plot([1,2,3], 'o-', picker=contains_points)
l = ax1.legend(["Legend"])
# picking up the legend patch.
def legend_picker(self, event):
return self.legendPatch.contains(event)
l.set_picker(legend_picker)
# when you want to pickup legend handles.
for a in l.legendHandles+l.texts:
a.set_picker(True)
def onpick(event, legend=l):
if event.artist in legend.legendHandles:
print 'handle in legend', event.artist
elif event.artist in legend.texts:
print 'text in legend', event.artist
elif event.artist == legend:
print event.artist
elif isinstance(event.artist, Line2D):
thisline = event.artist
xdata = thisline.get_xdata()
ydata = thisline.get_ydata()
print 'onpick line:', xdata, ydata
fig.canvas.mpl_connect('pick_event', onpick)
fig.canvas.draw()
plt.show()
------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users