Next topic

This Page

Quick search

"""Demonstrates how to convert mathtext to a wx.Bitmap for display in variouscontrols on wxPython."""importmatplotlibmatplotlib.use("WxAgg")fromnumpyimportarange,sin,pi,cos,logfrommatplotlib.backends.backend_wxaggimportFigureCanvasWxAggasFigureCanvasfrommatplotlib.backends.backend_wximportNavigationToolbar2Wxfrommatplotlib.figureimportFigureimportwxIS_GTK='wxGTK'inwx.PlatformInfoIS_WIN='wxMSW'inwx.PlatformInfoIS_MAC='wxMac'inwx.PlatformInfo############################################################# This is where the "magic" happens.frommatplotlib.mathtextimportMathTextParsermathtext_parser=MathTextParser("Bitmap")defmathtext_to_wxbitmap(s):ftimage,depth=mathtext_parser.parse(s,150)returnwx.BitmapFromBufferRGBA(ftimage.get_width(),ftimage.get_height(),ftimage.as_rgba_str())############################################################functions=[(r'$\sin(2 \pi x)$',lambdax:sin(2*pi*x)),(r'$\frac{4}{3}\pi x^3$',lambdax:(4.0/3.0)*pi*x**3),(r'$\cos(2 \pi x)$',lambdax:cos(2*pi*x)),(r'$\log(x)$',lambdax:log(x))]classCanvasFrame(wx.Frame):def__init__(self,parent,title):wx.Frame.__init__(self,parent,-1,title,size=(550,350))self.SetBackgroundColour(wx.NamedColour("WHITE"))self.figure=Figure()self.axes=self.figure.add_subplot(111)self.change_plot(0)self.canvas=FigureCanvas(self,-1,self.figure)self.sizer=wx.BoxSizer(wx.VERTICAL)self.add_buttonbar()self.sizer.Add(self.canvas,1,wx.LEFT|wx.TOP|wx.GROW)self.add_toolbar()# comment this out for no toolbarmenuBar=wx.MenuBar()# File Menumenu=wx.Menu()menu.Append(wx.ID_EXIT,"E&xit\tAlt-X","Exit this simple sample")menuBar.Append(menu,"&File")ifIS_GTKorIS_WIN:# Equation Menumenu=wx.Menu()fori,(mt,func)inenumerate(functions):bm=mathtext_to_wxbitmap(mt)item=wx.MenuItem(menu,1000+i," ")item.SetBitmap(bm)menu.AppendItem(item)self.Bind(wx.EVT_MENU,self.OnChangePlot,item)menuBar.Append(menu,"&Functions")self.SetMenuBar(menuBar)self.SetSizer(self.sizer)self.Fit()defadd_buttonbar(self):self.button_bar=wx.Panel(self)self.button_bar_sizer=wx.BoxSizer(wx.HORIZONTAL)self.sizer.Add(self.button_bar,0,wx.LEFT|wx.TOP|wx.GROW)fori,(mt,func)inenumerate(functions):bm=mathtext_to_wxbitmap(mt)button=wx.BitmapButton(self.button_bar,1000+i,bm)self.button_bar_sizer.Add(button,1,wx.GROW)self.Bind(wx.EVT_BUTTON,self.OnChangePlot,button)self.button_bar.SetSizer(self.button_bar_sizer)defadd_toolbar(self):"""Copied verbatim from embedding_wx2.py"""self.toolbar=NavigationToolbar2Wx(self.canvas)self.toolbar.Realize()ifIS_MAC:self.SetToolBar(self.toolbar)else:tw,th=self.toolbar.GetSizeTuple()fw,fh=self.canvas.GetSizeTuple()self.toolbar.SetSize(wx.Size(fw,th))self.sizer.Add(self.toolbar,0,wx.LEFT|wx.EXPAND)self.toolbar.update()defOnPaint(self,event):self.canvas.draw()defOnChangePlot(self,event):self.change_plot(event.GetId()-1000)defchange_plot(self,plot_number):t=arange(1.0,3.0,0.01)s=functions[plot_number][1](t)self.axes.clear()self.axes.plot(t,s)self.Refresh()classMyApp(wx.App):defOnInit(self):frame=CanvasFrame(None,"wxPython mathtext demo app")self.SetTopWindow(frame)frame.Show(True)returnTrueapp=MyApp()app.MainLoop()