469,596 Members | 2,260 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,596 developers. It's quick & easy.

embedding matplotlib in wxpython


I've been trying to embed matplotlib in wxpython. I want to be able to
put a figure (axes) in a wx.Panel and place it somewhere in my GUI.
The GUI should have other panels with buttons etc. that can control
the output on the figure. I've been looking at the examples from the
matplotlib website, but can't seem to get it to work..

Does anyone here have experience in embedding matplotlib in wxpython?

I have attached my code.. it makes two panels.. one with a matplotlib
plot, and one with a button.. but the plotpanel is just a small square
in the corner.... !! ..

Any help is appreciated!


import wx
import pylab
from matplotlib.numerix import arange, sin, cos, pi
import matplotlib

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure

class App(wx.App):

def OnInit(self):
self.frame = MainFrame("BioXtas - Autoplotter", (50,60),

return True

class MainFrame(wx.Frame):

def __init__(self, title, pos, size):
wx.Frame.__init__(self, None, -1, title, pos, size)

pPanel = PlotPanel(self, -1) # Plot panel

bPanel = ButtonPanel(self, 100,500, (200,100)) # button

sizer = wx.BoxSizer(wx.VERTICAL)



class ButtonPanel(wx.Panel):

def __init__(self, Parent, xPos, yPos, insize):

pos = (xPos, yPos)
wx.Panel.__init__(self, Parent, -1, pos, style =
wx.RAISED_BORDER, size = insize)

button = wx.Button(self, -1, 'HELLO!!', (10,10), (150,50))

class NoRepaintCanvas(FigureCanvasWxAgg):
"""We subclass FigureCanvasWxAgg, overriding the _onPaint method,
so that
the draw method is only called for the first two paint events.
After that,
the canvas will only be redrawn when it is resized.
def __init__(self, *args, **kwargs):
FigureCanvasWxAgg.__init__(self, *args, **kwargs)
self._drawn = 0

def _onPaint(self, evt):
Called when wxPaintEvt is generated
if not self._isRealized:

if self._drawn < 2:
self.draw(repaint = False)
self._drawn += 1

class PlotPanel(wx.Panel):

def __init__(self, parent, id = -1, color = None,\
dpi = None, style = wx.NO_FULL_REPAINT_ON_RESIZE,

wx.Panel.__init__(self, parent, id = id, style = style,

self.figure = Figure(None, dpi)
self.canvas = NoRepaintCanvas(self, -1, self.figure)
self._resizeflag = True

self.Bind(wx.EVT_IDLE, self._onIdle)
self.Bind(wx.EVT_SIZE, self._onSize)


def draw(self):
if not hasattr(self, 'subplot'):
self.subplot = self.figure.add_subplot(111)
theta = arange(0, 45*2*pi, 0.02)
rad = (0.8*theta/(2*pi)+1)
r = rad*(8 + sin(theta*7+rad/1.8))
x = r*cos(theta)
y = r*sin(theta)
#Now draw it
self.subplot.plot(x,y, '-r')

def _onSize(self, event):
self._resizeflag = True

def _onIdle(self, evt):
if self._resizeflag:
self._resizeflag = False

def _SetSize(self, pixels = None):
This method can be called to force the Plot to be a desired
size, which defaults to
the ClientSize of the panel
if not pixels:
pixels = self.GetClientSize()
if __name__ == "__main__":

app = App(0)

Apr 26 '07 #1
0 3123

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Oh Kyu Yoon | last post: by
3 posts views Thread by Madhusudan Singh | last post: by
4 posts views Thread by Matt Feinstein | last post: by
2 posts views Thread by dr_tyson | last post: by
7 posts views Thread by Mr. Roboto | last post: by
reply views Thread by =?ISO-8859-15?Q?J=FCrgen_Kareta?= | last post: by
2 posts views Thread by John | last post: by
4 posts views Thread by John Henry | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.