For animated line plots, I found the Cookbook very helpful:
<URL:http://www.scipy.org/Cookbook/Matplotlib/Animations#head-3d51654b8306b1585664e7fe060a60fc76e5aa08>

I'm a novice with GUI stuff, but with that background, I tried the following.
I wanted a graph where data points were added as they become available.
The attached meets my current needs.  Maybe someone will find it helpful,
or maybe someone can suggest how to make it better. (I've made it into
a working example, using Tkinter.  Some values are still hard coded.)

Cheers,
Alan Isaac

from collections import deque
import math
import Tkinter as tk
import numpy as np

import matplotlib as mpl
mpl.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, 
NavigationToolbar2TkAgg
#from matplotlib.figure import Figure 

#x = np.arange(0,2*np.pi,0.01)
def mk_data(ct=[0]):
                ct[0] += 1
                return 5+ math.sqrt(ct[0])*np.sin(ct[0]/10.0)


class TSPlot(FigureCanvasTkAgg):
        """Provides a simple time-series plot of
        the most recent and previous 100 observations,
        where `datafunc` returns a single new observation.
        """
        def __init__(self, datafunc, master=None, title='', **kwargs):
                xlength = 101
                self._title = title
                self._datafunc = datafunc
                self._background = None
                self._line = None
                self._ylim = (0,1)
                self._xlim = (-100,0)
                self._ydata = deque(maxlen=xlength)
                #Python 3 range object not sliceable
                self._xdata = [x+1-xlength for x in range(xlength)]
                self.fig = mpl.figure.Figure(figsize=(5,2.5), dpi=100)
                self._ax = self.fig.add_subplot(111)
                FigureCanvasTkAgg.__init__(self, self.fig, master=master)
                #grid to master
                self.get_tk_widget().grid(row=0,column=0, columnspan=2)
                self.update_line_cnt = 0
        def setup(self):
                # create the initial "line" (a single observation)
                new_ydata = self.update_data()
                self.adjust_ylim(new_ydata)
                self.set_background()
        def adjust_ylim(self, datum):
                """Return bool. Resets `_ylim`
                (if needed to accommodate `_ydata`).
                """
                ylow, yhigh = self._ylim
                adjust = False
                if not (ylow < datum < yhigh):
                        maxy = max(self._ydata)
                        miny = min(self._ydata)
                        yhigh = maxy + 0.5 * (maxy-miny)
                        ylow = miny - 0.5 * (maxy-miny)
                        if yhigh == ylow:
                                yhigh += 1
                                ylow -= 1
                        adjust = True
                self._ylim = ylow, yhigh
                return adjust
        def update(self, *args):
                """Return None. Update the line plot."""
                # update the data
                newdata = self.update_data()
                ydata = self._ydata
                xdata = self._xdata
                if len(ydata) < len(xdata):
                        xdata = xdata[-len(ydata):]
                # restore the clean slate background
                self.restore_region(self._background)
                self.line.set_data([xdata,ydata])
                # draw just the animated artist
                self._ax.draw_artist(self.line)
                # redraw just the axes rectangle
                self.blit(self._ax.bbox)
        def update_data(self):
                """Return number, the new value from `_datafunc`.
                """
                new_ydata = self._datafunc()
                self._ydata.append(new_ydata)
                if self.adjust_ylim(new_ydata):
                        self.set_background()
                return new_ydata
        def set_background(self):
                """Return None. Resets the background
                of the canvas.
                """
                ax = self._ax
                ax.clear()
                ydata = self._ydata
                xdata = self._xdata[-len(ydata):]
                self.line, = ax.plot(xdata, ydata, animated=True)
                ax.set_title(self._title, fontsize='small')
                ax.set_xlim(self._xlim)
                ax.set_ylim(self._ylim)
                self.show()
                #save the background (everything but the animated line)
                # in `_background` (a pixel buffer)
                self._background = self.copy_from_bbox(ax.bbox)
        def run(self):
                for i in range(500):
                        self.update()



root = tk.Tk()
myapp = TSPlot(mk_data, master=root)
#myapp.grid(row=0, column=0, columnspan=2)
btn = tk.Button(master=root, command=myapp.setup, text="SetUp")
btn.grid(row=1, column=0)
btn = tk.Button(master=root, command=myapp.run, text="Run")
btn.grid(row=1, column=1)

root.mainloop()

------------------------------------------------------------------------------
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