|
1 | 1 | #!/usr/bin/env python |
2 | 2 | """ |
3 | | -The SpanSelector is a mouse widget to select a vmin/vmax |
4 | | -range. When you left click drag in the axes, a rectangle shows the |
5 | | -selected region. When you release, the rectangle disappears and a |
6 | | -callback is called with min/max. |
| 3 | +The SpanSelector is a mouse widget to select a xmin/xmax range and plot the |
| 4 | +detail view of the selected region in the lower axes |
7 | 5 | """ |
8 | | -import pylab |
| 6 | +import numpy as npy |
| 7 | +from pylab import figure, show |
9 | 8 | from matplotlib.widgets import SpanSelector |
10 | 9 |
|
11 | | -fig = pylab.figure(figsize=(8,6)) |
| 10 | +fig = figure(figsize=(8,6)) |
12 | 11 | ax = fig.add_subplot(211, axisbg='#FFFFCC') |
13 | 12 |
|
14 | | -x,y = 4*(pylab.rand(2,100)-.5) |
15 | | -ax.plot(x,y,'o') |
16 | | -ax.set_xlim(-2,2) |
| 13 | +x = npy.arange(0.0, 5.0, 0.01) |
| 14 | +y = npy.sin(2*npy.pi*x) + 0.5*npy.random.randn(len(x)) |
| 15 | + |
| 16 | +ax.plot(x, y, '-') |
17 | 17 | ax.set_ylim(-2,2) |
18 | 18 | ax.set_title('Press left mouse button and drag to test') |
19 | 19 |
|
| 20 | +ax2 = fig.add_subplot(212, axisbg='#FFFFCC') |
| 21 | +line2, = ax2.plot(x, y, '-') |
| 22 | + |
20 | 23 |
|
21 | | -def onselect(vmin, vmax): |
22 | | - print vmin, vmax |
| 24 | +def onselect(xmin, xmax): |
| 25 | + indmin, indmax = npy.searchsorted(x, (xmin, xmax)) |
| 26 | + indmax = min(len(x)-1, indmax) |
| 27 | + |
| 28 | + thisx = x[indmin:indmax] |
| 29 | + thisy = y[indmin:indmax] |
| 30 | + line2.set_data(thisx, thisy) |
| 31 | + ax2.set_xlim(thisx[0], thisx[-1]) |
| 32 | + ax2.set_ylim(thisy.min(), thisy.max()) |
| 33 | + fig.canvas.draw() |
23 | 34 |
|
24 | 35 | # set useblit True on gtkagg for enhanced performance |
25 | 36 | span = SpanSelector(ax, onselect, 'horizontal', useblit=False, |
26 | 37 | rectprops=dict(alpha=0.5, facecolor='red') ) |
27 | 38 |
|
28 | | -ax2 = fig.add_subplot(212) |
29 | | -ax2.plot([1,2,3]) |
30 | | - |
31 | | -span2 = SpanSelector(ax2, onselect, 'vertical') |
32 | 39 |
|
33 | | -pylab.show() |
| 40 | +show() |
0 commit comments