pylab crashes simple wxPython script

Hello,

I want to write a wxPython script to pull up pylab plots (in a separate window), based on menu or button choices. The script below crashes with a segmentation fault. Am I doing something wrong here? Is there a workaround or fix?

      thanks,

        Brian Blais

#!/usr/bin/env python

from wxPython.wx import *
import pylab

class MyFrame(wxFrame):

     def __init__(self, parent, id, title):
         wxFrame.__init__(self, parent, -1, title)
         self.CenterOnScreen()

         menuBar = wxMenuBar()

         # 1st menu from left
         menu1 = wxMenu()
         menu1.Append(101, "&New")
         menu1.Append(102, "&Close", "Close this frame")
         menuBar.Append(menu1, "&File")
         EVT_MENU(self, 101, self.New)
         EVT_MENU(self, 102, self.Close)

         self.SetMenuBar(menuBar)
         self.Show(True)

     def New(self, event):
         pylab.plot([1,2,3,4],[5,6,7,8])
         pylab.show()

     def CloseWindow(self, event):
         self.Close()

class App(wxApp):
     def OnInit(self):
         frame = MyFrame(None, -1, "testing")
         self.SetTopWindow(frame)
         return True

def main():

     app = App(0)
     app.MainLoop()

if __name__ == "__main__":
     main()

···

--
-----------------

              bblais@...1129...
              http://web.bryant.edu/~bblais

Brian Blais ha scritto:

I want to write a wxPython script to pull up pylab plots (in a separate window), based on menu or button choices. The script below crashes with a segmentation fault. Am I doing something wrong here? Is there a workaround or fix?

As far as I know, mixing wxpython and pylab this way is BAD.

You should better choose between:
- (1)Launch pylab in a separate thread
- (2) (What I do) Using WxMPL and embedding a matplotlib plot in a
wxPanel or wxFrame. You have to use the OO interface to matplotlib.

m.

···

--
Massimo Sandal
University of Bologna
Department of Biochemistry "G.Moruzzi"

snail mail:
Via Irnerio 48, 40126 Bologna, Italy

email:
massimo.sandal@...898...

tel: +39-051-2094388
fax: +39-051-2094387

Hi,

Agree with massimo. One brief example might help:

import matplotlib
matplotlib.use('WXAgg')
from pylab import gca
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as
FigureCanvas
from matplotlib.font_manager import fontManager, FontProperties
from matplotlib.figure import Figure
from matplotlib.axes import Subplot
import matplotlib.numerix as numpy
from matplotlib.ticker import LogLocator, MultipleLocator
from matplotlib import rcParams

import wx

<snip>

class Your_Class(wx.Frame):
    def __init__(self,parent,id,title):
        wx.Frame.__init__(self,None,-1,"Your_Title")
  <snip>
  
  self.fig = Figure(figsize=(7,5),dpi=100)
        self.axes = self.fig.add_subplot(111)
        self.canvas = FigureCanvas(self, -1, self.fig)
        self.parent = self.canvas.GetParent()
        self.canvas.mpl_connect('motion_notify_event',self.mouse_move)

  <snip: SomeSizer>
  SomeSizer.add(self.canvas, 1, wx.LEFT|wx.TOP|wx.GROW|wx.EXPAND)

#useful method examples:
    def OnRefresh(self,event=None):
        """
            refreshes the plotting window
        """
        self.canvas.draw()
        self.toolbar.update()

#then the actual drawing function:
    def plot_data(self):
        """
            actual plotting function
        """
        raw_plot(self) #imported own function, see below
        self.toolbar.update() #not necesarry, but might help

def raw_plot(parent):
# note that all meta data / parameters from the parent are accessible !
# e.g. color = parent.color
  parent.fig.clear()
  a = parent.fig.add_subplot(211)
  etc. etc. etc.

Thought this might be helpful to get started. You might want to write things a
different way, though. Particularly the my application is a bit more than
just a short script, so feel free to drop anything you don't want.

One last thing: I'd recommend having a look at the matplotlib examples
('embedding_in_wx*.py').

Cheers,
Christian

···

On Thursday 08 June 2006 16:19, massimo sandal wrote:

Brian Blais ha scritto:
> I want to write a wxPython script to pull up pylab plots (in a separate
> window), based on menu or button choices. The script below crashes with
> a segmentation fault. Am I doing something wrong here? Is there a
> workaround or fix?

As far as I know, mixing wxpython and pylab this way is BAD.

You should better choose between:
- (1)Launch pylab in a separate thread
- (2) (What I do) Using WxMPL and embedding a matplotlib plot in a
wxPanel or wxFrame. You have to use the OO interface to matplotlib.

m.