Jesvin Jose
Jesvin Jose

Reputation: 23088

How to redraw a mathplotlib figure in a wxpython panel?

I want to draw a new figure on a each draw() operation. I pieced together code for drawing a static figure which is never updated after the object is created. But I want to be able to redraw when presented with new data.

How do I structure my code to do redrawable figures?


Here is the code in question, that draws exactly once:

from numpy import arange, sin, pi
import matplotlib
matplotlib.use('WXAgg')

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx

class CanvasPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        #self.size = (800, 50)
        self.figure = Figure()
        self.figure.set_size_inches( (8,1) )
        self.figure.set_dpi(80)
        #self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self, -1, self.figure )
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizer)
        self.Fit()

    def draw(self):
        self.axes = self.figure.add_subplot(111)
        t = arange(0.0, 3.0, 0.01)
        s = sin(2 * pi * t)
        self.axes.plot(t, s)
        #time.sleep(5)
        #self.figure.clear()

Upvotes: 2

Views: 3058

Answers (1)

hdrz
hdrz

Reputation: 471

As @acattle suggested in his comment, all you have to do is add these lines to your drawing subroutine, after you update your plot:

 self.canvas.draw()
 self.canvas.Refresh()

Upvotes: 2

Related Questions