1
- #!/usr/bin/env python
2
1
# -*- noplot -*-
3
2
4
3
"""
5
-
6
4
This example shows how to use matplotlib to provide a data cursor. It
7
5
uses matplotlib to draw the cursor and may be a slow since this
8
6
requires redrawing the figure with every mouse move.
9
7
10
8
Faster cursoring is possible using native GUI drawing, as in
11
- wxcursor_demo.py
9
+ wxcursor_demo.py.
10
+
11
+ Also, mpldatacursor can be used to achieve a similar effect. See webpage:
12
+ https://github.com/joferkington/mpldatacursor
12
13
"""
13
14
from __future__ import print_function
14
- from pylab import *
15
+ import matplotlib .pyplot as plt
16
+ import numpy as np
15
17
16
18
17
19
class Cursor (object ):
@@ -33,7 +35,7 @@ def mouse_move(self, event):
33
35
self .ly .set_xdata (x )
34
36
35
37
self .txt .set_text ('x=%1.2f, y=%1.2f' % (x , y ))
36
- draw ()
38
+ plt . draw ()
37
39
38
40
39
41
class SnaptoCursor (object ):
@@ -58,7 +60,7 @@ def mouse_move(self, event):
58
60
59
61
x , y = event .xdata , event .ydata
60
62
61
- indx = searchsorted (self .x , [x ])[0 ]
63
+ indx = np . searchsorted (self .x , [x ])[0 ]
62
64
x = self .x [indx ]
63
65
y = self .y [indx ]
64
66
# update the line positions
@@ -67,16 +69,16 @@ def mouse_move(self, event):
67
69
68
70
self .txt .set_text ('x=%1.2f, y=%1.2f' % (x , y ))
69
71
print ('x=%1.2f, y=%1.2f' % (x , y ))
70
- draw ()
72
+ plt . draw ()
71
73
72
- t = arange (0.0 , 1.0 , 0.01 )
73
- s = sin (2 * 2 * pi * t )
74
- fig , ax = subplots ()
74
+ t = np . arange (0.0 , 1.0 , 0.01 )
75
+ s = np . sin (2 * 2 * np . pi * t )
76
+ fig , ax = plt . subplots ()
75
77
76
- cursor = Cursor (ax )
77
- # cursor = SnaptoCursor(ax, t, s)
78
- connect ('motion_notify_event' , cursor .mouse_move )
78
+ # cursor = Cursor(ax)
79
+ cursor = SnaptoCursor (ax , t , s )
80
+ plt . connect ('motion_notify_event' , cursor .mouse_move )
79
81
80
82
ax .plot (t , s , 'o' )
81
- axis ([0 , 1 , - 1 , 1 ])
82
- show ()
83
+ plt . axis ([0 , 1 , - 1 , 1 ])
84
+ plt . show ()
0 commit comments