Skip to content

Commit af2efa4

Browse files
committed
Merge pull request matplotlib#746 from mdboom/nxutils_backward
Provide backward-compatible nxutils
2 parents d1cb66e + 243f4ff commit af2efa4

File tree

4 files changed

+247
-83
lines changed

4 files changed

+247
-83
lines changed

Diff for: examples/event_handling/lasso_demo.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,28 @@
44
selected points
55
66
This is currently a proof-of-concept implementation (though it is
7-
usable as is). There will be some refinement of the API and the
8-
inside polygon detection routine.
7+
usable as is). There will be some refinement of the API.
98
"""
109
from matplotlib.widgets import Lasso
11-
from matplotlib.nxutils import points_inside_poly
1210
from matplotlib.colors import colorConverter
1311
from matplotlib.collections import RegularPolyCollection
12+
from matplotlib import path
1413

15-
from matplotlib.pyplot import figure, show
14+
import matplotlib.pyplot as plt
1615
from numpy import nonzero
1716
from numpy.random import rand
1817

19-
class Datum:
18+
class Datum(object):
2019
colorin = colorConverter.to_rgba('red')
21-
colorout = colorConverter.to_rgba('green')
20+
colorout = colorConverter.to_rgba('blue')
2221
def __init__(self, x, y, include=False):
2322
self.x = x
2423
self.y = y
2524
if include: self.color = self.colorin
2625
else: self.color = self.colorout
2726

2827

29-
class LassoManager:
28+
class LassoManager(object):
3029
def __init__(self, ax, data):
3130
self.axes = ax
3231
self.canvas = ax.figure.canvas
@@ -46,21 +45,21 @@ def __init__(self, ax, data):
4645
ax.add_collection(self.collection)
4746

4847
self.cid = self.canvas.mpl_connect('button_press_event', self.onpress)
49-
self.ind = None
5048

5149
def callback(self, verts):
5250
facecolors = self.collection.get_facecolors()
53-
ind = nonzero(points_inside_poly(self.xys, verts))[0]
54-
for i in range(self.Nxy):
55-
if i in ind:
51+
p = path.Path(verts)
52+
ind = p.contains_points(self.xys)
53+
for i in range(len(self.xys)):
54+
if ind[i]:
5655
facecolors[i] = Datum.colorin
5756
else:
5857
facecolors[i] = Datum.colorout
5958

6059
self.canvas.draw_idle()
6160
self.canvas.widgetlock.release(self.lasso)
6261
del self.lasso
63-
self.ind = ind
62+
6463
def onpress(self, event):
6564
if self.canvas.widgetlock.locked(): return
6665
if event.inaxes is None: return
@@ -72,8 +71,7 @@ def onpress(self, event):
7271

7372
data = [Datum(*xy) for xy in rand(100, 2)]
7473

75-
fig = figure()
76-
ax = fig.add_subplot(111, xlim=(0,1), ylim=(0,1), autoscale_on=False)
74+
ax = plt.axes(xlim=(0,1), ylim=(0,1), autoscale_on=False)
7775
lman = LassoManager(ax, data)
7876

79-
show()
77+
plt.show()

Diff for: lib/matplotlib/nxutils.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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:: 1.2.0
17+
Use :meth:`~matplotlib.path.Path.contains_point` instead.
18+
"""
19+
warings.warn(
20+
"nxutils is deprecated. Use matplotlib.path.Path.contains_point"
21+
" instead.",
22+
DeprecationWarning)
23+
24+
p = path.Path(xyverts)
25+
return p.contains_point(x, y)
26+
27+
def points_inside_poly(xypoints, xyverts):
28+
"""
29+
mask = points_inside_poly(xypoints, xyverts)
30+
31+
Returns a boolean ndarray, True for points inside the polygon.
32+
33+
*xypoints*
34+
a sequence of N x,y pairs.
35+
36+
*xyverts*
37+
sequence of x,y vertices of the polygon.
38+
39+
A point on the boundary may be treated as inside or outside.
40+
41+
.. deprecated:: 1.2.0
42+
Use :meth:`~matplotlib.path.Path.contains_points` instead.
43+
"""
44+
warnings.warn(
45+
"nxutils is deprecated. Use matplotlib.path.Path.contains_points"
46+
" instead.",
47+
DeprecationWarning)
48+
49+
p = path.Path(xyverts)
50+
return p.contains_points(xypoints)

Diff for: lib/matplotlib/path.py

+20-1
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)