Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96ed5c4

Browse files
committed
Merge pull request #5127 from ericmjl/mep12-cursor_demo.py
mep12 on cursor_demo.py
2 parents 620bdd8 + 0c5a303 commit 96ed5c4

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

examples/pylab_examples/cursor_demo.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
#!/usr/bin/env python
21
# -*- noplot -*-
32

43
"""
5-
64
This example shows how to use matplotlib to provide a data cursor. It
75
uses matplotlib to draw the cursor and may be a slow since this
86
requires redrawing the figure with every mouse move.
97
108
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
1213
"""
1314
from __future__ import print_function
14-
from pylab import *
15+
import matplotlib.pyplot as plt
16+
import numpy as np
1517

1618

1719
class 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

3941
class 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

8082
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

Comments
 (0)