From: Clare S. <cla...@gm...> - 2012-04-23 08:03:48
|
Hi, I managed to figure this out. I need to pass an init function to FuncAnimation. Quoting from the MatPlotLib API documentation: *init_func* is a function used to draw a clear frame. If not given, the results of drawing from the first item in the frames sequence will be used. So just in case someone has the same problem, here's the correct code: import matplotlib import numpy as np from matplotlib.lines import Line2D import matplotlib.pyplot as plt import matplotlib.animation as animation class Scope: def __init__(self, ax, maxt=10, dt=1): self.ax = ax self.dt = dt self.maxt = maxt self.tdata = [0] self.ydata = [0] self.line = Line2D(self.tdata, self.ydata, marker='o') self.ax.add_line(self.line) self.ax.set_ylim(0, 200) self.ax.set_xlim(0, self.maxt) self.ax.set_xlabel("Time(s)") self.ax.set_ylabel("Jitter Buffer Size(ms)", color=self.line.get_color()) def init(self): self.line.set_data([], []) return self.line, def update(self, y): lastt = self.tdata[-1] if lastt == self.tdata[0] + self.maxt: # reset the arrays self.tdata = [self.tdata[-1]] self.ydata = [self.ydata[-1]] self.ax.set_xlim(self.tdata[0], self.tdata[0] + self.maxt) self.ax.figure.canvas.draw() t = self.tdata[-1] + self.dt self.tdata.append(t) self.ydata.append(y) self.line.set_data(self.tdata, self.ydata) return self.line, def emitter(): while True: yield np.random.randint(0,200) fig = plt.figure() ax = fig.add_subplot(111) scope = Scope(ax) # pass a generator in "emitter" to produce data for the update func ani = animation.FuncAnimation(fig, scope.update, emitter, interval=1000, blit=True, init_func=scope.init) plt.show() Best Regards, Clare. |