3
3
Cursor Demo
4
4
===========
5
5
6
- This example shows how to use matplotlib to provide a data cursor. It
7
- uses matplotlib to draw the cursor and may be a slow since this
8
- requires redrawing the figure with every mouse move.
6
+ This example shows how to use Matplotlib to provide a data cursor. It uses
7
+ Matplotlib to draw the cursor and may be a slow since this requires redrawing
8
+ the figure with every mouse move.
9
9
10
10
Faster cursoring is possible using native GUI drawing, as in
11
- wxcursor_demo.py .
11
+ :doc:`/gallery/user_interfaces/wxcursor_demo_sgskip` .
12
12
13
- The mpldatacursor and mplcursors third-party packages can be used to achieve a
14
- similar effect. See
13
+ The mpldatacursor__ and mplcursors__ third-party packages can be used to
14
+ achieve a similar effect.
15
15
16
- https://github.com/joferkington/mpldatacursor
17
- https://github.com/anntzer/mplcursors
16
+ __ https://github.com/joferkington/mpldatacursor
17
+ __ https://github.com/anntzer/mplcursors
18
18
"""
19
+
19
20
import matplotlib .pyplot as plt
20
21
import numpy as np
21
22
@@ -39,13 +40,13 @@ def mouse_move(self, event):
39
40
self .ly .set_xdata (x )
40
41
41
42
self .txt .set_text ('x=%1.2f, y=%1.2f' % (x , y ))
42
- plt .draw ()
43
+ self . ax . figure . canvas .draw ()
43
44
44
45
45
46
class SnaptoCursor (object ):
46
47
"""
47
- Like Cursor but the crosshair snaps to the nearest x,y point
48
- For simplicity, I'm assuming x is sorted
48
+ Like Cursor but the crosshair snaps to the nearest x, y point.
49
+ For simplicity, this assumes that *x* is sorted.
49
50
"""
50
51
51
52
def __init__ (self , ax , x , y ):
@@ -58,13 +59,11 @@ def __init__(self, ax, x, y):
58
59
self .txt = ax .text (0.7 , 0.9 , '' , transform = ax .transAxes )
59
60
60
61
def mouse_move (self , event ):
61
-
62
62
if not event .inaxes :
63
63
return
64
64
65
65
x , y = event .xdata , event .ydata
66
-
67
- indx = min (np .searchsorted (self .x , [x ])[0 ], len (self .x ) - 1 )
66
+ indx = min (np .searchsorted (self .x , x ), len (self .x ) - 1 )
68
67
x = self .x [indx ]
69
68
y = self .y [indx ]
70
69
# update the line positions
@@ -73,16 +72,20 @@ def mouse_move(self, event):
73
72
74
73
self .txt .set_text ('x=%1.2f, y=%1.2f' % (x , y ))
75
74
print ('x=%1.2f, y=%1.2f' % (x , y ))
76
- plt .draw ()
75
+ self .ax .figure .canvas .draw ()
76
+
77
77
78
78
t = np .arange (0.0 , 1.0 , 0.01 )
79
79
s = np .sin (2 * 2 * np .pi * t )
80
- fig , ax = plt .subplots ()
81
80
82
- # cursor = Cursor(ax)
83
- cursor = SnaptoCursor (ax , t , s )
84
- plt .connect ('motion_notify_event' , cursor .mouse_move )
81
+ fig , ax = plt .subplots ()
82
+ ax .plot (t , s , 'o' )
83
+ cursor = Cursor (ax )
84
+ fig .canvas .mpl_connect ('motion_notify_event' , cursor .mouse_move )
85
85
86
+ fig , ax = plt .subplots ()
86
87
ax .plot (t , s , 'o' )
87
- plt .axis ([0 , 1 , - 1 , 1 ])
88
+ snap_cursor = SnaptoCursor (ax , t , s )
89
+ fig .canvas .mpl_connect ('motion_notify_event' , snap_cursor .mouse_move )
90
+
88
91
plt .show ()
0 commit comments