1- #!/usr/bin/env python
21# -*- noplot -*-
32
43"""
5-
64This example shows how to use matplotlib to provide a data cursor. It
75uses matplotlib to draw the cursor and may be a slow since this
86requires redrawing the figure with every mouse move.
97
108Faster 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
1213"""
1314from __future__ import print_function
14- from pylab import *
15+ import matplotlib .pyplot as plt
16+ import numpy as np
1517
1618
1719class Cursor (object ):
@@ -33,7 +35,7 @@ def mouse_move(self, event):
3335 self .ly .set_xdata (x )
3436
3537 self .txt .set_text ('x=%1.2f, y=%1.2f' % (x , y ))
36- draw ()
38+ plt . draw ()
3739
3840
3941class SnaptoCursor (object ):
@@ -58,7 +60,7 @@ def mouse_move(self, event):
5860
5961 x , y = event .xdata , event .ydata
6062
61- indx = searchsorted (self .x , [x ])[0 ]
63+ indx = np . searchsorted (self .x , [x ])[0 ]
6264 x = self .x [indx ]
6365 y = self .y [indx ]
6466 # update the line positions
@@ -67,16 +69,16 @@ def mouse_move(self, event):
6769
6870 self .txt .set_text ('x=%1.2f, y=%1.2f' % (x , y ))
6971 print ('x=%1.2f, y=%1.2f' % (x , y ))
70- draw ()
72+ plt . draw ()
7173
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 ()
7577
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 )
7981
8082ax .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