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

Skip to content

Commit 8faa835

Browse files
authored
Merge pull request #25848 from anntzer/ld
Simplify lasso_demo example.
2 parents bdf5399 + 99b5f0b commit 8faa835

File tree

1 file changed

+25
-65
lines changed

1 file changed

+25
-65
lines changed

galleries/examples/event_handling/lasso_demo.py

Lines changed: 25 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
Lasso Demo
44
==========
55
6-
Show how to use a lasso to select a set of points and get the indices
7-
of the selected points. A callback is used to change the color of the
8-
selected points
9-
10-
This is currently a proof-of-concept implementation (though it is
11-
usable as is). There will be some refinement of the API.
6+
Use a lasso to select a set of points and get the indices of the selected points.
7+
A callback is used to change the color of the selected points.
128
139
.. note::
1410
This example exercises the interactive capabilities of Matplotlib, and this
@@ -28,79 +24,43 @@
2824
from matplotlib.widgets import Lasso
2925

3026

31-
class Datum:
32-
colorin = mcolors.to_rgba("red")
33-
colorout = mcolors.to_rgba("blue")
34-
35-
def __init__(self, x, y, include=False):
36-
self.x = x
37-
self.y = y
38-
if include:
39-
self.color = self.colorin
40-
else:
41-
self.color = self.colorout
42-
43-
4427
class LassoManager:
4528
def __init__(self, ax, data):
46-
self.axes = ax
47-
self.canvas = ax.figure.canvas
48-
self.data = data
49-
50-
self.Nxy = len(data)
51-
52-
facecolors = [d.color for d in data]
53-
self.xys = [(d.x, d.y) for d in data]
29+
# The information of whether a point has been selected or not is stored in the
30+
# collection's array (0 = out, 1 = in), which then gets colormapped to blue
31+
# (out) and red (in).
5432
self.collection = RegularPolyCollection(
55-
6, sizes=(100,),
56-
facecolors=facecolors,
57-
offsets=self.xys,
58-
offset_transform=ax.transData)
59-
33+
6, sizes=(100,), offset_transform=ax.transData,
34+
offsets=data, array=np.zeros(len(data)),
35+
clim=(0, 1), cmap=mcolors.ListedColormap(["tab:blue", "tab:red"]))
6036
ax.add_collection(self.collection)
61-
62-
self.cid_press = self.canvas.mpl_connect('button_press_event',
63-
self.on_press)
64-
self.cid_release = self.canvas.mpl_connect('button_release_event',
65-
self.on_release)
37+
canvas = ax.figure.canvas
38+
canvas.mpl_connect('button_press_event', self.on_press)
39+
canvas.mpl_connect('button_release_event', self.on_release)
6640

6741
def callback(self, verts):
68-
facecolors = self.collection.get_facecolors()
69-
p = path.Path(verts)
70-
ind = p.contains_points(self.xys)
71-
for i in range(len(self.xys)):
72-
if ind[i]:
73-
facecolors[i] = Datum.colorin
74-
else:
75-
facecolors[i] = Datum.colorout
76-
77-
self.canvas.draw_idle()
42+
data = self.collection.get_offsets()
43+
self.collection.set_array(path.Path(verts).contains_points(data))
44+
canvas = self.collection.figure.canvas
45+
canvas.draw_idle()
7846
del self.lasso
7947

8048
def on_press(self, event):
81-
if self.canvas.widgetlock.locked():
49+
canvas = self.collection.figure.canvas
50+
if event.inaxes is not self.collection.axes or canvas.widgetlock.locked():
8251
return
83-
if event.inaxes is None:
84-
return
85-
self.lasso = Lasso(event.inaxes,
86-
(event.xdata, event.ydata),
87-
self.callback)
88-
# acquire a lock on the widget drawing
89-
self.canvas.widgetlock(self.lasso)
52+
self.lasso = Lasso(event.inaxes, (event.xdata, event.ydata), self.callback)
53+
canvas.widgetlock(self.lasso) # acquire a lock on the widget drawing
9054

9155
def on_release(self, event):
92-
if hasattr(self, 'lasso') and self.canvas.widgetlock.isowner(self.lasso):
93-
self.canvas.widgetlock.release(self.lasso)
56+
canvas = self.collection.figure.canvas
57+
if hasattr(self, 'lasso') and canvas.widgetlock.isowner(self.lasso):
58+
canvas.widgetlock.release(self.lasso)
9459

9560

9661
if __name__ == '__main__':
97-
9862
np.random.seed(19680801)
99-
100-
data = [Datum(*xy) for xy in np.random.rand(100, 2)]
101-
ax = plt.axes(xlim=(0, 1), ylim=(0, 1), autoscale_on=False)
102-
ax.set_title('Lasso points using left mouse button')
103-
104-
lman = LassoManager(ax, data)
105-
63+
ax = plt.figure().add_subplot(
64+
xlim=(0, 1), ylim=(0, 1), title='Lasso points using left mouse button')
65+
manager = LassoManager(ax, np.random.rand(100, 2))
10666
plt.show()

0 commit comments

Comments
 (0)