|
8 | 8 |
|
9 | 9 | """ |
10 | 10 | from matplotlib.widgets import RectangleSelector |
11 | | -from pylab import subplot, arange, plot, sin, cos, pi, show |
12 | | -def line_select_callback(event1, event2): |
13 | | - 'event1 and event2 are the press and release events' |
14 | | - x1, y1 = event1.xdata, event1.ydata |
15 | | - x2, y2 = event2.xdata, event2.ydata |
16 | | - print "(%3.2f, %3.2f) --> (%3.2f, %3.2f)"%(x1,y1,x2,y2) |
17 | | - print " The button you used were: ",event1.button, event2.button |
| 11 | +import numpy as np |
| 12 | +import matplotlib.pyplot as plt |
18 | 13 |
|
| 14 | +def line_select_callback(eclick, erelease): |
| 15 | + 'eclick and erelease are the press and release events' |
| 16 | + x1, y1 = eclick.xdata, eclick.ydata |
| 17 | + x2, y2 = erelease.xdata, erelease.ydata |
| 18 | + print "(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2) |
| 19 | + print " The button you used were: ", eclick.button, erelease.button |
19 | 20 |
|
20 | | -current_ax=subplot(111) # make a new plotingrange |
21 | | -N=100000 # If N is large one can see improvement |
22 | | -x=10.0*arange(N)/(N-1) # by use blitting! |
| 21 | +def toggle_selector(event): |
| 22 | + print ' Key pressed.' |
| 23 | + if event.key in ['Q', 'q'] and toggle_selector.RS.active: |
| 24 | + print ' RectangleSelector deactivated.' |
| 25 | + toggle_selector.RS.set_active(False) |
| 26 | + if event.key in ['A', 'a'] and not toggle_selector.RS.active: |
| 27 | + print ' RectangleSelector activated.' |
| 28 | + toggle_selector.RS.set_active(True) |
23 | 29 |
|
24 | | -plot(x,sin(.2*pi*x),lw=3,c='b',alpha=.7) # plot something |
25 | | -plot(x,cos(.2*pi*x),lw=3.5,c='r',alpha=.5) |
26 | | -plot(x,-sin(.2*pi*x),lw=3.5,c='g',alpha=.3) |
| 30 | + |
| 31 | +current_ax = plt.subplot(111) # make a new plotingrange |
| 32 | +N = 100000 # If N is large one can see |
| 33 | +x = np.linspace(0.0, 10.0, N) # improvement by use blitting! |
| 34 | + |
| 35 | +plt.plot(x, +np.sin(.2*np.pi*x), lw=3.5, c='b', alpha=.7) # plot something |
| 36 | +plt.plot(x, +np.cos(.2*np.pi*x), lw=3.5, c='r', alpha=.5) |
| 37 | +plt.plot(x, -np.sin(.2*np.pi*x), lw=3.5, c='g', alpha=.3) |
27 | 38 |
|
28 | 39 | print "\n click --> release" |
29 | 40 |
|
30 | 41 | # drawtype is 'box' or 'line' or 'none' |
31 | | -LS = RectangleSelector(current_ax, line_select_callback, |
32 | | - drawtype='box',useblit=True, |
33 | | - button = [1, 3], # don't use center mouse button |
34 | | - minspanx=5,minspany=5,spancoords='pixels') |
35 | | -show() |
| 42 | +toggle_selector.RS = RectangleSelector(current_ax, line_select_callback, |
| 43 | + drawtype='box', useblit=True, |
| 44 | + button=[1,3], # don't use middle button |
| 45 | + minspanx=5, minspany=5, |
| 46 | + spancoords='pixels') |
| 47 | +plt.connect('key_press_event', toggle_selector) |
| 48 | +plt.show() |
0 commit comments