8000 Deprecate setting a Collection/Patch's pickradius via set_picker. · matplotlib/matplotlib@9766c90 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9766c90

Browse files
committed
Deprecate setting a Collection/Patch's pickradius via set_picker.
This is the same deprecation as the one introduced for Line2D in 3.3. (The exact deprecation strategy used here may already break convoluted call sequences on Collections: calling `coll.set_pickradius(3); coll.set_picker(5); coll.set_picker(False); coll.set_picker(True)` will leave the pickradius at 5 instead of 3 as was the case before, but I'm not too worried about that (if anything the new behavior is more sensical...).) On Patches, note that the old implementation of `_process_radius` was likely wrong: after calling `set_picker(True)`, `_picker` would be a bool and therefore a `Number`, so the fallback cases to the patch linewidth did not occur.
1 parent 7e59e29 commit 9766c90

File tree

5 files changed

+70
-31
lines changed

5 files changed

+70
-31
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Setting pickradius on Collections and Patches via ``set_picker``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Setting the pickradius (i.e. the tolerance for pick events and containment
4+
checks)of a `.Collection` or a `.Patch` via `.Artist.set_picker` is deprecated;
5+
use `.set_pickradius` instead.

lib/matplotlib/artist.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,10 +558,10 @@ def set_picker(self, picker):
558558
artist, return *hit=True* and props is a dictionary of
559559
properties you want added to the PickEvent attributes.
560560
561-
- *deprecated*: For `.Line2D` only, *picker* can also be a float
562-
that sets the tolerance for checking whether an event occurred
563-
"on" the line; this is deprecated. Use `.Line2D.set_pickradius`
564-
instead.
561+
- *deprecated*: For `.Line2D` and `.Collection`, *picker* can also
562+
be a float that sets the tolerance for checking whether an event
563+
occurred "on" the artist; this is deprecated. Use
564+
`.Line2D.set_pickradius`/`.Collection.set_pickradius` instead.
565565
"""
566566
self._picker = picker
567567

lib/matplotlib/collections.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,17 @@ def draw(self, renderer):
415415
renderer.close_group(self.__class__.__name__)
416416
self.stale = False
417417

418+
def set_picker(self, p):
419+
# docstring inherited
420+
# After deprecation, the whole method can be deleted and inherited.
421+
if isinstance(p, Number) and not isinstance(p, bool):
422+
cbook.warn_deprecated(
423+
"3.4", message="Setting the collections's pick radius via "
424+
"set_picker is deprecated since %(since)s and will be removed "
425+
"%(removal)s; use set_pickradius instead.")
426+
self.set_pickradius(p)
427+
self._picker = p
428+
418429
def set_pickradius(self, pr):
419430
"""
420431
Set the pick radius used for containment tests.
@@ -439,32 +450,21 @@ def contains(self, mouseevent):
439450
inside, info = self._default_contains(mouseevent)
440451
if inside is not None:
441452
return inside, info
442-
443453
if not self.get_visible():
444454
return False, {}
445-
446-
pickradius = (
447-
float(self._picker)
448-
if isinstance(self._picker, Number) and
449-
self._picker is not True # the bool, not just nonzero or 1
450-
else self._pickradius)
451-
452455
if self.axes:
453456
self.axes._unstale_viewLim()
454-
455457
transform, transOffset, offsets, paths = self._prepare_points()
456-
457458
# Tests if the point is contained on one of the polygons formed
458459
# by the control points of each of the paths. A point is considered
459460
# "on" a path if it would lie within a stroke of width 2*pickradius
460461
# following the path. If pickradius <= 0, then we instead simply check
461462
# if the point is *inside* of the path instead.
462463
ind = _path.point_in_path_collection(
463-
mouseevent.x, mouseevent.y, pickradius,
464+
mouseevent.x, mouseevent.y, self._pickradius,
464465
transform.frozen(), paths, self.get_transforms(),
465-
offsets, transOffset, pickradius <= 0,
466+
offsets, transOffset, self._pickradius <= 0,
466467
self._offset_position)
467-
468468
return len(ind) > 0, dict(ind=ind)
469469

470470
def set_urls(self, urls):

lib/matplotlib/patches.py

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def __init__(self,
9494
# unscaled dashes. Needed to scale dash patterns by lw
9595
self._us_dashes = None
9696
self._linewidth = 0
97+
self._pickradius = None
9798

9899
self.set_fill(fill)
99100
self.set_linestyle(linestyle)
@@ -102,9 +103,7 @@ def __init__(self,
102103
self.set_hatch(hatch)
103104
self.set_capstyle(capstyle)
104105
self.set_joinstyle(joinstyle)
105-
106-
if len(kwargs):
107-
self.update(kwargs)
106+
self.update(kwargs)
108107

109108
def get_verts(self):
110109
"""
@@ -120,17 +119,52 @@ def get_verts(self):
120119
return polygons[0]
121120
return []
122121

122+
def set_picker(self, p):
123+
# docstring inherited
124+
# After deprecation, the whole method can be deleted and inherited.
125+
if isinstance(p, Number) and not isinstance(p, bool):
126+
cbook.warn_deprecated(
127+
"3.4", message="Setting the collections's pick radius via "
128+
"set_picker is deprecated since %(since)s and will be removed "
129+
"%(removal)s; use set_pickradius instead.")
130+
self.set_pickradius(p)
131+
self._picker = p
132+
133+
def set_pickradius(self, d):
134+
"""
135+
Set the pick radius used for containment tests.
136+
137+
Parameters
138+
----------
139+
d : float or None
140+
The pick radius, in points. If None, the patch linewidth is used
141+
as pickradius if the patch edge is not transparent.
142+
"""
143+
if not isinstance(d, Number) or d < 0:
144+
raise ValueError("pick radius should be a distance")
145+
self._pickradius = d
146+
147+
def get_pickradius(self):
148+
"""
149+
Return the pick radius used for containment tests.
150+
151+
Returns
152+
-------
153+
float or None
154+
The pick radius, in points. If None, the patch linewidth is used
155+
as pickradius if the patch edge is not transparent.
156+
"""
157+
return self._pickradius
158+
123159
def _process_radius(self, radius):
124-
if radius is not None:
125-
return radius
126-
if isinstance(self._picker, Number):
127-
_radius = self._picker
128-
else:
129-
if self.get_edgecolor()[3] == 0:
130-
_radius = 0
131-
else:
132-
_radius = self.get_linewidth()
133-
return _radius
160+
return (
161+
radius if radius is not None
162+
else self._pickradius if self._pickradius is not None
163+
# Deprecated case.
164+
else self._picker if (isinstance(self._picker, Number)
165+
and not isinstance(self._picker, bool))
166+
else 0 if self.get_edgecolor()[3] == 0
167+
else self.get_linewidth())
134168

135169
def contains(self, mouseevent, radius=None):
136170
"""

lib/matplotlib/tests/test_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ def test_hexbin_pickable():
757757
fig, ax = plt.subplots()
758758
data = (np.arange(200) / 200).reshape((2, 100))
759759
x, y = data
760-
hb = ax.hexbin(x, y, extent=[.1, .3, .6, .7], picker=-1)
760+
hb = ax.hexbin(x, y, extent=[.1, .3, .6, .7], picker=True, pickradius=-1)
761761
mouse_event = SimpleNamespace(x=400, y=300)
762762
assert hb.contains(mouse_event)[0]
763763

0 commit comments

Comments
 (0)
0