8000 Split apart pick event demo · matplotlib/matplotlib@46c44b6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 46c44b6

Browse files
committed
Split apart pick event demo
In the docs, all the figures show up scrunched at the end after a large block of code. But each figure is self-contained and already contains some documentation, so split them up based on that.
1 parent c17f475 commit 46c44b6

File tree

1 file changed

+121
-110
lines changed

1 file changed

+121
-110
lines changed

examples/event_handling/pick_event_demo.py

Lines changed: 121 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -81,114 +81,125 @@ def pick_handler(event):
8181
np.random.seed(19680801)
8282

8383

84-
def pick_simple():
85-
# simple picking, lines, rectangles and text
86-
fig, (ax1, ax2) = plt.subplots(2, 1)
87-
ax1.set_title('click on points, rectangles or text', picker=True)
88-
ax1.set_ylabel('ylabel', picker=True, bbox=dict(facecolor='red'))
89-
line, = ax1.plot(rand(100), 'o', picker=True, pickradius=5)
90-
91-
# pick the rectangle
92-
ax2.bar(range(10), rand(10), picker=True)
93-
for label in ax2.get_xticklabels(): # make the xtick labels pickable
94-
label.set_picker(True)
95-
96-
def onpick1(event):
97-
if isinstance(event.artist, Line2D):
98-
thisline = event.artist
99-
xdata = thisline.get_xdata()
100-
ydata = thisline.get_ydata()
101-
ind = event.ind
102-
print('onpick1 line:', np.column_stack([xdata[ind], ydata[ind]]))
103-
elif isinstance(event.artist, Rectangle):
104-
patch = event.artist
105-
print('onpick1 patch:', patch.get_path())
106-
elif isinstance(event.artist, Text):
107-
text = event.artist
108-
print('onpick1 text:', text.get_text())
109-
110-
fig.canvas.mpl_connect('pick_event', onpick1)
111-
112-
113-
def pick_custom_hit():
114-
# picking with a custom hit test function
115-
# you can define custom pickers by setting picker to a callable
116-
# function. The function has the signature
117-
#
118-
# hit, props = func(artist, mouseevent)
119-
#
120-
# to determine the hit test. if the mouse event is over the artist,
121-
# return hit=True and props is a dictionary of
122-
# properties you want added to the PickEvent attributes
123-
124-
def line_picker(line, mouseevent):
125-
"""
126-
Find the points within a certain distance from the mouseclick in
127-
data coords and attach some extra attributes, pickx and picky
128-
which are the data points that were picked.
129-
"""
130-
if mouseevent.xdata is None:
131-
return False, dict()
132-
xdata = line.get_xdata()
133-
ydata = line.get_ydata()
134-
maxd = 0.05
135-
d = np.sqrt(
136-
(xdata - mouseevent.xdata)**2 + (ydata - mouseevent.ydata)**2)
137-
138-
ind, = np.nonzero(d <= maxd)
139-
if len(ind):
140-
pickx = xdata[ind]
141-
picky = ydata[ind]
142-
props = dict(ind=ind, pickx=pickx, picky=picky)
143-
return True, props
144-
else:
145-
return False, dict()
146-
147-
def onpick2(event):
148-
print('onpick2 line:', event.pickx, event.picky)
149-
150-
fig, ax = plt.subplots()
151-
ax.set_title('custom picker for line data')
152-
line, = ax.plot(rand(100), rand(100), 'o', picker=line_picker)
153-
fig.canvas.mpl_connect('pick_event', onpick2)
154-
155-
156-
def pick_scatter_plot():
157-
# picking on a scatter plot (matplotlib.collections.RegularPolyCollection)
158-
159-
x, y, c, s = rand(4, 100)
160-
161-
def onpick3(event):
84+
#############################################################################
85+
# Simple picking, lines, rectangles and text
86+
# ------------------------------------------
87+
88+
fig, (ax1, ax2) = plt.subplots(2, 1)
89+
ax1.set_title('click on points, rectangles or text', picker=True)
90+
ax1.set_ylabel('ylabel', picker=True, bbox=dict(facecolor='red'))
91+
line, = ax1.plot(rand(100), 'o', picker=True, pickradius=5)
92+
93+
# Pick the rectangle.
94+
ax2.bar(range(10), rand(10), picker=True)
95+
for label in ax2.get_xticklabels(): # Make the xtick labels pickable.
96+
label.set_picker(True)
97+
98+
99+
def onpick1(event):
100+
if isinstance(event.artist, Line2D):
101+
thisline = event.artist
102+
xdata = thisline.get_xdata()
103+
ydata = thisline.get_ydata()
162104
ind = event.ind
163-
print('onpick3 scatter:', ind, x[ind], y[ind])
164-
165-
fig, ax = plt.subplots()
166-
ax.scatter(x, y, 100*s, c, picker=True)
167-
fig.canvas.mpl_connect('pick_event', onpick3)
168-
169-
170-
def pick_image():
171-
# picking images (matplotlib.image.AxesImage)
172-
fig, ax = plt.subplots()
173-
ax.imshow(rand(10, 5), extent=(1, 2, 1, 2), picker=True)
174-
ax.imshow(rand(5, 10), extent=(3, 4, 1, 2), picker=True)
175-
ax.imshow(rand(20, 25), extent=(1, 2, 3, 4), picker=True)
176-
ax.imshow(rand(30, 12), extent=(3, 4, 3, 4), picker=True)
177-
ax.set(xlim=(0, 5), ylim=(0, 5))
178-
179-
def onpick4(event):
180-
artist = event.artist
181-
if isinstance(artist, AxesImage):
182-
im = artist
183-
A = im.get_array()
184-
print('onpick4 image', A.shape)
185-
186-
fig.canvas.mpl_connect('pick_event', onpick4)
187-
188-
189-
if __name__ == '__main__':
190-
pick_simple()
191-
pick_custom_hit()
192-
pick_scatter_plot()
193-
pick_image()
194-
plt.show()
105+
print('onpick1 line:', np.column_stack([xdata[ind], ydata[ind]]))
106+
elif isinstance(event.artist, Rectangle):
107+
patch = event.artist
108+
print('onpick1 patch:', patch.get_path())
109+
elif isinstance(event.artist, Text):
110+
text = event.artist
111+
print('onpick1 text:', text.get_text())
112+
113+
114+
fig.canvas.mpl_connect('pick_event', onpick1)
115+
116+
117+
#############################################################################
118+
# Picking with a custom hit test function
119+
# ---------------------------------------
120+
# You can define custom pickers by setting picker to a callable function. The
121+
# function has the signature::
122+
#
123+
# hit, props = func(artist, mouseevent)
124+
#
125+
# to determine the hit test. If the mouse event is over the artist, return
126+
# ``hit=True`` and ``props`` is a dictionary of properties you want added to
127+
# the `.PickEvent` attributes.
128+
129+
def line_picker(line, mouseevent):
130+
"""
131+
Find the points within a certain distance from the mouseclick in
132+
data coords and attach some extra attributes, pickx and picky
133+
which are the data points that were picked.
134+
"""
135+
if mouseevent.xdata is None:
136+
return False, dict()
137+
xdata = line.get_xdata()
138+
ydata = line.get_ydata()
139+
maxd = 0.05
140+
d = np.sqrt(
141+
(xdata - mouseevent.xdata)**2 + (ydata - mouseevent.ydata)**2)
142+
143+
ind, = np.nonzero(d <= maxd)
144+
if len(ind):
145+
pickx = xdata[ind]
146+
picky = ydata[ind]
147+
props = dict(ind=ind, pickx=pickx, picky=picky)
148+
return True, props
149+
else:
150+
return False, dict()
151+
152+
153+
def onpick2(event):
154+
print('onpick2 line:', event.pickx, event.picky)
155+
156+
157+
fig, ax = plt.subplots()
158+
ax.set_title('custom picker for line data')
159+
line, = ax.plot(rand(100), rand(100), 'o', picker=line_picker)
160+
fig.canvas.mpl_connect('pick_event', onpick2)
161+
162+
163+
#############################################################################
164+
# Picking on a scatter plot
165+
# -------------------------
166+
# A scatter plot is backed by a `~matplotlib.collections.PathCollection`.
167+
168+
x, y, c, s = rand(4, 100)
169+
170+
171+
def onpick3(event):
172+
ind = event.ind
173+
print('onpick3 scatter:', ind, x[ind], y[ind])
174+
175+
176+
fig, ax = plt.subplots()
177+
ax.scatter(x, y, 100*s, c, picker=True)
178+
fig.canvas.mpl_connect('pick_event', onpick3)
179+
180+
181+
#############################################################################
182+
# Picking images
183+
# --------------
184+
# Images plotted using `.Axes.imshow` are `~matplotlib.image.AxesImage`
185+
# objects.
186+
187+
fig, ax = plt.subplots()
188+
ax.imshow(rand(10, 5), extent=(1, 2, 1, 2), picker=True)
189+
ax.imshow(rand(5, 10), extent=(3, 4, 1, 2), picker=True)
190+
ax.imshow(rand(20, 25), extent=(1, 2, 3, 4), picker=True)
191+
ax.imshow(rand(30, 12), extent=(3, 4, 3, 4), picker=True)
192+
ax.set(xlim=(0, 5), ylim=(0, 5))
193+
194+
195+
def onpick4(event):
196+
artist = event.artist
197+
if isinstance(artist, AxesImage):
198+
im = artist
199+
A = im.get_array()
200+
print('onpick4 image', A.shape)
201+
202+
203+
fig.canvas.mpl_connect('pick_event', onpick4)
204+
205+
plt.show()

0 commit comments

Comments
 (0)
0