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