Menu

[r5829]: / trunk / users_guide / code / pick_demo2.py  Maximize  Restore  History

Download this file

34 lines (28 with data), 1.0 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from pylab import figure, show, nx
def line_picker(line, mouseevent):
"""
find the points within a certain distance from the mouseclick in
data coords and attach some extra attributes, pickx and picky
which are the data points that were picked
"""
if mouseevent.xdata is None: return False, dict()
xdata = line.get_xdata()
ydata = line.get_ydata()
maxd = 0.05
d = nx.sqrt((xdata-mouseevent.xdata)**2. + (ydata-mouseevent.ydata)**2.)
ind = nx.nonzero(nx.less_equal(d, maxd))
if len(ind):
pickx = nx.take(xdata, ind)
picky = nx.take(ydata, ind)
props = dict(ind=ind, pickx=pickx, picky=picky)
return True, props
else:
return False, dict()
def onpick2(event):
print 'onpick2 line:', event.pickx, event.picky
fig = figure()
ax1 = fig.add_subplot(111)
ax1.set_title('custom picker for line data')
line, = ax1.plot(nx.mlab.rand(100), nx.mlab.rand(100), 'o', picker=line_picker)
fig.canvas.mpl_connect('pick_event', onpick2)
show()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.