From 91f47d6eff63187f582c395c007d0152980be6b3 Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Fri, 3 Jun 2022 14:32:46 +0200 Subject: [PATCH] Unify set_pickradius argument --- doc/api/axis_api.rst | 1 + lib/matplotlib/axis.py | 23 ++++++++++++++++------- lib/matplotlib/collections.py | 7 ++++--- lib/matplotlib/lines.py | 19 ++++++++++--------- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/doc/api/axis_api.rst b/doc/api/axis_api.rst index 80a6612fa165..d950d1e253a4 100644 --- a/doc/api/axis_api.rst +++ b/doc/api/axis_api.rst @@ -150,6 +150,7 @@ Interactive :nosignatures: Axis.contains + Axis.pickradius Axis.get_pickradius Axis.set_pickradius diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index b3524d00c917..d3e6ca4004ff 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -5,6 +5,7 @@ import datetime import functools import logging +from numbers import Number import numpy as np @@ -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`.""" @@ -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 @@ -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): @@ -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): diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index 5caef5bc8e25..cef043e79ddb 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -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 diff --git a/lib/matplotlib/lines.py b/lib/matplotlib/lines.py index 703307ee584a..fffdf1896ffd 100644 --- a/lib/matplotlib/lines.py +++ b/lib/matplotlib/lines.py @@ -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([]) @@ -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 @@ -488,7 +488,8 @@ 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. @@ -496,12 +497,12 @@ def set_pickradius(self, d): 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) @@ -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):