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