Skip to content

Backport PR #19741 on branch v3.4.x (Only override pickradius when picker is not a bool.) #19742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
Expand Down
27 changes: 27 additions & 0 deletions lib/matplotlib/tests/test_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import itertools
import timeit
from types import SimpleNamespace

from cycler import cycler
import numpy as np
Expand Down Expand Up @@ -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])