From: Hans F. <H.F...@so...> - 2004-09-21 06:56:28
|
Dear all, I am trying to use gcurve to continuously display the last N points of a scalar function. More concrete: a simulation goes through a number of iterations and for each iteration, I'd like to plot one scalar value. I want to display the last N points using gcurve. Since I didn't see a way of deleting individual points from gcurve, I have written the little class (appended below) which does nearly what I want. What it doesn't do is to auto-scale the graph so that the last N points are displayed in optimal resolution. I am happy to compute the required min and max values for the y-axis myself but don't know how to pass it to vpython in the correct way. Can anybody help me? Or is this plotting of last N-points achievable much easier using another approach? Thank you, Hans import Numeric import visual.graph class PlotLastPoints: """plots the last N points of data that is provided subsequently. The first N-points are drawn using the usual vpython-gcurve(pos=[x,y]) method. After that, the first point is overwritten with the N+1 entry, the 2nd point is replaced by the N+2 value etc. This is intended for a simulation where we have iterations on the x-axis, so the actual value there doesn't really matter. Bug: By not using gcurve's default mechanism for adding points, visual doesn't autoscale the graphs. """ def __init__(self,N): self.N = N self.data=Numeric.zeros((N,),'d') self.last = 0 self.disp=visual.graph.gdisplay(width=600,height=200) self.plot = visual.graph.gcurve() self.full = False #have we more or exactly N points in self.data ? def update(self, E ): if self.full == False and (self.last == self.N): self.full = True if self.full: self.last = self.last % self.N self.data[self.last]=E self.plot.gcurve.x=Numeric.arange(self.N)[:] self.plot.gcurve.y=self.data[:] else: self.plot.plot(pos=[self.last,E]) self.data[self.last]=E self.last +=1 if __name__=="__main__": import math,time plot = PlotLastPoints(50) for i in range(2000): plot.update( math.sin(i/50.0)) time.sleep(0.02) |