Hi,

2009/7/29 Jae-Joon Lee <lee.j.j...@gmail.com>:
> On Tue, Jul 28, 2009 at 9:11 AM, Roland Koebler<r.koeb...@yahoo.de> wrote:
[cut]
>> Any ideas?
>>
>
> http://matplotlib.sourceforge.net/examples/animation/animation_blit_gtk2.html
>
> The above example does something similar to (b).
> It saves the previous plot (only axes area is saved) as a bitmap. In
> next run, it restores the saved bitmap after shifting. And then draw
> only the new data points.
>
> The example requires the svn version of matplotlib.

I have attached another example of blit animation that does NOT
require the svn version of MPL.


   Antonio
#!/usr/bin/env python
"""
This an example script that shows how to do a Matplotlib blit animation in 
a Gtk2 window.

The script shows howto to bind to the MPL 'draw_event' in order to
make the blit animation working with padnning/zooming and window resizing.

Furthermore the MPL navigation toolbar is modified through the addition of two
custom buttons.

2009 (C) Antonino Ingargiola <trite...@gmail.com>
Licence: GNU GPL v2 or later.
"""

from time import sleep
import gtk
import gobject
import numpy as np

from matplotlib.figure import Figure
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
from matplotlib.backends.backend_gtk import NavigationToolbar2GTK as NavigationToolbar

##
# Simple GTK windows base classes
#
class BaseWindow:
    """A base gtk window that can be closed."""
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_default_size(550, 500)
        self.window.connect("delete_event", self.on_delete_event)
        self.window.connect("destroy", self.on_destroy)
    def main(self):
        gtk.main()
    def on_delete_event(self, widget, *args):
        return False
    def on_destroy(self, widget, data=None):
        gtk.main_quit()

class PlotWindow(BaseWindow):
    """A gtk window with a figure inside."""
    def __init__(self, show=True, create_axis=False):
        BaseWindow.__init__(self)
        self.figure = Figure()
        self.canvas = FigureCanvas(self.figure)
        if create_axis: self.ax = self.figure.add_subplot(111)
        self.toolbar = NavigationToolbar(self.canvas, self.window)
        self.vbox = gtk.VBox()
        self.vbox.pack_start(self.toolbar, expand=False)
        self.vbox.pack_start(self.canvas, expand=True)
        self.window.add(self.vbox)
        if show: self.window.show_all()

##
# The main application
#
class Plotter(PlotWindow):
    def __init__(self):
        PlotWindow.__init__(self, create_axis=True, show=False)
        # Now we have self.canvas, self.figure and self.ax correctly assigned
        
        # Let add two buttons to the toolbar: scale and play
        self.scalebutton = gtk.ToolButton(gtk.STOCK_ZOOM_FIT)
        self.scalebutton.connect("clicked", self.scale_clicked)
        self.toolbar.insert(self.scalebutton, 0)
        self.playbutton = gtk.ToggleToolButton(gtk.STOCK_MEDIA_PLAY)
        self.playbutton.set_active(False)
        self.playbutton.connect("clicked", self.play_toggled)
        self.toolbar.insert(self.playbutton, 0)
        self.window.show_all()
        
        # Now create the plot of the first frame (note: animated=True)
        self.t = np.arange(100)*0.1
        self.line, = self.ax.plot(self.t, np.sin(self.t), animated=True)
        self.ax.grid(True)
        
        # Connect the callback for the draw_event
        self.cid = self.canvas.mpl_connect('draw_event', self.ax_redraw)
        
        # Start the gtk main loop
        self.n = 0
        self.main() 

    def ax_redraw(self, widget=None):
        # Redraw the axis
        self.ax.draw()
        # Save the new background for the animation
        self.background = self.canvas.copy_from_bbox(self.ax.bbox)
        # The animated line needs to be drawn explicitly
        self.draw_line()

    def canvas_redraw(self):
        # Disconnect the 'draw_event' callbak to avoid recursion
        self.canvas.mpl_disconnect(self.cid)
        # Completely canvas redraw
        self.canvas.draw()
        # Reconnect the 'draw_event' callback
        self.cid = self.canvas.mpl_connect('draw_event', self.ax_redraw)
        # Save the new background for the animation
        self.background = self.canvas.copy_from_bbox(self.ax.bbox)
        # The animated line needs to be drawn explicitly
        self.draw_line()

    def draw_line(self):
        # Draw the line into the axis
        self.ax.draw_artist(self.line)
        self.canvas.blit(self.ax.bbox)

    def update_plot(self):
        """The fast (blit) update to be used in animations."""
        self.n += 1
        # Restore the clean slate background
        self.canvas.restore_region(self.background)
        # Update the line data
        self.line.set_ydata(np.sin(self.t+self.n*0.1))
        # Draw the line
        self.draw_line()
        return True

    def play_toggled(self, widget):
        if self.playbutton.get_active(): 
            self.gid = gobject.timeout_add(50, self.update_plot)
        else: 
            gobject.source_remove(self.gid)

    def scale_clicked(self, widget):
        x, y = self.line.get_xdata(), self.line.get_ydata()
        self.ax.set_ylim([y.min()*2, y.max()*2])
        self.ax.set_xlim([x.min()-2, x.max()+2])
        self.canvas_redraw()


if __name__ == "__main__": 
    Plotter()
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to