Hi, I want to embed matplotlib in wxpython. But I met a problem.
In the following simplified code, I want to switch between sin(x) and cos(x) curve on clicking the button. However, my code refuse to draw sin(x). What is the point?
and other question, I want the gui shows both the curve and the button, but now only curve is displayed. I have to resize the window if I want to see the button. Any hints?
thanks
Lee
from numpy import arange, sin, cos, pi
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
#~ from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure
import wx
class CanvasPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.btn=wx.Button(self, -1, 'sin(x)')
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
self.axes.hold(False)
self.canvas = FigureCanvas(self, -1, self.figure)
self.sizer = wx.FlexGridSizer(1, 2, 0, 0)
self.sizer.Add(self.canvas, 0, wx.LEFT | wx.TOP | wx.GROW)
self.sizer.Add(self.btn, 1, wx.LEFT | wx.TOP)
self.sizer.AddGrowableRow(0)
self.sizer.AddGrowableCol(0)
self.sizer.AddGrowableCol(1)
self.SetSizer(self.sizer)
self.Fit()
self.Bind(wx.EVT_BUTTON, self.evtBtn, self.btn)
def evtBtn(self, evt):
x=arange(0, pi*2, 0.1)
s, c=sin(x), cos(x)
if self.btn.GetLabel()=='sin(x)':
self.btn.SetLabel('cos(x)')
self.axes.plot(x, c)
else:
self.btn.SetLabel('sin(x)')
self.axes.plot(x, s)
if __name__ == "__main__":
app = wx.PySimpleApp()
fr = wx.Frame(None, title='test')
panel = CanvasPanel(fr)
panel.evtBtn(None)
fr.Show()
app.MainLoop()