repeated GUI generation generates crash

Hi, I'm new to matplotlib, trying to migrate from Matlab, so please excuse my rookie ignorance and Matlab thinking.
My question is: why does a GUI figure crash upon repeated calls in IPython?
Here is a detailed description:
I'm using the Enthought Python Distribution (EPD_Py25) and I enter IPython with the command: "ipython". I then run the script below using: %run myScript.py. Everything works fine: when I press 'Enter' while in the GUI the figure closes and I can retrieve the data from obj.x. However, when I run the script again using %run myScript.py a figure is generated but nothing happens; I am forced to reset everything using pyplot.close('all'), but cannot regain the GUI functionality without exiting IPython.

This problem does not occur if I use EPD's PyLab (i.e. "ipython -pylab"). Does anyone know why?

Here is the script:
from numpy.random import rand
from matplotlib import pyplot
class myGUI:
     def __init__(self,x):
         self.fig = pyplot.gcf()
         self.x = x
         #initiate figure:
         self.connect()
         pyplot.plot(self.x)
         pyplot.show()
     def connect(self):
         self.cidkeypress = self.fig.canvas.mpl_connect('key_press_event', self.keypress)
     def disconnect(self):
         self.fig.canvas.mpl_disconnect(self.cidkeypress)
         pyplot.close(self.fig)
     def keypress(self,event):
         if event.key == 'enter':
             self.disconnect()
         else:
             self.x = rand(self.x.size)
             pyplot.plot(self.x)
             self.fig.canvas.draw()
pyplot.figure()
obj = myGUI(rand(10))

Todd Pataky wrote:

I'm using the Enthought Python Distribution (EPD_Py25) and I enter IPython with the command: "ipython".

....

This problem does not occur if I use EPD's PyLab (i.e. "ipython - pylab"). Does anyone know why?

because the whole point of the "pylab" flag to ipython is to tell ipython to start up the MPL plotting stuff in separate thread so that it can work like you want it to. If you don't' use that flag, then each call to pyplot tries to start up a new app, ,but the old one is still running, hence you problems.

ipython itself can be used for all sort of things that have nothing to do with matplotlib, so that's not its default behavior.

Just use ipython -pylab if you want to do interactive plotting, that's what it's for.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...

Chris, thank you very much for your reply. I encountered a related error, and was hoping you could help me understand this one too...
Using ipython -pylab I run the script below to generate a GUI, but when the GUI finishes running (after 3 button clicks), ipython crashes with the following error message: "Segmentation fault". This error does not occur if I remove the call to pyplot.close in myGUI.disconnect, nor does it occur if I use a key_press_event instead of a button_press_event.
Why is an error generated when I try to close the figure?
Why does this error occur for button_press_events but not key_press_events?

from numpy.random import rand
from matplotlib import pyplot

class myGUI:
     def __init__(self,x):
         self.fig = pyplot.gcf()
         self.n = 0
         self.connect()
         pyplot.plot(x)
         pyplot.show()

     def connect(self):
         self.cidbuttonpress = self.fig.canvas.mpl_connect('button_press_event', self.buttonpress)

     def disconnect(self):
         self.fig.canvas.mpl_disconnect(self.cidbuttonpress)
         pyplot.close(self.fig)

     def buttonpress(self,event):
         self.n += 1
         if self.n >=2:
             self.disconnect()

pyplot.figure()
obj = myGUI(rand(10))

Todd Pataky wrote:

I'm using the Enthought Python Distribution (EPD_Py25) and I enter
IPython with the command: "ipython".

....

This problem does not occur if I use EPD's PyLab (i.e. "ipython -
pylab"). Does anyone know why?

because the whole point of the "pylab" flag to ipython is to tell
ipython to start up the MPL plotting stuff in separate thread so that it
can work like you want it to. If you don't' use that flag, then each
call to pyplot tries to start up a new app, ,but the old one is still
running, hence you problems.

ipython itself can be used for all sort of things that have nothing to
do with matplotlib, so that's not its default behavior.

Just use ipython -pylab if you want to do interactive plotting, that's
what it's for.

-Chris

···

On 22 May 2009, at 00:49, Christopher Barker wrote: