Using matplot lib in wxPython problem refresh screen

Hello,

I have a problem when I try to refresh a plot I create.

The code is shown below:

What I want to do is that when the user initiates a key press the plot is updated. (If you minimise and maximise then it is done, the problem is the repainting of the window. I have tried searching on the web but cant find out how to do it (as well as trying lots of things with show() but alas no luck so far).

Any help much appreciated.

Thanks

import wx
import os
import matplotlib

either WX or WXAgg can be used here.

matplotlib.use(‘WX’)

from matplotlib.backends.backend_wxagg import FigureCanvasWx as FigCanvas

matplotlib.use(‘WXAgg’)
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas

from matplotlib.figure import Figure
import matplotlib.numerix as numpy

class PlotFrame(wx.Frame):

start_msg  = """ Press Any Key to Initiate Plot - well thats the plan... """
def __init__(self):
    wx.Frame.__init__(self, None, -1, "Test Plotting")
    self.fig   = Figure((5.0,3.0), 100)
    self.canvas= FigCanvas(self, -1, self.fig)
    self.axes  = self.fig.add_axes([0.15,0.15,0.75,0.75])

    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(self.canvas, 1, wx.LEFT|wx.TOP|wx.GROW)
    sizer.Add(wx.StaticText(self,-1, self.start_msg), 0,
              wx.ALIGN_LEFT|wx.TOP)

    self.canvas.Bind(wx.EVT_KEY_DOWN, self.onKeyEvent)

    self.SetSizer(sizer)
    self.Fit()
    self.Build_Menus()

def Build_Menus(self):
    """ build menus """
    MENU_EXIT  = wx.NewId()
    MENU_SAVE  = wx.NewId()

    menuBar = wx.MenuBar()

    f0 = wx.Menu()
    f0.Append(MENU_EXIT,   "E&xit", "Exit")
    menuBar.Append(f0,     "&File");

    self.SetMenuBar(menuBar)

    self.Bind(wx.EVT_MENU, self.onExit ,        id=MENU_EXIT)

def onKeyEvent(self,event=None):
    """ capture , act upon keystroke events"""
    print "I plot here"
    self.Plot_Data()
    print "but how do i get it to be drawn?"
    self.Show()

def onExit(self,event=None):
    self.Destroy()

def Plot_Data(self):
    t = numpy.arange(0.0,5.0,0.01)
    s = numpy.sin(2.0*numpy.pi*t)
    c = numpy.cos(0.4*numpy.pi*t)
    self.axes.plot(t,s)
    self.axes.plot(t,c)

if name == ‘main’:
app = wx.PySimpleApp()
fig = PlotFrame()
fig.Show(True)
app.MainLoop()

···

She said what? About who? Shameful celebrity quotes on Search Star!

your self.show() is passing the order to the frame; you need to comunicate with the canvas with draw()

I suggest you to use wxmpl, it is the best way to integrate mpl into wx
http://agni.phys.iit.edu/~kmcivor/wxmpl/

Bob Cumming wrote:

···

Hello,
I have a problem when I try to refresh a plot I create.
The code is shown below:
What I want to do is that when the user initiates a key press the plot is updated. (If you minimise and maximise then it is done, the problem is the repainting of the window. I have tried searching on the web but cant find out how to do it (as well as trying lots of things with show() but alas no luck so far).
Any help much appreciated.
Thanks

import os
import matplotlib
# either WX or WXAgg can be used here.
# matplotlib.use('WX')
# from matplotlib.backends.backend_wxagg import FigureCanvasWx as FigCanvas
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas
from matplotlib.figure import Figure
import matplotlib.numerix as numpy
class PlotFrame(wx.Frame):
    start_msg = """ Press Any Key to Initiate Plot - well thats the plan... """
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Test Plotting")
        self.fig = Figure((5.0,3.0), 100)
        self.canvas= FigCanvas(self, -1, self.fig)
        self.axes = self.fig.add_axes([0.15,0.15,0.75,0.75])

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.canvas, 1, wx.LEFT|wx.TOP|wx.GROW)
        sizer.Add(wx.StaticText(self,-1, self.start_msg), 0,
                  wx.ALIGN_LEFT|wx.TOP)
        self.canvas.Bind(wx.EVT_KEY_DOWN, self.onKeyEvent)
        self.SetSizer(sizer)
        self.Fit()
        self.Build_Menus()
    def Build_Menus(self):
        """ build menus """
        MENU_EXIT = wx.NewId()
        MENU_SAVE = wx.NewId()
        menuBar = wx.MenuBar()
        f0 = wx.Menu()
        f0.Append(MENU_EXIT, "E&xit", "Exit")
        menuBar.Append(f0, "&File");

        self.SetMenuBar(menuBar)
        self.Bind(wx.EVT_MENU, self.onExit , id=MENU_EXIT)
       def onKeyEvent(self,event=None):
        """ capture , act upon keystroke events"""
        print "I plot here"
        self.Plot_Data()
        print "but how do i get it to be drawn?"
        self.Show()

    def onExit(self,event=None):
        self.Destroy()
    def Plot_Data(self):
        t = numpy.arange(0.0,5.0,0.01)
        s = numpy.sin(2.0*numpy.pi*t)
        c = numpy.cos(0.4*numpy.pi*t)
        self.axes.plot(t,s)
        self.axes.plot(t,c)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    fig = PlotFrame()
    fig.Show(True)
    app.MainLoop()

------------------------------------------------------------------------
She said what? About who? Shameful celebrity quotes on Search Star! <http://www.msnsearchstar.com>
------------------------------------------------------------------------

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
------------------------------------------------------------------------

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options