Skip to content

Commit 67352c8

Browse files
committed
Creates a backward compatibility layer for nxutils. nxutils is now implemented in terms of the methods in _path.cpp. A "points_in_path" method has been added to support "nxutils.points_in_poly".
1 parent b765d87 commit 67352c8

File tree

4 files changed

+248
-75
lines changed

4 files changed

+248
-75
lines changed

examples/event_handling/lasso_demo.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
inside polygon detection routine.
99
"""
1010
from matplotlib.widgets import Lasso
11-
from matplotlib.nxutils import points_inside_poly
1211
from matplotlib.colors import colorConverter
1312
from matplotlib.collections import RegularPolyCollection
13+
from matplotlib import path
1414

1515
from matplotlib.pyplot import figure, show
1616
from numpy import nonzero
@@ -50,7 +50,8 @@ def __init__(self, ax, data):
5050

5151
def callback(self, verts):
5252
facecolors = self.collection.get_facecolors()
53-
ind = nonzero(points_inside_poly(self.xys, verts))[0]
53+
p = path.Path(verts)
54+
ind = nonzero(p.contains_points(self.xys))[0]
5455
for i in range(self.Nxy):
5556
if i in ind:
5657
facecolors[i] = Datum.colorin

lib/matplotlib/nxutils.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import warnings
2+
3+
from matplotlib import path
4+
5+
def pnpoly(x, y, xyverts):
6+
"""
7+
inside = pnpoly(x, y, xyverts)
8+
9+
Return 1 if x,y is inside the polygon, 0 otherwise.
10+
11+
*xyverts*
12+
a sequence of x,y vertices.
13+
14+
A point on the boundary may be treated as inside or outside.
15+
16+
Deprecated: Use `matplotlib.path.Path.contains_point` instead.
17+
"""
18+
warings.warn(
19+
DeprecationWarning,
20+
"nxutils is deprecated. Use matplotlib.path.Path.contains_point instead.")
21+
22+
p = path.Path(xyverts)
23+
return p.contains_point(x, y)
24+
25+
def points_inside_poly(xypoints, xyverts):
26+
"""
27+
mask = points_inside_poly(xypoints, xyverts)
28+
29+
Returns a boolean ndarray, True for points inside the polygon.
30+
31+
*xypoints*
32+
a sequence of N x,y pairs.
33+
34+
*xyverts*
35+
sequence of x,y vertices of the polygon.
36+
37+
A point on the boundary may be treated as inside or outside.
38+
39+
Deprecated: Use `matplotlib.path.Path.contains_points` instead.
40+
"""
41+
warnings.warn(
42+
DeprecationWarning,
43+
"nxutils is deprecated. Use matplotlib.path.Path.contains_points instead.")
44+
45+
p = path.Path(xyverts)
46+
return p.contains_points(xypoints)

lib/matplotlib/path.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from matplotlib._path import point_in_path, get_path_extents, \
1313
point_in_path_collection, get_path_collection_extents, \
1414
path_in_path, path_intersects_path, convert_path_to_polygons, \
15-
cleanup_path
15+
cleanup_path, points_in_path
1616
from matplotlib.cbook import simple_linear_interpolation, maxdict
1717
from matplotlib import rcParams
1818

@@ -280,12 +280,31 @@ def contains_point(self, point, transform=None, radius=0.0):
280280
281281
If *transform* is not *None*, the path will be transformed
282282
before performing the test.
283+
284+
*radius* allows the path to be made slightly larger or
285+
smaller.
283286
"""
284287
if transform is not None:
285288
transform = transform.frozen()
286289
result = point_in_path(point[0], point[1], radius, self, transform)
287290
return result
288291

292+
def contains_points(self, points, transform=None, radius=0.0):
293+
"""
294+
Returns a bool array which is *True* if the path contains the
295+
corresponding point.
296+
297+
If *transform* is not *None*, the path will be transformed
298+
before performing the test.
299+
300+
*radius* allows the path to be made slightly larger or
301+
smaller.
302+
"""
303+
if transform is not None:
304+
transform = transform.frozen()
305+
result = points_in_path(points, radius, self, transform)
306+
return result
307+
289308
def contains_path(self, path, transform=None):
290309
"""
291310
Returns *True* if this path completely contains the given path.

0 commit comments

Comments
 (0)