1
1
#!/usr/bin/env python
2
+ """
2
3
4
+ This example shows how to use matplotlib to provide a data cursor. It
5
+ uses matplotlib to draw the cursor and may be a slow since this
6
+ requires redrawing the figure with every mouse move.
7
+
8
+ Faster cursoring is possible using native GUI drawing, as in
9
+ wxcursor_demo.py
10
+ """
3
11
from matplotlib .matlab import *
4
12
5
13
@@ -13,27 +21,19 @@ def __init__(self, canvas, ax):
13
21
# text location in axes coords
14
22
self .txt = ax .text ( 0.7 , 0.9 , '' , transform = ax .transAxes )
15
23
16
- def mouse_move (self , widget , event ):
17
- height = self .ax .figure .bbox .height ()
18
- x , y = event .x , height - event .y
19
-
20
- if self .ax .in_axes (x , y ):
21
- # transData transforms data coords to display coords. Use
22
- # the inverse method to transform back to data coords then
23
- # update the line
24
+ def mouse_move (self , event ):
25
+ if not event .inaxes : return
26
+ ax = event .inaxes
27
+ minx , maxx = ax .get_xlim ()
28
+ miny , maxy = ax .get_ylim ()
24
29
25
- # the cursor position
26
- x , y = ax .transData .inverse_xy_tup ( (x ,y ) )
27
- # the view limits
28
- minx , maxx = ax .viewLim .intervalx ().get_bounds ()
29
- miny , maxy = ax .viewLim .intervaly ().get_bounds ()
30
+ x , y = event .xdata , event .ydata
31
+ # update the line positions
32
+ self .lx .set_data ( (minx , maxx ), (y , y ) )
33
+ self .ly .set_data ( (x , x ), (miny , maxy ) )
30
34
31
- # update the line positions
32
- self .lx .set_data ( (minx , maxx ), (y , y ) )
33
- self .ly .set_data ( (x , x ), (miny , maxy ) )
34
-
35
- self .txt .set_text ( 'x=%1.2f, y=%1.2f' % (x ,y ) )
36
- self .canvas .draw ()
35
+ self .txt .set_text ( 'x=%1.2f, y=%1.2f' % (x ,y ) )
36
+ self .canvas .draw ()
37
37
38
38
39
39
class SnaptoCursor :
@@ -51,40 +51,34 @@ def __init__(self, canvas, ax, x, y):
51
51
# text location in axes coords
52
52
self .txt = ax .text ( 0.7 , 0.9 , '' , transform = ax .transAxes )
53
53
54
- def mouse_move (self , widget , event ):
55
- height = self .ax .figure .bbox .height ()
56
- x , y = event .x , height - event .y
57
-
58
- if self .ax .in_axes (x , y ):
59
- # transData transforms data coords to display coords. Use
60
- # the inverse method to transform back to data coords then
61
- # update the line
62
-
63
- # the cursor position
64
- x , y = ax .transData .inverse_xy_tup ( (x ,y ) )
65
- # the view limits
66
- minx , maxx = ax .viewLim .intervalx ().get_bounds ()
67
- miny , maxy = ax .viewLim .intervaly ().get_bounds ()
68
-
69
- indx = searchsorted (self .x , [x ])[0 ]
70
- x = self .x [indx ]
71
- y = self .y [indx ]
72
- # update the line positions
73
- self .lx .set_data ( (minx , maxx ), (y , y ) )
74
- self .ly .set_data ( (x , x ), (miny , maxy ) )
75
-
76
- self .txt .set_text ( 'x=%1.2f, y=%1.2f' % (x ,y ) )
77
- print 'x=%1.2f, y=%1.2f' % (x ,y )
78
- self .canvas .draw ()
54
+ def mouse_move (self , event ):
55
+
56
+ if not event .inaxes : return
57
+ ax = event .inaxes
58
+ minx , maxx = ax .get_xlim ()
59
+ miny , maxy = ax .get_ylim ()
60
+
61
+ x , y = event .xdata , event .ydata
62
+
63
+ indx = searchsorted (self .x , [x ])[0 ]
64
+ x = self .x [indx ]
65
+ y = self .y [indx ]
66
+ # update the line positions
67
+ self .lx .set_data ( (minx , maxx ), (y , y ) )
68
+ self .ly .set_data ( (x , x ), (miny , maxy ) )
69
+
70
+ self .txt .set_text ( 'x=%1.2f, y=%1.2f' % (x ,y ) )
71
+ print 'x=%1.2f, y=%1.2f' % (x ,y )
72
+ self .canvas .draw ()
79
73
80
74
t = arange (0.0 , 1.0 , 0.01 )
81
75
s = sin (2 * 2 * pi * t )
82
76
ax = subplot (111 )
83
77
84
78
canvas = get_current_fig_manager ().canvas
85
- # cursor = Cursor(canvas, ax)
86
- cursor = SnaptoCursor (canvas , ax , t , s )
87
- canvas .connect ('motion_notify_event' , cursor .mouse_move )
79
+ cursor = Cursor (canvas , ax )
80
+ # cursor = SnaptoCursor(canvas, ax, t, s)
81
+ canvas .mpl_connect ('motion_notify_event' , cursor .mouse_move )
88
82
89
83
ax .plot (t , s , 'o' )
90
84
axis ([0 ,1 ,- 1 ,1 ])
0 commit comments