Skip to content

Unify set_pickradius argument #23196

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 1 commit into from
Aug 4, 2022
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
1 change: 1 addition & 0 deletions doc/api/axis_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Interactive
:nosignatures:

Axis.contains
Axis.pickradius
Axis.get_pickradius
Axis.set_pickradius

Expand Down
23 changes: 16 additions & 7 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import datetime
import functools
import logging
from numbers import Number

import numpy as np

Expand Down Expand Up @@ -1357,7 +1358,7 @@ def get_offset_text(self):

def get_pickradius(self):
"""Return the depth of the axis used by the picker."""
return self.pickradius
return self._pickradius

def get_majorticklabels(self):
"""Return this Axis' major tick labels, as a list of `~.text.Text`."""
Expand Down Expand Up @@ -1831,9 +1832,17 @@ def set_pickradius(self, pickradius):

Parameters
----------
pickradius : float
pickradius : float
The acceptance radius for containment tests.
See also `.Axis.contains`.
"""
self.pickradius = pickradius
if not isinstance(pickradius, Number) or pickradius < 0:
raise ValueError("pick radius should be a distance")
self._pickradius = pickradius

pickradius = property(
get_pickradius, set_pickradius, doc="The acceptance radius for "
"containment tests. See also `.Axis.contains`.")

# Helper for set_ticklabels. Defining it here makes it picklable.
@staticmethod
Expand Down Expand Up @@ -2217,8 +2226,8 @@ def contains(self, mouseevent):
return False, {}
(l, b), (r, t) = self.axes.transAxes.transform([(0, 0), (1, 1)])
inaxis = 0 <= xaxes <= 1 and (
b - self.pickradius < y < b or
t < y < t + self.pickradius)
b - self._pickradius < y < b or
t < y < t + self._pickradius)
return inaxis, {}

def set_label_position(self, position):
Expand Down Expand Up @@ -2470,8 +2479,8 @@ def contains(self, mouseevent):
return False, {}
(l, b), (r, t) = self.axes.transAxes.transform([(0, 0), (1, 1)])
inaxis = 0 <= yaxes <= 1 and (
l - self.pickradius < x < l or
r < x < r + self.pickradius)
l - self._pickradius < x < l or
r < x < r + self._pickradius)
return inaxis, {}

def set_label_position(self, position):
Expand Down
7 changes: 4 additions & 3 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,16 +419,17 @@ def draw(self, renderer):
renderer.close_group(self.__class__.__name__)
self.stale = False

def set_pickradius(self, pr):
@_api.rename_parameter("3.6", "pr", "pickradius")
def set_pickradius(self, pickradius):
"""
Set the pick radius used for containment tests.

Parameters
----------
pr : float
pickradius : float
Pick radius, in points.
"""
self._pickradius = pr
self._pickradius = pickradius

def get_pickradius(self):
return self._pickradius
Expand Down
19 changes: 10 additions & 9 deletions lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,11 @@ def __init__(self, xdata, ydata,
# update kwargs before updating data to give the caller a
# chance to init axes (and hence unit support)
self._internal_update(kwargs)
self.pickradius = pickradius
self._pickradius = pickradius
self.ind_offset = 0
if (isinstance(self._picker, Number) and
not isinstance(self._picker, bool)):
self.pickradius = self._picker
self._pickradius = self._picker

self._xorig = np.asarray([])
self._yorig = np.asarray([])
Expand Down Expand Up @@ -455,9 +455,9 @@ def contains(self, mouseevent):
# Convert pick radius from points to pixels
if self.figure is None:
_log.warning('no figure set when check if mouse is on line')
pixels = self.pickradius
pixels = self._pickradius
else:
pixels = self.figure.dpi / 72. * self.pickradius
pixels = self.figure.dpi / 72. * self._pickradius

# The math involved in checking for containment (here and inside of
# segment_hits) assumes that it is OK to overflow, so temporarily set
Expand Down Expand Up @@ -488,20 +488,21 @@ def get_pickradius(self):
"""
return self._pickradius

def set_pickradius(self, d):
@_api.rename_parameter("3.6", "d", "pickradius")
def set_pickradius(self, pickradius):
"""
Set the pick radius used for containment tests.

See `.contains` for more details.

Parameters
----------
d : float
pickradius : float
Pick radius, in points.
"""
if not isinstance(d, Number) or d < 0:
if not isinstance(pickradius, Number) or pickradius < 0:
raise ValueError("pick radius should be a distance")
self._pickradius = d
self._pickradius = pickradius

pickradius = property(get_pickradius, set_pickradius)

Expand Down Expand Up @@ -612,7 +613,7 @@ def set_picker(self, p):
if callable(p):
self._contains = p
else:
self.pickradius = p
self.set_pickradius(p)
self._picker = p

def get_bbox(self):
Expand Down