11#!/usr/bin/env python
2+ """
23
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+ """
311from matplotlib .matlab import *
412
513
@@ -13,27 +21,19 @@ def __init__(self, canvas, ax):
1321 # text location in axes coords
1422 self .txt = ax .text ( 0.7 , 0.9 , '' , transform = ax .transAxes )
1523
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 ()
2429
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 ) )
3034
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 ()
3737
3838
3939class SnaptoCursor :
@@ -51,40 +51,34 @@ def __init__(self, canvas, ax, x, y):
5151 # text location in axes coords
5252 self .txt = ax .text ( 0.7 , 0.9 , '' , transform = ax .transAxes )
5353
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 ()
7973
8074t = arange (0.0 , 1.0 , 0.01 )
8175s = sin (2 * 2 * pi * t )
8276ax = subplot (111 )
8377
8478canvas = 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 )
8882
8983ax .plot (t , s , 'o' )
9084axis ([0 ,1 ,- 1 ,1 ])
0 commit comments