Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8c68617

Browse files
authored
Merge pull request #15216 from timhoffm/doc-contains-point
DOC: Update docstrings of contains_point(s) methods
2 parents 82918ac + 4e2195a commit 8c68617

File tree

2 files changed

+99
-19
lines changed

2 files changed

+99
-19
lines changed

lib/matplotlib/patches.py

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,47 @@ def contains(self, mouseevent, radius=None):
154154

155155
def contains_point(self, point, radius=None):
156156
"""
157-
Returns ``True`` if the given *point* is inside the path
158-
(transformed with its transform attribute).
157+
Return whether the given point is inside the patch.
158+
159+
Parameters
160+
----------
161+
point : (float, float)
162+
The point (x, y) to check in target coordinates of
163+
``self.get_transform()``. For patches added to a figure or axes,
164+
these are display coordinates.
165+
radius : float, optional
166+
Adds an additional margin on the patch in coordinates of transform.
167+
target. See `.Path.contains_point` for further details.
168+
169+
Returns
170+
-------
171+
bool
172+
173+
Notes
174+
-----
175+
The proper use of this method depends on the transform of the patch.
176+
Isolated patches do not have a transform. In this, the patch creation
177+
coordinates and the point coordinates match. The follow checks that
178+
the center of a circle is within the circle
179+
180+
>>> center = 0, 0
181+
>>> c = Circle(center, radius=1)
182+
>>> c.contains_point(center)
183+
True
184+
185+
The convention of checking against the transformed patch stems from
186+
the fact that this method is predominantly used to check if display
187+
coordinates (e.g. from mouse events) are within the patch. If you want
188+
to do the above check with data coordinates, you have to properly
189+
transform them first:
190+
191+
>>> center = 0, 0
192+
>>> c = Circle(center, radius=1)
193+
>>> plt.gca().add_patch(c)
194+
>>> transformed_center = c.get_transform().transform(center)
195+
>>> c.contains_point(transformed_center)
196+
True
159197
160-
*radius* allows the path to be made slightly larger or smaller.
161198
"""
162199
radius = self._process_radius(radius)
163200
return self.get_path().contains_point(point,
@@ -166,12 +203,26 @@ def contains_point(self, point, radius=None):
166203

167204
def contains_points(self, points, radius=None):
168205
"""
169-
Returns a bool array which is ``True`` if the (closed) path
170-
contains the corresponding point.
171-
(transformed with its transform attribute).
206+
Return whether the given points are inside the patch.
172207
173-
*points* must be Nx2 array.
174-
*radius* allows the path to be made slightly larger or smaller.
208+
Parameters
209+
----------
210+
points : (N, 2) array
211+
The points to check in target coordinates of
212+
``self.get_transform()``. For patches added to a figure or axes,
213+
these are display coordinates. Columns contain x and y values.
214+
radius : float, optional
215+
Adds an additional margin on the patch in coordinates of transform.
216+
target. See `.Path.contains_points` for further details.
217+
218+
Returns
219+
-------
220+
length-N bool array
221+
222+
Notes
223+
-----
224+
The proper use of this method depends on the transform of the patch.
225+
See the notes on `.Patch.contains_point`.
175226
"""
176227
radius = self._process_radius(radius)
177228
return self.get_path().contains_points(points,

lib/matplotlib/path.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,27 @@ def transformed(self, transform):
450450

451451
def contains_point(self, point, transform=None, radius=0.0):
452452
"""
453-
Returns whether the (closed) path contains the given point.
453+
Return whether the (closed) path contains the given point.
454454
455-
If *transform* is not ``None``, the path will be transformed before
456-
performing the test.
457-
458-
*radius* allows the path to be made slightly larger or smaller.
455+
Parameters
456+
----------
457+
point : (float, float)
458+
The point (x, y) to check.
459+
transform : `matplotlib.transforms.Transform`, optional
460+
If not ``None``, *point* will be compared to self transformed
461+
by *transform*; i.e. for a correct check, *transform* should
462+
transform the path into the coordinate system of *point*.
463+
radius : float, default: 0.
464+
Adds an additional margin on the path in coordinates of *point*.
465+
The path is extended tangentially by *radius/2*; i.e. if you would
466+
draw the path with a linewidth of *radius*, all points on the line
467+
would still be considered to be contained in the area. Conversely,
468+
negative values shrink the area; points on the imaginary line
469+
will be considered outside the area.
470+
471+
Returns
472+
-------
473+
bool
459474
"""
460475
if transform is not None:
461476
transform = transform.frozen()
@@ -470,13 +485,27 @@ def contains_point(self, point, transform=None, radius=0.0):
470485

471486
def contains_points(self, points, transform=None, radius=0.0):
472487
"""
473-
Returns a bool array which is ``True`` if the (closed) path contains
474-
the corresponding point.
488+
Return whether the (closed) path contains the given point.
475489
476-
If *transform* is not ``None``, the path will be transformed before
477-
performing the test.
478-
479-
*radius* allows the path to be made slightly larger or smaller.
490+
Parameters
491+
----------
492+
points : (N, 2) array
493+
The points to check. Columns contain x and y values.
494+
transform : `matplotlib.transforms.Transform`, optional
495+
If not ``None``, *points* will be compared to self transformed
496+
by *transform*; i.e. for a correct check, *transform* should
497+
transform the path into the coordinate system of *points*.
498+
radius : float, default: 0.
499+
Adds an additional margin on the path in coordinates of *points*.
500+
The path is extended tangentially by *radius/2*; i.e. if you would
501+
draw the path with a linewidth of *radius*, all points on the line
502+
would still be considered to be contained in the area. Conversely,
503+
negative values shrink the area; points on the imaginary line
504+
will be considered outside the area.
505+
506+
Returns
507+
-------
508+
length-N bool array
480509
"""
481510
if transform is not None:
482511
transform = transform.frozen()

0 commit comments

Comments
 (0)