import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation sum = 0 def data_gen(t=0): cnt = 0 sumy = 0 while cnt < 2000: cnt += 1 t += 1 y = np.random.randint(1,200,1) sumy=sumy+y yield t, sumy/cnt def init(): ax.set_ylim(50, 150) # Change as needed to cover the expected temperature range ax.set_xlim(0, 10) ax.set_xlabel('Count') ax.set_ylabel('Average value') del xdata[:] del ydata[:] line.set_data(xdata, ydata) return line, fig, ax = plt.subplots() line, = ax.plot([], [], lw=2) ax.grid() xdata, ydata = [], [] def run(data): # update the data t, y = data xdata.append(t) ydata.append(y) xmin, xmax = ax.get_xlim() if t >= xmax: ax.set_xlim(xmin, 2*xmax) ax.figure.canvas.draw() line.set_data(xdata, ydata) return line, ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=1, repeat=False, init_func=init) plt.show()