A wxPython/Matplotlib/Basemap example

Dear ALL,

Enclosed is an sample application I put together (partially using
wxGlade) that is a first attempt at providing a working example of a
GUI frontend with wxPython and Matplotlib/Basemap.

The application consists of two panels, separated by a splitter. The
left panel has a notebook with two pages, which have, respectively, a
listbox and a read-only multine text control. The right panel has a
figure object created at runtime, to which a basemap object is
attached.

The application reads comma-delimited data files of geographic
coordinates (in decimal format) selected by the user, appending them
to the listbox. When a file is selected on the list, the points are
plotted on the basemap displayed in the panel to the right.

Well, at least it should! It turns out that the map is correctly
displayed, but it then fills almost the whole parent window,
overlapping the left panel where the notebook with the listbox and the
text control should appear! This illustrates one of the worst (in my
opinion) things about wxPython, that is the handling of "sizers" to
correctly place the widgets on an application window.

I have commented the parts of the code where the map is created, so
that one can see clearly what happens with and without the map plot.
Just uncomment these parts to see the map displayed (with a toolbar
properly placed, at least under Linux).

If someone can help me with this, this sample application could
provide a useful skeleton to build programs which integrate
Matplotlib/Basemap with wxPython (incidentally, this sample
application bears a very slight resemblance to Google Earth... :)). I
am, of course, donating it to the community.

Thanks in advance!

With best regards,

Gaia.py (6.93 KB)

cities.csv (179 Bytes)

···

--
Dr. Mauro J. Cavalcanti
Ecoinformatics Studio
P.O. Box 46521, CEP 20551-970
Rio de Janeiro, RJ, BRASIL
E-mail: maurobio@...287...
Web: http://studio.infobio.net
Linux Registered User #473524 * Ubuntu User #22717
"Life is complex. It consists of real and imaginary parts."

Mauro Cavalcanti wrote:

Well, at least it should! It turns out that the map is correctly
displayed, but it then fills almost the whole parent window,
overlapping the left panel where the notebook with the listbox and the
text control should appear! This illustrates one of the worst (in my
opinion) things about wxPython, that is the handling of "sizers" to
correctly place the widgets on an application window.

well, I love sizers -- but they do take a while to wrap your brain around.

Your difficulty probably stems from using a combination of wxGlade and handwritten code. Also, I don't know if wxGlade forces you to do it this way, but you have a whole bunch of stuff all nested in the Frame, when it would be better designed if each component was it's own class.

anyway, I think your problem is:

# ----- UNCOMMENT THE LINES BELOW TO DRAW THE MAP
    #self.figure = Figure(figsize=(10,5))
    #self.canvas = FigureCanvas(self, -1, self.figure)

you've given "self" as the parent of your FigureCanvas, when it needs to be parented by the Panel it goes on, which I think is self.PlotPanel, so the above line should be:

self.canvas = FigureCanvas(self.PlotPanel, -1, self.figure)

One thing wx does do "wrong" is let you put a control on one parent, then Add it to a sizer attached to a different parent -- it should probably raise an error when you do that.

-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...

Dear Chris,

Thank your very much for your prompt reply.

2008/11/20 Christopher Barker <Chris.Barker@...259...>:

well, I love sizers -- but they do take a while to wrap your brain around.

I love Python *and* wxPython/wxWidgets -- but having worked several
years with RAD tools like Visual Basic and (much better) Borland
Delphi, I find all available GUI builders for Python very limited (and
sizers are among these perceived limitations).

Your difficulty probably stems from using a combination of wxGlade and
handwritten code. Also, I don't know if wxGlade forces you to do it this

As a matter of fact, wxGlade forces one to do this. I usually do not
like (or use) code generated by wxGlade, but for rapid prototyping I
find it to be better than, for example, Boa Constructor (which is as
huge and slow is the boa snakes themselves).

you've given "self" as the parent of your FigureCanvas, when it needs to be
parented by the Panel it goes on, which I think is self.PlotPanel, so the
above line should be:

Sure, it worked, but I still have problems with properly sizing the
figure. This I could amend by declaring:

self.figure = Figure()
instead of self.figure = Figure(figsize=(10,5))

and:

self.ax = self.figure.add_subplot(111)
instead of self.ax = self.figure.add_axes([0,0,1,1])

However the figure is still not properly sized when the window is
maximized (but this is obviously another problem with my handling of
wxPython classes).

I hope to have this example up and running as it may be a useful
addition to the collection of examples in the Basemap module, saving a
lot of time for others!

With best regards,

···

--
Dr. Mauro J. Cavalcanti
Ecoinformatics Studio
P.O. Box 46521, CEP 20551-970
Rio de Janeiro, RJ, BRASIL
E-mail: maurobio@...287...
Web: http://studio.infobio.net
Linux Registered User #473524 * Ubuntu User #22717
"Life is complex. It consists of real and imaginary parts."

Mauro Cavalcanti wrote:

Dear Chris,

Thank your very much for your prompt reply.

2008/11/20 Christopher Barker <Chris.Barker@...259...>:

well, I love sizers -- but they do take a while to wrap your brain around.
    
I love Python *and* wxPython/wxWidgets -- but having worked several
years with RAD tools like Visual Basic and (much better) Borland
Delphi, I find all available GUI builders for Python very limited (and
sizers are among these perceived limitations).

I agree (coming from Delphi), they either don't work or are too limited.
For wxPython I developed a very simple method, which even has the F12 function :wink:
  http://mientki.ruhosting.nl/data_www/pylab_works/pw_gui_support.htm
code can be downloaded here
  Google Code Archive - Long-term storage for Google Code Project Hosting.

btw I would love to see your Basemap,
(but as I'm in the iddle of releasing a large project based on an old version of MatPlatLib,
so I don't dare to update),
are there any screenshots ?

thanks,
Stef Mientki

Dear Stef,

Thanks for the message. Your PyLab-Works looks really great (and I
liked the interface).

I could not progress much further than applying the last advice from
Chris Baker, which at least allowed the plot to be shown in its
correct place.

Indeed, the sample applications now *almost* works, except when the
window is resized. Also, the points plotted on the map only appears
when the map panel is resized using the splitter (because in this
case, the entire panel is redrawn and it seems that this is the
missing trick).

I am attaching the current version of source code plus a screenshot.

With best regards,

2008/11/20 Stef Mientki <stef.mientki@...287...>:

Gaia.py (6.92 KB)

···

I agree (coming from Delphi), they either don't work or are too limited.
For wxPython I developed a very simple method, which even has the F12
function :wink:
http://mientki.ruhosting.nl/data_www/pylab_works/pw_gui_support.htm
code can be downloaded here
Google Code Archive - Long-term storage for Google Code Project Hosting.

btw I would love to see your Basemap,
(but as I'm in the iddle of releasing a large project based on an old
version of MatPlatLib,
so I don't dare to update),
are there any screenshots ?

thanks,
Stef Mientki

--
Dr. Mauro J. Cavalcanti
Ecoinformatics Studio
P.O. Box 46521, CEP 20551-970
Rio de Janeiro, RJ, BRASIL
E-mail: maurobio@...287...
Web: http://studio.infobio.net
Linux Registered User #473524 * Ubuntu User #22717
"Life is complex. It consists of real and imaginary parts."

hello Mauro,

Dear Stef,

Thanks for the message. Your PyLab-Works looks really great (and I

liked the interface).
I’m glad you liked it, hope to release PyLab_Works this month.

I could not progress much further than applying the last advice from

Chris Baker, which at least allowed the plot to be shown in its

correct place.

Indeed, the sample applications now almost works, except when the

window is resized. Also, the points plotted on the map only appears

when the map panel is resized using the splitter (because in this

case, the entire panel is redrawn and it seems that this is the

missing trick).
maybe you could send an explicit resize event to the panel or to the splitter:
self.Panel_Bottom.SendSizeEvent ()

I am attaching the current version of source code plus a screenshot.
nice screenshot, thanks.
Stef

···

On Fri, Nov 21, 2008 at 3:45 AM, Mauro Cavalcanti <maurobio@…287…> wrote:

With best regards,

2008/11/20 Stef Mientki <stef.mientki@…287…>:

I agree (coming from Delphi), they either don’t work or are too limited.

For wxPython I developed a very simple method, which even has the F12

function :wink:

http://mientki.ruhosting.nl/data_www/pylab_works/pw_gui_support.htm

code can be downloaded here

http://code.google.com/p/pylab-works/downloads/list

btw I would love to see your Basemap,

(but as I’m in the iddle of releasing a large project based on an old

version of MatPlatLib,

so I don’t dare to update),

are there any screenshots ?

thanks,

Stef Mientki

Dr. Mauro J. Cavalcanti

Ecoinformatics Studio

P.O. Box 46521, CEP 20551-970

Rio de Janeiro, RJ, BRASIL

E-mail: maurobio@…287…

Web: http://studio.infobio.net

Linux Registered User #473524 * Ubuntu User #22717

“Life is complex. It consists of real and imaginary parts.”

Dear Stef,

2008/11/21 Stef Mientki <stef.mientki@...287...>:

maybe you could send an explicit resize event to the panel or to the
splitter:
self.Panel_Bottom.SendSizeEvent ()

Yes, it worked! I just was not aware of this SendEvent() in wxPython,
it is handy. Now, I have to work out the correct display of the map
when the window is resized.

Will also use a CheckList control in place of the current Listbox, so
that plotting of points on the map can be turned on or off by the
user.

Of course, this is just a sample demonstration (but just plot a "Blue
Marble" image on the map panel and use an orthographic projection and
you can see what this humble sample will look like... ).

Thanks a lot for your help.

With best regards,

···

--
Dr. Mauro J. Cavalcanti
Ecoinformatics Studio
P.O. Box 46521, CEP 20551-970
Rio de Janeiro, RJ, BRASIL
E-mail: maurobio@...287...
Web: http://studio.infobio.net
Linux Registered User #473524 * Ubuntu User #22717
"Life is complex. It consists of real and imaginary parts."

Hello!

I am attaching the current version of source code plus a screenshot.

I would like to ask you why you are trying to implement this by yourself in wxPython/matplotlib.

There are already two pythonised applications that may be used for your purpose. And they are aware of the projections and different geographical formats:
* QGIS with Pythons libary and plugins: http://qgis.org/content/view/145/113/

* Thuban: http://thuban.intevation.org/

Maybe this could save you some work/time.

Kind regards,
Timmie

Dear Tim,

Thanks for your message.

I am not just "trying", I am indeed implementing a software package
for species distribution mapping and biogeographic analysis, using
wxPython/Matplotlib. And, by the way, it is going quite well.

Both Thuban and QGis, as generalized implementations of GIS, are far
from adequate for my needs (and of fellow conservation biologists).

Hope this helps.

Best regards,

2008/11/25 Tim Michelsen <timmichelsen@...1423...>:

···

Hello!

I am attaching the current version of source code plus a screenshot.

I would like to ask you why you are trying to implement this by yourself
in wxPython/matplotlib.

There are already two pythonised applications that may be used for your
purpose. And they are aware of the projections and different
geographical formats:
* QGIS with Pythons libary and plugins:
http://qgis.org/content/view/145/113/

* Thuban: http://thuban.intevation.org/

Maybe this could save you some work/time.

Kind regards,
Timmie

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

--
Dr. Mauro J. Cavalcanti
Ecoinformatics Studio
P.O. Box 46521, CEP 20551-970
Rio de Janeiro, RJ, BRASIL
E-mail: maurobio@...287...
Web: http://studio.infobio.net
Linux Registered User #473524 * Ubuntu User #22717
"Life is complex. It consists of real and imaginary parts."

Hello Mauro,

I am not just "trying",

Sorry for "disregarding" your using the term trying.

  I am indeed implementing a software package

for species distribution mapping and biogeographic analysis, using
wxPython/Matplotlib.

Thanks for being more specific. There is another program aiming at /similar/ goals:
http://www.metamodellers.com/epigrass.html

But this is not my domain of work...

And, by the way, it is going quite well.

My crongrats!

Both Thuban and QGis, as generalized implementations of GIS, are far
from adequate for my needs (and of fellow conservation biologists).

I just thought of there because they easyly convert CSV into geographical data.

In case you are after format conversion you may see: ogr2ogr.

Hope this helps.

Vice versa. I thought that my ideas my be useful.

Nevertheless, have all possible sucess!

Kind regards,
Timmie