From 9eb10efe7c37bd91fe56ce5bdc25681965eb7e57 Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Sun, 3 Apr 2011 14:12:59 -1000 Subject: [PATCH] Bugfix: enable picking of AsteriskCollection instances A primary use of these is in scatter, where they provide + and x symbols. --- CHANGELOG | 3 +++ lib/matplotlib/collections.py | 38 +++++++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index b1c9694d7e25..4b9ab7affdd7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +2011-04-03 Fixed broken pick interface to AsteriskCollection objects + used by scatter. - EF + 2011-03-29 Wrapped ViewVCCachedServer definition in a factory function. This class now inherits from urllib2.HTTPSHandler in order to fetch data from github, but HTTPSHandler is not defined diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index 6a93ccbc27d9..edd84a185826 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -90,6 +90,7 @@ def __init__(self, self.set_linewidth(linewidths) self.set_linestyle(linestyles) self.set_antialiased(antialiaseds) + self.set_pickradius(pickradius) self.set_urls(urls) @@ -105,7 +106,6 @@ def __init__(self, else: self._uniform_offsets = offsets - self._pickradius = pickradius self.update(kwargs) self._paths = None @@ -221,6 +221,12 @@ def draw(self, renderer): gc.restore() renderer.close_group(self.__class__.__name__) + def set_pickradius(self, pr): + self._pickradius = pr + + def get_pickradius(self): + return self._pickradius + def contains(self, mouseevent): """ Test whether the mouse event occurred in the collection. @@ -228,19 +234,35 @@ def contains(self, mouseevent): Returns True | False, ``dict(ind=itemlist)``, where every item in itemlist contains the event. """ - if callable(self._contains): return self._contains(self,mouseevent) - if not self.get_visible(): return False,{} + if callable(self._contains): + return self._contains(self,mouseevent) + + if not self.get_visible(): + return False, {} + + if self._picker is True: # the Boolean constant, not just nonzero or 1 + pickradius = self._pickradius + else: + try: + pickradius = float(self._picker) + except TypeError: + # This should not happen if "contains" is called via + # pick, the normal route; the check is here in case + # it is called through some unanticipated route. + warnings.warn( + "Collection picker %s could not be converted to float" + % self._picker) + pickradius = self._pickradius transform, transOffset, offsets, paths = self._prepare_points() ind = mpath.point_in_path_collection( - mouseevent.x, mouseevent.y, self._pickradius, + mouseevent.x, mouseevent.y, pickradius, transform.frozen(), paths, self.get_transforms(), - offsets, transOffset, len(self._facecolors)>0) - return len(ind)>0,dict(ind=ind) + offsets, transOffset, pickradius <= 0) + + return len(ind)>0, dict(ind=ind) - def set_pickradius(self,pickradius): self.pickradius = 5 - def get_pickradius(self): return self.pickradius def set_urls(self, urls): if urls is None: