python and matplotlib animation

After searching through the forums and trying many different things I have
been unable to get animation working correctly using matplotlib in my python
application. I'm hoping one of the experts here can take a quick look at my
code and tell me what I am doing incorrectly.

What I am seeing is that the each line continues to be drawn as part of my
[timer] update call. I suspect that it is related to the 'hold' call but
changing the hold state had no effect. I've tried numerous different
scenarios based on available examples but so far nothing has seemed to help.

Here is the code:

import matplotlib as mpl
mpl.use('WXAgg')
mpl.rcParams['toolbar'] = 'None'
import wx
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas
from matplotlib.figure import Figure
import numpy as num

# begin wxGlade: dependencies
# end wxGlade

# begin wxGlade: extracode

# end wxGlade

class HeartPanel(wx.Panel):
    def __init__(self, *args, **kwds):
        # begin wxGlade: HeartPanel.__init__
        kwds["style"] = wx.RAISED_BORDER|wx.TAB_TRAVERSAL
        wx.Panel.__init__(self, *args, **kwds)

        self.__set_properties()
        self.__do_layout()
        # end wxGlade
        mpl.rc('figure', fc='black', dpi=96, ec='black')
        self.figure = Figure(figsize=(5.2, 1.25))
        self.canvas = Canvas(self, -1, self.figure)
        self.canvas.SetBackgroundColour(wx.Colour(0, 0, 0))
# self.canvas.draw()
        self.bgnd = None
        self.timer = wx.Timer(self, 5555)
        self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
        self.count = 0

    def __set_properties(self):
        # begin wxGlade: HeartPanel.__set_properties
        self.SetMinSize((500,120))
        self.SetBackgroundColour(wx.Colour(0, 0, 0))
        self.SetForegroundColour(wx.Colour(0, 255, 0))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: HeartPanel.__do_layout
        pass
        # end wxGlade

    # pylint: disable-msg=W0201
    def init_plot(self):
        '''
        Method to initialize the plot that is used to animate the display.
        '''
        self.figure.subplots_adjust(left=0, top=1, right=1, bottom=0)
        self.axes = self.figure.add_subplot(111)
        if self.bgnd == None:
            self.bgnd = self.canvas.copy_from_bbox(self.axes.bbox)
        self.axes.set_axis_bgcolor('black')
        self.x_data = num.arange(0, 2*num.pi, 0.01)
        self.line = self.axes.plot(self.x_data, \
                                   num.sin(self.x_data), \
                                   color='green', \
                                   figure=self.figure, \
                                   lw=2, \
                                   animated=True)
        self.axes.hold(False)
    
    def start_timer(self, time_value=100):
        '''
        Method to start the animation update timer.
        '''
# self.axes.draw()
        self.canvas.Show(True)
# self.canvas.draw()
        self.count = 0
        self.timer.Start(time_value, wx.TIMER_CONTINUOUS)
        
    def stop_timer(self):
        '''
        Method to stop the animation update timer.
        '''
        self.canvas.Show(False)
        self.timer.Stop()
        
    def on_timer(self, event):
        '''
        Event handler for the animation update timer.
        '''
        self.count += 1
        self.canvas.restore_region(self.bgnd)
        # update the data
        self.line[0].set_ydata(num.sin(self.x_data + self.count/10.0))
        self.axes.clear()
        # just draw the animated artist
        self.axes.draw_artist(self.line[0])
        # just redraw the axes rectangle
        self.canvas.blit(self.axes.bbox)
        
# end of class HeartPanel

Any help is greatly appreciated.

···

--
View this message in context: http://www.nabble.com/python-and-matplotlib-animation-tp21185506p21185506.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

I'll be happy to take a look at this -- I did a quick read through of
the code, and did not see an obvious error, so please create a
complete, free-standing example which uses model data, eg a sine wave
of varying amplitude or frequency over time, and I'll try and take a
look tonight on a machine which has wxpython installed. I

JDH

···

On Mon, Dec 29, 2008 at 1:13 AM, davev <dveach@...2429...> wrote:

After searching through the forums and trying many different things I have
been unable to get animation working correctly using matplotlib in my python
application. I'm hoping one of the experts here can take a quick look at my
code and tell me what I am doing incorrectly.

Will do (done actually). Here is the list of files:
MyFrame.py
HeartPanel.py
app.py
anim.wxg

I’ve included the wxGlade file just to be complete. Please let me know if you see anything that is obviously wrong here. It’s not much different from some of the examples I’ve looked at but doesn’t produce similar results. dave

···

John Hunter-4 wrote:

On Mon, Dec 29, 2008 at 1:13 AM, davev wrote:

After searching through the forums and trying many different things I have
been unable to get animation working correctly using matplotlib in my python
application. I’m hoping one of the experts here can take a quick look at my
code and tell me what I am doing incorrectly.
I’ll be happy to take a look at this – I did a quick read through of
the code, and did not see an obvious error, so please create a
complete, free-standing example which uses model data, eg a sine wave
of varying amplitude or frequency over time, and I’ll try and take a
look tonight on a machine which has wxpython installed. I
JDH



Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users


View this message in context: Re: python and matplotlib animation

Sent from the matplotlib - users mailing list archive at Nabble.com.

The problem was that you did not call "draw" in your plot init method
before saving the background region. Unless you are in interactive
mode in pylab, you must manually force a figure draw with a call to
figure.canvas.draw. Eg, the method should look like::

    def init_plot(self):
        '''
        Method to initialize the plot that is used to animate the display.
        '''
        self.figure.subplots_adjust(left=0, top=1, right=1, bottom=0)
        self.axes = self.figure.add_subplot(111)
        self.axes.set_axis_bgcolor('black')
        self.figure.canvas.draw()
        if self.bgnd == None:
            self.bgnd = self.canvas.copy_from_bbox(self.axes.bbox)

        self.x_data = num.arange(0, 2*num.pi, 0.01)
        self.line = self.axes.plot(self.x_data, \
                                   num.sin(self.x_data), \
                                   color='green', \
                                   figure=self.figure, \
                                   lw=2, \
                                   animated=True)
        self.axes.hold(False)

I was able to run your example, and with these changes it works as expected.

JDH

···

On Mon, Dec 29, 2008 at 2:04 PM, davev <dveach@...2429...> wrote:

Will do (done actually). Here is the list of files: MyFrame.py HeartPanel.py
app.py anim.wxg I've included the wxGlade file just to be complete. Please
let me know if you see anything that is obviously wrong here. It's not much
different from some of the examples I've looked at but doesn't produce
similar results. dave

Thank you John, that made the difference. I had read that you needed to call
the draw function for the canvas first but it wasn't clear from the
examples. Now that you have given me the correct place to make the call,
things are working as expected.

If we wanted to, this small project could easily be turned into an
additional example for the animation example code so that others could
potentially benefit from it as well. If you think that is a good idea, let
me know and I'll look at making it happen. At the very least, the final
version of the files could be posted so that others could easily
view/download the files as example code. I would include the wxGlade file so
that there would also be an example of that available as well.

Thanks again for the help.
dave

John Hunter-4 wrote:

···

On Mon, Dec 29, 2008 at 2:04 PM, davev <dveach@...2429...> wrote:

Will do (done actually). Here is the list of files: MyFrame.py
HeartPanel.py
app.py anim.wxg I've included the wxGlade file just to be complete.
Please
let me know if you see anything that is obviously wrong here. It's not
much
different from some of the examples I've looked at but doesn't produce
similar results. dave

The problem was that you did not call "draw" in your plot init method
before saving the background region. Unless you are in interactive
mode in pylab, you must manually force a figure draw with a call to
figure.canvas.draw. Eg, the method should look like::

    def init_plot(self):
        '''
        Method to initialize the plot that is used to animate the display.
        '''
        self.figure.subplots_adjust(left=0, top=1, right=1, bottom=0)
        self.axes = self.figure.add_subplot(111)
        self.axes.set_axis_bgcolor('black')
        self.figure.canvas.draw()
        if self.bgnd == None:
            self.bgnd = self.canvas.copy_from_bbox(self.axes.bbox)

        self.x_data = num.arange(0, 2*num.pi, 0.01)
        self.line = self.axes.plot(self.x_data, \
                                   num.sin(self.x_data), \
                                   color='green', \
                                   figure=self.figure, \
                                   lw=2, \
                                   animated=True)
        self.axes.hold(False)

I was able to run your example, and with these changes it works as
expected.

JDH

------------------------------------------------------------------------------
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
View this message in context: http://www.nabble.com/python-and-matplotlib-animation-tp21185506p21222019.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Sure, that sounds great. I think the example with the start and stop
buttons would be nice, as would the wxglade part. I can add them to
the animations examples dir.

JDH

···

On Tue, Dec 30, 2008 at 1:31 PM, davev <dveach@...2429...> wrote:

Thank you John, that made the difference. I had read that you needed to call
the draw function for the canvas first but it wasn't clear from the
examples. Now that you have given me the correct place to make the call,
things are working as expected.

If we wanted to, this small project could easily be turned into an
additional example for the animation example code so that others could
potentially benefit from it as well. If you think that is a good idea, let
me know and I'll look at making it happen. At the very least, the final
version of the files could be posted so that others could easily
view/download the files as example code. I would include the wxGlade file so
that there would also be an example of that available as well.

Please feel free to do so. If you need anything from me, I will be glad to
provide it.

dave

John Hunter-4 wrote:

···

On Tue, Dec 30, 2008 at 1:31 PM, davev <dveach@...2429...> wrote:

Thank you John, that made the difference. I had read that you needed to
call
the draw function for the canvas first but it wasn't clear from the
examples. Now that you have given me the correct place to make the call,
things are working as expected.

If we wanted to, this small project could easily be turned into an
additional example for the animation example code so that others could
potentially benefit from it as well. If you think that is a good idea,
let
me know and I'll look at making it happen. At the very least, the final
version of the files could be posted so that others could easily
view/download the files as example code. I would include the wxGlade file
so
that there would also be an example of that available as well.

Sure, that sounds great. I think the example with the start and stop
buttons would be nice, as would the wxglade part. I can add them to
the animations examples dir.

JDH

------------------------------------------------------------------------------
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
View this message in context: http://www.nabble.com/python-and-matplotlib-animation-tp21185506p21226091.html
Sent from the matplotlib - users mailing list archive at Nabble.com.