From 13aeba6ba19d1a0890990780db66dc145cff5c17 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 18 Mar 2021 19:07:12 -0400 Subject: [PATCH] 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. --- lib/matplotlib/lines.py | 3 ++- lib/matplotlib/tests/test_lines.py | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) 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])