Skip to content

Commit 18510ce

Browse files
committed
fixed cursor demos
svn path=/trunk/matplotlib/; revision=498
1 parent aa04e91 commit 18510ce

File tree

5 files changed

+66
-71
lines changed

5 files changed

+66
-71
lines changed

INSTALL

+6-7
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,13 @@ PACKAGE MANAGERS: (rpms, apt, fink)
123123
packages at http://mentors.debian.net
124124

125125

126-
* add these lines to your /etc/apt/sources.list:
126+
* add this lines to your /etc/apt/sources.list:
127+
deb http://anakonda.altervista.org/debian packages/
128+
deb-src http://anakonda.altervista.org/debian sources/
127129

128-
deb http://mentors.debian.net/debian unstable main contrib non-free
129-
deb-src http://mentors.debian.net/debian unstable main contrib non-free
130-
131-
* then run:
132-
# apt-get update
133-
# apt-get install python-matplotlib python-matplotlib-doc
130+
* then run:
131+
# apt-get update
132+
# apt-get install python-matplotlib python-matplotlib-doc
134133

135134
OS X
136135

TODO

+3-1
Original file line numberDiff line numberDiff line change
@@ -539,4 +539,6 @@ ZeroDivisionError: SeparableTransformation::eval_scalars yin interval is zero; c
539539
-- user events and nav event clashing on tkagg coords_demo/win32
540540

541541
-- check signal connect and disconnect in interactive use with
542-
multiple figure creation / deletion etc.
542+
multiple figure creation / deletion etc.
543+
544+
-- add xlim, ylim

examples/cursor_demo.py

+41-47
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
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+
"""
311
from 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

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

8074
t = arange(0.0, 1.0, 0.01)
8175
s = sin(2*2*pi*t)
8276
ax = subplot(111)
8377

8478
canvas = 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

8983
ax.plot(t, s, 'o')
9084
axis([0,1,-1,1])

examples/pythonic_matplotlib.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
fig = figure(1)
5454

55-
ax1 = subplot(211)
55+
ax1 = fig.add_subplot(211)
5656
ax1.plot(t, sin(2*pi*t))
5757
ax1.grid(True)
5858
ax1.set_ylim( (-2,2) )
@@ -63,7 +63,7 @@
6363
label.set_color('r')
6464

6565

66-
ax2 = subplot(212)
66+
ax2 = fig.add_subplot(212)
6767
ax2.plot(t, sin(2*2*pi*t))
6868
ax2.grid(True)
6969
ax2.set_ylim( (-2,2) )

examples/wxcursor_demo.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ def __init__(self):
4444

4545
self.add_toolbar() # comment this out for no toolbar
4646

47+
EVT_PAINT(self, self.OnPaint)
48+
4749

4850
def mouse_move(self, event):
4951
self.draw_cursor(event)
@@ -58,22 +60,19 @@ def add_toolbar(self):
5860
# update the axes menu on the toolbar
5961
self.toolbar.update()
6062

61-
6263
def OnPaint(self, event):
64+
self.erase_cursor()
6365
try: del self.lastInfo
6466
except AttributeError: pass
6567
self.canvas.draw()
68+
event.Skip()
6669

6770
def draw_cursor(self, event):
6871
'event is a MplEvent. Draw a cursor over the axes'
69-
7072
if event.inaxes is None:
71-
try: lastline1, lastline2, lastax, lastdc = self.lastInfo
73+
self.erase_cursor()
74+
try: del self.lastInfo
7275
except AttributeError: pass
73-
else:
74-
lastdc.DrawLine(*lastline1) # erase old
75-
lastdc.DrawLine(*lastline2) # erase old
76-
del self.lastInfo
7776
return
7877
canvas = self.canvas
7978
figheight = canvas.figure.bbox.height()
@@ -97,12 +96,7 @@ def draw_cursor(self, event):
9796

9897
x, y, left, right, bottom, top = [int(val) for val in x, y, left, right, bottom, top]
9998

100-
try: lastline1, lastline2, lastax, lastdc = self.lastInfo
101-
except AttributeError: pass
102-
else:
103-
lastdc.DrawLine(*lastline1) # erase old
104-
lastdc.DrawLine(*lastline2) # erase old
105-
99+
self.erase_cursor()
106100
line1 = (x, bottom, x, top)
107101
line2 = (left, y, right, y)
108102
self.lastInfo = line1, line2, ax, dc
@@ -113,7 +107,13 @@ def draw_cursor(self, event):
113107
time, price = event.xdata, event.ydata
114108
self.statusBar.SetStatusText("Time=%f Price=%f"% (time, price), 0)
115109

116-
110+
def erase_cursor(self):
111+
try: lastline1, lastline2, lastax, lastdc = self.lastInfo
112+
except AttributeError: pass
113+
else:
114+
lastdc.DrawLine(*lastline1) # erase old
115+
lastdc.DrawLine(*lastline2) # erase old
116+
117117
class App(wxApp):
118118

119119
def OnInit(self):

0 commit comments

Comments
 (0)