wx apps and matplotlib plots, are they friends?

I’ve converted an analysis program that does some
plotting to use the matplotlib API, then wrote a GUI control program using
wxWidgets to call it. I’ve tried to keep the two parts as separate as possible
so there is an analysis class that is instantiated in the GUI part, and then
the GUI part just calls the analysis methods.

Ok, so under ipython -pylab everything seems to (mostly) go
ok. The GUI comes up, data gets loaded into the analysis object, processed and
plotted in a couple of separate matplotlib windows (ie. ones that are not specifically
part of the GUI), and results come back to update some wx textctrl fields on
the GUI. I qualify this as mostly ok because if I use the TkAgg backend for the
plotting, which is done by the analysis class, the whole lot crashes to
non-existence as soon as either plot window is moved (yep, this is Windows). If
I use the wxAgg backend, however, it seems to play nice®.

The problem is when all this is done outside of ipython –
ie. running from the command line ala “python myAnalysisUI.py” -
the matplotlib figures come up when required but text fields are no longer
updated in the GUI once plotting is done. The GUI is still responsive to clicks
on buttons etc, but it’s as if the final bit after plotting gets blocked.

This makes me wonder about a couple of things. How well do essentially
independent matplotlib windows work with a wx App? The fact one backend works (wxAgg)
and one doesn’t (TkAgg) is a bit strange. Should I be doing something related
to threads, which is where maybe ipython is doing a better job, to finish what
needs doing in the GUI once plotting has finished? Haven’t done anything
involving threading so far so I’m guessing here. I should add I do import
a few things from pylab – figure, show, twinx. I gather pylab and wx don’t
mix well.

A Minor Detail: wxAgg backend plots by default are missing
the left and right navigation button bitmaps in the toolbar which looks a bit
funny to me. Only when you pan/zoom do they start to appear. I made the following
change to my local copy of backend_wx.py so they’d always appear, if
anyone’s interested. Doesn’t seem to be bothered when clicking arrows
if there’s no history.

def set_history_buttons(self):

can_backward =

(self._views._pos > 0)

can_forward =

(self._views._pos < len(self._views._elements) - 1)

···

self.EnableTool(self._NTB2_BACK, can_backward)

self.EnableTool(self._NTB2_FORWARD, can_forward)

self.EnableTool(self._NTB2_BACK, True)

self.EnableTool(self._NTB2_FORWARD, True)

The other vitals are: OS: WinXP, Python 2.5, matplotlib 0.90.1,
wxPython 2.8.0.1, ipython 0.8.2

Paul Smith

Systems Engineer

CAT Underground Mining Technology

Ph: +61 (0)3 9853 4050

Fax: +61 (0)3 9853 4955

Hi Paul,

Paul Smith wrote:

I�ve converted an analysis program that does some plotting to use the matplotlib API, then wrote a GUI control program using wxWidgets to call it. I�ve tried to keep the two parts as separate as possible so there is an analysis class that is instantiated in the GUI part, and then the GUI part just calls the analysis methods.

Ok, so under ipython -pylab everything seems to (mostly) go ok. The GUI comes up, data gets loaded into the analysis object, processed and plotted in a couple of separate matplotlib windows (ie. ones that are not specifically part of the GUI), and results come back to update some wx textctrl fields on the GUI. I qualify this as mostly ok because if I use the TkAgg backend for the plotting, which is done by the analysis class, the whole lot crashes to non-existence as soon as either plot window is moved (yep, this is Windows). If I use the wxAgg backend, however, it seems to play nice(r).

I think this is due to using TkAgg with wxPython, IIRC TK and WX don't
"like" to work together, I think there is some conflict but don't
remember the details.

The problem is when all this is done outside of ipython � ie. running from the command line ala �python myAnalysisUI.py� - the matplotlib figures come up when required but text fields are no longer updated in the GUI once plotting is done. The GUI is still responsive to clicks on buttons etc, but it�s as if the final bit after plotting gets blocked.

This makes me wonder about a couple of things. How well do essentially independent matplotlib windows work with a wx App?

What do you call an independent window? Is this a wx.Frame which shows
the matplotlib plots?

Do have a smallish sample which shows the problem?

...

The other vitals are: OS: WinXP, Python 2.5, matplotlib 0.90.1, wxPython 2.8.0.1, ipython 0.8.2

I have just about the same, WinXP / Vista, Python 2.5.1, matplotlib
0.90.1 wxPython 2.8.4.0 (but have used older versions too).

Werner

This appears to be your problem. If you are using mpl in a wx app,
you should not be importing pylab or running ipython in pylab mode
because you will get mainloop conflicts. Rather, use the matplotlib
API directly following the lead of examples/embedding_in_wx*.py and
tthen run ipython in -wthread mode rather than pylab mode.

JDH

···

On 6/22/07, Paul Smith <paul.smith@...1609...> wrote:

I've converted an analysis program that does some plotting to use the
matplotlib API, then wrote a GUI control program using wxWidgets to call it.
I've tried to keep the two parts as separate as possible so there is an
analysis class that is instantiated in the GUI part, and then the GUI part
just calls the analysis methods.

Ok, so under ipython -pylab everything seems to (mostly) go ok. The GUI

I would also highly recommend Ken McIvor's wxmpl:
http://agni.phys.iit.edu/~kmcivor/wxmpl/

It makes wxPython and mpl play very nicely together.

I have been involved in several threads recently about how no to
import pylab in data analysis libraries and when embedding in gui's.
You may find this thread useful:
http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg02732.html

Ryan

···

On 6/22/07, John Hunter <jdh2358@...287...> wrote:

On 6/22/07, Paul Smith <paul.smith@...1609...> wrote:
> I've converted an analysis program that does some plotting to use the
> matplotlib API, then wrote a GUI control program using wxWidgets to call it.
> I've tried to keep the two parts as separate as possible so there is an
> analysis class that is instantiated in the GUI part, and then the GUI part
> just calls the analysis methods.

> Ok, so under ipython -pylab everything seems to (mostly) go ok. The GUI

This appears to be your problem. If you are using mpl in a wx app,
you should not be importing pylab or running ipython in pylab mode
because you will get mainloop conflicts. Rather, use the matplotlib
API directly following the lead of examples/embedding_in_wx*.py and
tthen run ipython in -wthread mode rather than pylab mode.

JDH

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users