Skip to content

Commit 94ca1a7

Browse files
committed
added another example
svn path=/trunk/matplotlib/; revision=3393
1 parent 9ed9709 commit 94ca1a7

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

examples/pick_event_demo3.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import numpy as npy
2+
from pylab import figure, show
3+
4+
5+
X = npy.random.rand(100, 200)
6+
xs = npy.mean(X, axis=1)
7+
ys = npy.std(X, axis=1)
8+
9+
fig = figure()
10+
ax = fig.add_subplot(211)
11+
ax.set_title('click on point to plot time series')
12+
line, = ax.plot(xs, ys, 'o', picker=5) # 5 points tolerance
13+
ax2 = fig.add_subplot(212)
14+
15+
class PointBrowser:
16+
"""
17+
Click on a point to select and highlight it -- the data that
18+
generated the point will be shown in the lower axes. Use the 'n'
19+
and 'p' keys to browse through the next and pervious points
20+
"""
21+
def __init__(self):
22+
self.lastind = 0
23+
24+
self.text = ax.text(0.05, 0.95, 'selected: none',
25+
transform=ax.transAxes, va='top')
26+
self.selected, = ax.plot([xs[0]], [ys[0]], 'o', ms=12, alpha=0.4,
27+
color='yellow', visible=False)
28+
29+
def onpress(self, event):
30+
if self.lastind is None: return
31+
if event.key not in ('n', 'p'): return
32+
if event.key=='n': inc = 1
33+
else: inc = -1
34+
35+
36+
self.lastind += inc
37+
self.lastind = npy.clip(self.lastind, 0, len(xs)-1)
38+
self.update()
39+
40+
def onpick(self, event):
41+
42+
if event.artist!=line: return True
43+
44+
N = len(event.ind)
45+
if not N: return True
46+
47+
# the click locations
48+
x = event.mouseevent.xdata
49+
y = event.mouseevent.ydata
50+
51+
52+
distances = npy.hypot(x-xs[event.ind], y-ys[event.ind])
53+
indmin = distances.argmin()
54+
dataind = event.ind[indmin]
55+
56+
self.lastind = dataind
57+
self.update()
58+
59+
def update(self):
60+
if self.lastind is None: return
61+
62+
dataind = self.lastind
63+
64+
ax2.cla()
65+
ax2.plot(X[dataind])
66+
67+
ax2.text(0.05, 0.9, 'mu=%1.3f\nsigma=%1.3f'%(xs[dataind], ys[dataind]),
68+
transform=ax2.transAxes, va='top')
69+
ax2.set_ylim(-0.5, 1.5)
70+
self.selected.set_visible(True)
71+
self.selected.set_data(xs[dataind], ys[dataind])
72+
73+
self.text.set_text('selected: %d'%dataind)
74+
fig.canvas.draw()
75+
76+
77+
browser = PointBrowser()
78+
79+
fig.canvas.mpl_connect('pick_event', browser.onpick)
80+
fig.canvas.mpl_connect('key_press_event', browser.onpress)
81+
82+
show()

examples/widgets/span_selector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def onselect(xmin, xmax):
3333
fig.canvas.draw()
3434

3535
# set useblit True on gtkagg for enhanced performance
36-
span = SpanSelector(ax, onselect, 'horizontal', useblit=False,
36+
span = SpanSelector(ax, onselect, 'horizontal', useblit=True,
3737
rectprops=dict(alpha=0.5, facecolor='red') )
3838

3939

0 commit comments

Comments
 (0)