[wxPython-users] Re: Recommend a plotting library?
Werner F. Bruhin
werner.bruhin at free.fr
Wed Apr 11 10:01:38 PDT 2007
Hi Grant,
Grant Edwards wrote:
> OK, I'll try that. The recipe I'm currently using is at =
>
> http://www.py2exe.org/index.cgi/MatPlotLib
>
> =
The beginning of that page is a bit out of date.
Attached a cleaned up setup.py with the corresponding matplot example file.
>> 3) if it creates error messages, remove or rename the file:
>> /backends/_wxagg.pyd
>> =
You need to do Andrea's step 3 if you do not use wxPython 2.6 Unicode.
Werner
-------------- next part --------------
#!/usr/bin/env python
"""
An example of how to use wx or wxagg in an application with a custom
toolbar
"""
from matplotlib.numerix import arange, sin, pi
import matplotlib
# uncomment the following to use wx rather than wxagg
#matplotlib.use('WX')
#from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas
# comment out the following to use wx rather than wxagg
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCa=
nvas
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg
from matplotlib.backends.backend_wx import _load_bitmap
from matplotlib.figure import Figure
from matplotlib.numerix.mlab import rand
import wx
class MyNavigationToolbar(NavigationToolbar2WxAgg):
"""
Extend the default wx toolbar with your own event handlers
"""
ON_CUSTOM =3D wx.NewId()
def __init__(self, canvas, cankill):
NavigationToolbar2WxAgg.__init__(self, canvas)
# for simplicity I'm going to reuse a bitmap from wx, you'll
# probably want to add your own.
self.AddSimpleTool(self.ON_CUSTOM, _load_bitmap('stock_left.xpm'),
'Click me', 'Activate custom contol')
self.Bind(wx.EVT_TOOL, self._on_custom, id=3Dself.ON_CUSTOM)
def _on_custom(self, evt):
# add some text to the axes in a random location in axes (0,1)
# coords) with a random color
# get the axes
ax =3D self.canvas.figure.axes[0]
# generate a random location can color
x,y =3D tuple(rand(2))
rgb =3D tuple(rand(3))
# add the text and draw
ax.text(x, y, 'You clicked me',
transform=3Dax.transAxes,
color=3Drgb)
self.canvas.draw()
evt.Skip()
class CanvasFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,
'CanvasFrame',size=3D(550,350))
self.SetBackgroundColour(wx.NamedColor("WHITE"))
self.figure =3D Figure(figsize=3D(5,4), dpi=3D100)
self.axes =3D self.figure.add_subplot(111)
t =3D arange(0.0,3.0,0.01)
s =3D sin(2*pi*t)
self.axes.plot(t,s)
self.canvas =3D FigureCanvas(self, -1, self.figure)
self.sizer =3D wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1, wx.TOP | wx.LEFT | wx.EXPAND)
# Capture the paint message
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.toolbar =3D MyNavigationToolbar(self.canvas, True)
self.toolbar.Realize()
if wx.Platform =3D=3D '__WXMAC__':
# Mac platform (OSX 10.3, MacPython) does not seem to cope with
# having a toolbar in a sizer. This work-around gets the buttons
# back, but at the expense of having the toolbar at the top
self.SetToolBar(self.toolbar)
else:
# On Windows platform, default window size is incorrect, so set
# toolbar width to figure width.
tw, th =3D self.toolbar.GetSizeTuple()
fw, fh =3D self.canvas.GetSizeTuple()
# By adding toolbar in sizer, we are able to put it at the bott=
om
# of the frame - so appearance is closer to GTK version.
# As noted above, doesn't work for Mac.
self.toolbar.SetSize(wx.Size(fw, th))
self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
# update the axes menu on the toolbar
self.toolbar.update()
self.SetSizer(self.sizer)
self.Fit()
def OnPaint(self, event):
self.canvas.draw()
event.Skip()
class App(wx.App):
def OnInit(self):
'Create the main window and insert the custom frame'
frame =3D CanvasFrame()
frame.Show(True)
return True
app =3D App(0)
app.MainLoop()
-------------- next part --------------
# -*- coding: iso-8859-1 -*-#
from distutils.core import setup
import os
from os.path import join
import shutil
import glob
import py2exe
from py2exe.build_exe import py2exe
import sys
import matplotlib
mpdir, mpfiles =3D matplotlib.get_py2exe_datafiles()
# should not be needed as o py2exe 0.6 no
##### cleanup dist and build directory first (for new py2exe version)
####if os.path.exists("dist/prog"):
#### shutil.rmtree("dist/prog")
####
####if os.path.exists("dist/lib"):
#### shutil.rmtree("dist/lib")
####
####if os.path.exists("build"):
#### shutil.rmtree("build")
#### =
#
# A program using wxPython
# The manifest will be inserted as resource into the .exe. This
# gives the controls the Windows XP appearance (if run on XP ;-)
#
manifest_template =3D '''
<?xml version=3D"1.0" encoding=3D"UTF-8" standalone=3D"yes"?>
<assembly xmlns=3D"urn:schemas-microsoft-com:asm.v1" manifestVersion=3D"1.0=
">
<assemblyIdentity
version=3D"5.0.0.0"
processorArchitecture=3D"x86"
name=3D"%(prog)s"
type=3D"win32"
/>
<description>%(prog)s</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type=3D"win32"
name=3D"Microsoft.Windows.Common-Controls"
version=3D"6.0.0.0"
processorArchitecture=3D"X86"
publicKeyToken=3D"6595b64144ccf1df"
language=3D"*"
/>
</dependentAssembly>
</dependency>
<trustInfo xmlns=3D"urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel
level=3D"AsInvoker"
uiAccess=3D"false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
'''
RT_MANIFEST =3D 32
#
# options for py2exe
options =3D {"py2exe": {"compressed": 1,
"optimize": 2,
"packages": ["encodings",
"pytz", "matplotlib.numerix",
],
"excludes": ["MySQLdb", "Tkconstants", "Tkinter", "tc=
l"
],
"dll_excludes": ["tcl84.dll", "tk84.dll", "wxmsw26uh_=
vc.dll"]
}
}
zipfile =3D r"lib\library.zip"
class MetaBase:
def __init__(self, **kw):
self.__dict__.update(kw)
self.version =3D '1.0'
self.author =3D "yourname"
self.author_email =3D "name at whatever.com"
self.company_name =3D ""
self.copyright =3D "2003 - 2007 by whoever"
self.url =3D "http://www.whatever.com/"
self.download_url =3D "http://www.whatever.com/en/"
self.trademark =3D ""
self.comments =3D "a comment on the prog"
self.name =3D "the prog name"
self.description =3D "a desc on the prog"
wx_emb =3D MetaBase(
script =3D "embedding_in_wx4.py",
other_resources =3D [(RT_MANIFEST, 1, manifest_template % dict(=
prog=3D"your prog name"))],
## icon_resources =3D [(1, r"images/some.ico")],
dest_base =3D r"prog\wx_embed")
setup(
classifiers =3D ["Copyright:: your name",
"Development Status :: 5 Stable",
"Intended Audience :: End User",
"License :: Shareware",
"Operating System :: Microsoft :: Windows 2000",
"Operating System :: Microsoft :: Windows XP",
"Operating System :: Microsoft :: Windows 9x",
"Programming Language :: Python, wxPython",
"Topic :: Home Use"
"Natural Language :: German",
"Natural Language :: French",
"Natural Language :: English"],
windows =3D [wx_emb],
options =3D options,
zipfile =3D zipfile,
data_files =3D [("lib\\matplotlibdata", mpfiles), # use this line if =
use zipfile option
## matplotlib.get_py2exe_datafiles(), # or this one if y=
ou don't use the zipfile option
]
)
More information about the wxpython-users
mailing list