Menu

[r7469]: / trunk / users_guide / code / pick_demo1.py  Maximize  Restore  History

Download this file

39 lines (32 with data), 1.3 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
34
35
36
37
38
from pylab import figure, show, nx
from matplotlib.lines import Line2D
from matplotlib.patches import Patch, Rectangle
from matplotlib.text import Text
fig = figure()
ax1 = fig.add_subplot(211)
ax1.set_title('click on points, rectangles or text', picker=True)
ax1.set_ylabel('ylabel', picker=True, bbox=dict(facecolor='red'))
line, = ax1.plot(nx.mlab.rand(100), 'o', picker=5) # 5 points tolerance
ax2 = fig.add_subplot(212)
# pick the bars
bars = ax2.bar(range(10), nx.mlab.rand(10), picker=True)
for label in ax2.get_xticklabels():
label.set_picker(True) # make the xtick labels pickable
# this function will be called when one of the picker Artists is
# clicked on
def onpick1(event):
if isinstance(event.artist, Line2D):
thisline = event.artist
xdata = thisline.get_xdata()
ydata = thisline.get_ydata()
ind = event.ind
print 'onpick1 line:', zip(nx.take(xdata, ind), nx.take(ydata, ind))
elif isinstance(event.artist, Rectangle):
patch = event.artist
print 'onpick1 patch:', patch.get_verts()
elif isinstance(event.artist, Text):
text = event.artist
print 'onpick1 text:', text.get_text()
# now register your function to get a callback on a pick event
fig.canvas.mpl_connect('pick_event', onpick1)
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.