Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 13aeba6

Browse files
committed
Only override pickradius when picker is not a bool.
This was done when the deprecation was added, but then removed when it was undeprecated. However, people now seem to be relying on this new behaviour, and it seems more correct, so we should add it back. Fixes #19700.
1 parent afbcb50 commit 13aeba6

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/matplotlib/lines.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ def __init__(self, xdata, ydata,
397397
self.update(kwargs)
398398
self.pickradius = pickradius
399399
self.ind_offset = 0
400-
if isinstance(self._picker, Number):
400+
if (isinstance(self._picker, Number) and
401+
not isinstance(self._picker, bool)):
401402
self.pickradius = self._picker
402403

403404
self._xorig = np.asarray([])

lib/matplotlib/tests/test_lines.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import itertools
66
import timeit
7+
from types import SimpleNamespace
78

89
from cycler import cycler
910
import numpy as np
@@ -264,3 +265,29 @@ def test_marker_as_markerstyle():
264265
def test_odd_dashes(fig_test, fig_ref):
265266
fig_test.add_subplot().plot([1, 2], dashes=[1, 2, 3])
266267
fig_ref.add_subplot().plot([1, 2], dashes=[1, 2, 3, 1, 2, 3])
268+
269+
270+
def test_picking():
271+
fig, ax = plt.subplots()
272+
mouse_event = SimpleNamespace(x=fig.bbox.width // 2,
273+
y=fig.bbox.height // 2 + 15)
274+
275+
# Default pickradius is 5, so event should not pick this line.
276+
l0, = ax.plot([0, 1], [0, 1], picker=True)
277+
found, indices = l0.contains(mouse_event)
278+
assert not found
279+
280+
# But with a larger pickradius, this should be picked.
281+
l1, = ax.plot([0, 1], [0, 1], picker=True, pickradius=20)
282+
found, indices = l1.contains(mouse_event)
283+
assert found
284+
assert_array_equal(indices['ind'], [0])
285+
286+
# And if we modify the pickradius after creation, it should work as well.
287+
l2, = ax.plot([0, 1], [0, 1], picker=True)
288+
found, indices = l2.contains(mouse_event)
289+
assert not found
290+
l2.set_pickradius(20)
291+
found, indices = l2.contains(mouse_event)
292+
assert found
293+
assert_array_equal(indices['ind'], [0])

0 commit comments

Comments
 (0)