|
3 | 3 | Lasso Demo |
4 | 4 | ========== |
5 | 5 |
|
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. |
12 | 8 |
|
13 | 9 | .. note:: |
14 | 10 | This example exercises the interactive capabilities of Matplotlib, and this |
|
28 | 24 | from matplotlib.widgets import Lasso |
29 | 25 |
|
30 | 26 |
|
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 | | - |
44 | 27 | class LassoManager: |
45 | 28 | 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). |
54 | 32 | 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"])) |
60 | 36 | 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) |
66 | 40 |
|
67 | 41 | 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() |
78 | 46 | del self.lasso |
79 | 47 |
|
80 | 48 | 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(): |
82 | 51 | 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 |
90 | 54 |
|
91 | 55 | 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) |
94 | 59 |
|
95 | 60 |
|
96 | 61 | if __name__ == '__main__': |
97 | | - |
98 | 62 | 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)) |
106 | 66 | plt.show() |
0 commit comments