in fact that may become clearer with my code.
There are two classes. One does the animation. The other is supposed to add a button. The infinite loop is at the end


Bruno

Greg Willden a écrit :
Hi Bruno,

Which method are you using to do the animation?
Timer, Idle Event?

If you are using a timer then it is pretty straightforward to start and stop the timer from the handler for the button you added.

I am using wxPython and I've hacked the animation_blit_wx.py example.

You need functions like this:
timer = wx.Timer(panel,TIMER_ID)
wx.EVT_TIMER(panel,TIMER_ID, update_line) << Here you bind the timer to your update function

timer.Start(10)
timer.Stop()


Cheers,
Greg

On 8/16/07, *bruno* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:

    Hello,

    I am probably reinventing the wheel. I am trying to get "rolling"
    graph
    a la labview with matplotlib. I have more or less managed to get
    what I
    want using the simple animation technique provided by matplotlib. I
    would like now to have a button an the same graph that would enable me
    to stop the program that is in an infinite acquisition loop.

    I have managed to use a matplotlib widget with the graph to make it
    print something on a terminal, however I do not see how to make it
    stop
    an infinite loop. I have tried to make the button change the state
    of a
    variable and give the state of this variable a a stop condition to the
    loop. However this doesn't work. In fact the button doesen't seems
    to be
    "listening" during the loop.

    Any idea of how I should be doing that ?

    Cordially

    Bruno

    -------------------------------------------------------------------------
    This SF.net email is sponsored by: Splunk Inc.
    Still grepping through log files to find problems?  Stop.
    Now Search log events and configuration files using AJAX and a
    browser.
    Download your FREE copy of Splunk now >>  http://get.splunk.com/
    _______________________________________________
    Matplotlib-users mailing list
    Matplotlib-users@lists.sourceforge.net
    <mailto:Matplotlib-users@lists.sourceforge.net>
    https://lists.sourceforge.net/lists/listinfo/matplotlib-users
    <https://lists.sourceforge.net/lists/listinfo/matplotlib-users>




--
Linux. Because rebooting is for adding hardware.

# -*- coding: utf-8 -*-
# Class to get rolling grahs à la Labview
import pylab as p
import numpy.core.ma as m
import numpy as n
import matplotlib.widgets as w
class RollingGraph:
    """
    Class that gives rolling graphs. Implemeneted with low-tech
    matplotlib solution

    ***Beware to work this class has to be used with the numpy package in newer
    version than the 0.9.9.2706 given with the enthought python package. I
    have tested it on numpy 1.0.3 that required also to upgrade matplotlib to
    version 0.90.1***
    """
    def __init__(self,x,y,xlim,ylim,npts=300,color="b",logy=False):
        p.ion()
        p.clf()
        self.ax=p.subplot(111)
        if logy:
            self.line,=p.semilogy(x,y,".-")
            if ylim <= 0:
                raise("ylim must be >0 in log mode")
            self.ax.set_ylim(ylim)
        else:
            self.line,=p.plot(x,y,".-")
            self.ax.set_ylim(ylim)
        self.ax.set_xlim(xlim)
        self.ymin=ylim[0]
        self.xmax=xlim[1]
        self.xmin=xlim[0]
        self.npts=npts
        self.x=m.zeros(npts,dtype=float)
        self.y=m.zeros(npts,dtype=float)
        self.x[0]=x
        self.y[0]=y
        self.index=0
        self.logy=logy
        
    def update_data(self,x,y):
        self.index+=1
        if (self.index<self.npts):
            self.x[self.index]=x
            self.y[self.index]=y
            mask=m.ones(self.npts)
            mask[0:self.index]=0
            self.line.set_xdata(m.masked_array(self.x,m.make_mask(mask)))
            self.line.set_ydata(m.masked_array(self.y,m.make_mask(mask)))
            y=self.y[0:self.index]
            if self.logy:
                if min(y)==0:
                    yminlim=1e-2
                elif min(y)<1:
                    yminlim=min(y)**1.1
                else:
                    yminlim=min(y)**0.9
                if yminlim>self.ymin:
                        yminlim=self.ymin
            else:
                yminlim=p.min(y)*(1-0.1*p.sign(p.min(y)))
            self.ax.set_ylim([yminlim,p.max(y)*(1+0.1*p.sign(p.max(y)))])
        else:
            self.x=n.roll(self.x,-1)
            self.y=n.roll(self.y,-1)
            self.x[-1]=x
            self.y[-1]=y
            self.line.set_xdata(self.x)
            self.line.set_ydata(self.y)
            self.ax.set_xlim([p.min(self.x),p.max(self.x)])
            if self.logy:
                if min(self.y)==0:
                    yminlim=1e-2
                elif min(self.y)<1:
                    yminlim=min(self.y)**1.1
                else:
                    yminlim=min(self.y)**0.9
                if yminlim>self.ymin:
                        yminlim=self.ymin
            else:
                yminlim=p.min(self.y)*(1-0.1*p.sign(p.min(self.y)))
            self.ax.set_ylim([yminlim,p.max(self.y)*(1+0.1*p.sign(p.max(self.y)))])
        p.draw()
            
class RollingViewer(RollingGraph):

    """
    class that add a large label giviing the current Y value and
    a stop button
    """
    
    def __init__(self,y,ylim,npts=300,color="b",logy=False):
        RollingGraph.__init__(self,p.array([0]),y,[0,300],ylim,npts=300,color="b",logy=False)
        p.subplots_adjust(top=0.6)
        self.indic=p.title("%05g" % y[0],fontsize=90,color="blue")
        self.indic.set_y(1.35)
        self.indic.set_x(0.46)
        self.button_stop=w.Button(p.axes([0.4,0.65,0.2,0.1]),"STOP")
        self.stop=False
        self.button_stop.on_clicked(self.stop_boucle)
    def update_data(self,y):
        print self.index
        RollingGraph.update_data(self,p.array([self.index]),y)
        self.indic.set_text("%05g" % y[0])
    def stop_boucle(self,event):
        self.stop=True
        print "coucou\n"



        
##if __name__=="__main__":
##    g=RollingGraph(p.array([0]),p.array([1e-3]),[0,300],[1e-3,1],logy=True)
##    for i in p.arange(1,400):
##        g.update_data(i,1-p.cos(i*p.pi/200.))


if __name__=="__main__":
   g=RollingViewer(p.array([0]),[0,1])
   while not g.stop:
       g.update_data(p.array([p.rand()]))
 
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to