|
| 1 | +from matplotlib.matlab import * |
| 2 | + |
| 3 | + |
| 4 | +class Cursor: |
| 5 | + def __init__(self, canvas, ax): |
| 6 | + self.canvas = canvas |
| 7 | + self.ax = ax |
| 8 | + self.lx, = ax.plot( (0,0), (0,0), 'k-' ) # the horiz line |
| 9 | + self.ly, = ax.plot( (0,0), (0,0), 'k-' ) # the vert line |
| 10 | + |
| 11 | + # text location in axes coords |
| 12 | + self.txt = ax.text( 0.7, 0.9, '', transform=ax.transAxes) |
| 13 | + |
| 14 | + def mouse_move(self, widget, event): |
| 15 | + height = self.ax.figure.bbox.height() |
| 16 | + x, y = event.x, height-event.y |
| 17 | + |
| 18 | + if self.ax.in_axes(x, y): |
| 19 | + # transData transforms data coords to display coords. Use |
| 20 | + # the inverse method to transform back to data coords then |
| 21 | + # update the line |
| 22 | + |
| 23 | + # the cursor position |
| 24 | + x, y = ax.transData.inverse_xy_tup( (x,y) ) |
| 25 | + # the view limits |
| 26 | + minx, maxx = ax.viewLim.intervalx().get_bounds() |
| 27 | + miny, maxy = ax.viewLim.intervaly().get_bounds() |
| 28 | + |
| 29 | + # update the line positions |
| 30 | + self.lx.set_data( (minx, maxx), (y, y) ) |
| 31 | + self.ly.set_data( (x, x), (miny, maxy) ) |
| 32 | + |
| 33 | + self.txt.set_text( 'x=%1.2f, y=%1.2f'%(x,y) ) |
| 34 | + self.canvas.draw() |
| 35 | + |
| 36 | + |
| 37 | +class SnaptoCursor: |
| 38 | + """ |
| 39 | + Like Cursor but the crosshair snaps to the nearest x,y point |
| 40 | + For simplicity, I'm assuming x is sorted |
| 41 | + """ |
| 42 | + def __init__(self, canvas, ax, x, y): |
| 43 | + self.canvas = canvas |
| 44 | + self.ax = ax |
| 45 | + self.lx, = ax.plot( (0,0), (0,0), 'k-' ) # the horiz line |
| 46 | + self.ly, = ax.plot( (0,0), (0,0), 'k-' ) # the vert line |
| 47 | + self.x = x |
| 48 | + self.y = y |
| 49 | + # text location in axes coords |
| 50 | + self.txt = ax.text( 0.7, 0.9, '', transform=ax.transAxes) |
| 51 | + |
| 52 | + def mouse_move(self, widget, event): |
| 53 | + height = self.ax.figure.bbox.height() |
| 54 | + x, y = event.x, height-event.y |
| 55 | + |
| 56 | + if self.ax.in_axes(x, y): |
| 57 | + # transData transforms data coords to display coords. Use |
| 58 | + # the inverse method to transform back to data coords then |
| 59 | + # update the line |
| 60 | + |
| 61 | + # the cursor position |
| 62 | + x, y = ax.transData.inverse_xy_tup( (x,y) ) |
| 63 | + # the view limits |
| 64 | + minx, maxx = ax.viewLim.intervalx().get_bounds() |
| 65 | + miny, maxy = ax.viewLim.intervaly().get_bounds() |
| 66 | + |
| 67 | + indx = searchsorted(self.x, [x])[0] |
| 68 | + x = self.x[indx] |
| 69 | + y = self.y[indx] |
| 70 | + # update the line positions |
| 71 | + self.lx.set_data( (minx, maxx), (y, y) ) |
| 72 | + self.ly.set_data( (x, x), (miny, maxy) ) |
| 73 | + |
| 74 | + self.txt.set_text( 'x=%1.2f, y=%1.2f'%(x,y) ) |
| 75 | + print 'x=%1.2f, y=%1.2f'%(x,y) |
| 76 | + self.canvas.draw() |
| 77 | + |
| 78 | +t = arange(0.0, 1.0, 0.01) |
| 79 | +s = sin(2*2*pi*t) |
| 80 | +ax = subplot(111) |
| 81 | + |
| 82 | +canvas = get_current_fig_manager().canvas |
| 83 | +#cursor = Cursor(canvas, ax) |
| 84 | +cursor = SnaptoCursor(canvas, ax, t, s) |
| 85 | +canvas.connect('motion_notify_event', cursor.mouse_move) |
| 86 | + |
| 87 | +ax.plot(t, s, 'o') |
| 88 | +axis([0,1,-1,1]) |
| 89 | +show() |
0 commit comments