diff --git a/lib/matplotlib/lines.py b/lib/matplotlib/lines.py index 7fad0c59f0e2..1203862a3f50 100644 --- a/lib/matplotlib/lines.py +++ b/lib/matplotlib/lines.py @@ -397,7 +397,8 @@ def __init__(self, xdata, ydata, self.update(kwargs) self.pickradius = pickradius self.ind_offset = 0 - if isinstance(self._picker, Number): + if (isinstance(self._picker, Number) and + not isinstance(self._picker, bool)): self.pickradius = self._picker self._xorig = np.asarray([]) diff --git a/lib/matplotlib/tests/test_lines.py b/lib/matplotlib/tests/test_lines.py index 815146ed0bc7..def26456bb36 100644 --- a/lib/matplotlib/tests/test_lines.py +++ b/lib/matplotlib/tests/test_lines.py @@ -4,6 +4,7 @@ import itertools import timeit +from types import SimpleNamespace from cycler import cycler import numpy as np @@ -264,3 +265,29 @@ def test_marker_as_markerstyle(): def test_odd_dashes(fig_test, fig_ref): fig_test.add_subplot().plot([1, 2], dashes=[1, 2, 3]) fig_ref.add_subplot().plot([1, 2], dashes=[1, 2, 3, 1, 2, 3]) + + +def test_picking(): + fig, ax = plt.subplots() + mouse_event = SimpleNamespace(x=fig.bbox.width // 2, + y=fig.bbox.height // 2 + 15) + + # Default pickradius is 5, so event should not pick this line. + l0, = ax.plot([0, 1], [0, 1], picker=True) + found, indices = l0.contains(mouse_event) + assert not found + + # But with a larger pickradius, this should be picked. + l1, = ax.plot([0, 1], [0, 1], picker=True, pickradius=20) + found, indices = l1.contains(mouse_event) + assert found + assert_array_equal(indices['ind'], [0]) + + # And if we modify the pickradius after creation, it should work as well. + l2, = ax.plot([0, 1], [0, 1], picker=True) + found, indices = l2.contains(mouse_event) + assert not found + l2.set_pickradius(20) + found, indices = l2.contains(mouse_event) + assert found + assert_array_equal(indices['ind'], [0])