2

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()
Community
  • 1
  • 1
Jesvin Jose
  • 22,498
  • 32
  • 109
  • 202
  • What did you try for redrawing? – Dr. Jan-Philip Gehrcke May 25 '12 at 15:15
  • I dont know how to proceed actually – Jesvin Jose May 25 '12 at 17:56
  • I'm not familiar with matplotlib, but if your problem is just that the new plot you added does not show up, does FigueCanvas have some sort of Refresh() method? Have you tried calling it? If not, have you tried refreshing the panel (self.Refresh()) after adding the new plot? Maybe self.Layout() would work too (if Layout() does help, I suggest assing "self.SetAutoLayout(True)" to your init). – acattle May 27 '12 at 01:15
  • May be the following will help you:- http://stackoverflow.com/questions/11762966/embedding-interactive-matplotlib-figures-in-wxpython?lq=1 – Umar Yusuf Jun 03 '16 at 20:27

1 Answers1

2

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()
hdrz
  • 461
  • 6
  • 12