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

Skip to content

Commit be71fa5

Browse files
committed
added wx cursor demo
svn path=/trunk/matplotlib/; revision=482
1 parent 38461c2 commit be71fa5

2 files changed

Lines changed: 148 additions & 0 deletions

File tree

MANIFEST

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ examples/logo.py
242242
examples/major_minor_demo1.py
243243
examples/major_minor_demo2.py
244244
examples/mathtext_demo.py
245+
examples/matplotlib_icon.py
245246
examples/mpl_with_glade.glade
246247
examples/mpl_with_glade.py
247248
examples/mri_demo.py
@@ -266,13 +267,15 @@ examples/stock_demo.py
266267
examples/subplot_demo.py
267268
examples/system_monitor.py
268269
examples/table_demo.py
270+
examples/test.py
269271
examples/text_handles.py
270272
examples/text_themes.py
271273
examples/to_numeric.py
272274
examples/two_dymamic_images.py
273275
examples/two_scales.py
274276
examples/vertical_ticklabels.py
275277
examples/vline_demo.py
278+
examples/wxcursor_demo.py
276279
examples/data/AAPL.dat
277280
examples/data/INTC.dat
278281
examples/data/ct.raw
@@ -348,16 +351,31 @@ fonts/ttf/cmr10.ttf
348351
fonts/ttf/cmsy10.ttf
349352
fonts/ttf/cmtt10.ttf
350353
fonts/ttf/local.conf
354+
images/back.png
351355
images/back.ppm
356+
images/back.svg
352357
images/back.xpm
358+
images/filesave.png
353359
images/filesave.ppm
360+
images/filesave.svg
354361
images/filesave.xpm
362+
images/forward.png
355363
images/forward.ppm
364+
images/forward.svg
356365
images/forward.xpm
366+
images/hand.png
357367
images/hand.ppm
368+
images/hand.svg
358369
images/hand.xpm
370+
images/home.png
359371
images/home.ppm
372+
images/home.svg
360373
images/home.xpm
374+
images/matplotlib.svg
375+
images/move.png
376+
images/move.ppm
377+
images/move.svg
378+
images/move.xpm
361379
images/stock_close.ppm
362380
images/stock_close.xpm
363381
images/stock_down.ppm
@@ -376,7 +394,9 @@ images/stock_zoom-in.ppm
376394
images/stock_zoom-in.xpm
377395
images/stock_zoom-out.ppm
378396
images/stock_zoom-out.xpm
397+
images/zoom_to_rect.png
379398
images/zoom_to_rect.ppm
399+
images/zoom_to_rect.svg
380400
images/zoom_to_rect.xpm
381401
license/LICENSE
382402
license/LICENSE_PAINT

examples/wxcursor_demo.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!/usr/bin/env python
2+
"""
3+
Show how to have wx draw a cursor over an axes that moves with the
4+
mouse and reports the data coords
5+
"""
6+
7+
from matplotlib.numerix import arange, sin, pi
8+
9+
import matplotlib
10+
matplotlib.use('WXAgg')
11+
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
12+
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
13+
14+
from matplotlib.figure import Figure
15+
16+
from wxPython.wx import *
17+
18+
class CanvasFrame(wxFrame):
19+
20+
def __init__(self):
21+
wxFrame.__init__(self,None,-1,
22+
'CanvasFrame',size=(550,350))
23+
24+
self.SetBackgroundColour(wxNamedColor("WHITE"))
25+
26+
self.figure = Figure()
27+
self.axes = self.figure.add_subplot(111)
28+
t = arange(0.0,3.0,0.01)
29+
s = sin(2*pi*t)
30+
31+
self.axes.plot(t,s)
32+
self.axes.set_xlabel('Time (s)')
33+
self.axes.set_ylabel('Price ($)')
34+
self.canvas = FigureCanvas(self, -1, self.figure)
35+
self.canvas.mpl_connect('motion_notify_event', self.mouse_move)
36+
self.sizer = wxBoxSizer(wxVERTICAL)
37+
self.sizer.Add(self.canvas, 1, wxLEFT | wxTOP | wxGROW)
38+
self.SetSizer(self.sizer)
39+
self.Fit()
40+
41+
self.statusBar = wxStatusBar(self, -1)
42+
self.statusBar.SetFieldsCount(1)
43+
self.SetStatusBar(self.statusBar)
44+
45+
self.add_toolbar() # comment this out for no toolbar
46+
47+
48+
def mouse_move(self, event):
49+
self.draw_cursor(event)
50+
51+
def add_toolbar(self):
52+
self.toolbar = NavigationToolbar2Wx(self.canvas)
53+
self.toolbar.Realize()
54+
tw, th = self.toolbar.GetSizeTuple()
55+
fw, fh = self.canvas.GetSizeTuple()
56+
self.toolbar.SetSize(wxSize(fw, th))
57+
self.sizer.Add(self.toolbar, 0, wxLEFT | wxEXPAND)
58+
# update the axes menu on the toolbar
59+
self.toolbar.update()
60+
61+
62+
def OnPaint(self, event):
63+
try: del self.lastInfo
64+
except AttributeError: pass
65+
self.canvas.draw()
66+
67+
def draw_cursor(self, event):
68+
'event is a MplEvent. Draw a cursor over the axes'
69+
70+
if event.inaxes is None:
71+
try: lastline1, lastline2, lastax, lastdc = self.lastInfo
72+
except AttributeError: pass
73+
else:
74+
lastdc.DrawLine(*lastline1) # erase old
75+
lastdc.DrawLine(*lastline2) # erase old
76+
del self.lastInfo
77+
return
78+
canvas = self.canvas
79+
figheight = canvas.figure.bbox.height()
80+
ax = event.inaxes
81+
left,bottom,width,height = ax.bbox.get_bounds()
82+
bottom = figheight-bottom
83+
top = bottom - height
84+
right = left + width
85+
x, y = event.x, event.y
86+
y = figheight-y
87+
88+
dc = wxClientDC(canvas)
89+
dc.SetLogicalFunction(wxXOR)
90+
wbrush = wxBrush(wxColour(255,255,255), wxTRANSPARENT)
91+
wpen = wxPen(wxColour(200, 200, 200), 1, wxSOLID)
92+
dc.SetBrush(wbrush)
93+
dc.SetPen(wpen)
94+
95+
dc.ResetBoundingBox()
96+
dc.BeginDrawing()
97+
98+
x, y, left, right, bottom, top = [int(val) for val in x, y, left, right, bottom, top]
99+
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+
106+
line1 = (x, bottom, x, top)
107+
line2 = (left, y, right, y)
108+
self.lastInfo = line1, line2, ax, dc
109+
dc.DrawLine(*line1) # draw new
110+
dc.DrawLine(*line2) # draw new
111+
dc.EndDrawing()
112+
113+
time, price = event.xdata, event.ydata
114+
self.statusBar.SetStatusText("Time=%f Price=%f"% (time, price), 0)
115+
116+
117+
class App(wxApp):
118+
119+
def OnInit(self):
120+
'Create the main window and insert the custom frame'
121+
frame = CanvasFrame()
122+
frame.Show(true)
123+
124+
return true
125+
126+
app = App(0)
127+
app.MainLoop()
128+

0 commit comments

Comments
 (0)