|
From: John H. <jd...@gm...> - 2008-08-29 17:53:18
|
On Fri, Aug 29, 2008 at 12:01 PM, Ray Salem <ray...@gm...> wrote:
> This is my first post, I really enjoy using python/numpy/scipy/matplotlib
> - great replacement for matlab. I am having a problem, with plot 1 point at
> a time. i am attempt to emulate an issue in the lab, where we get samples
> every second and plot them. The figure becomes non-responsive and eventually
> stops plotting, does anyone have an idea.
Yep, this is a bad idea. The line object in matplotlib is a fairly
heavy object which has a lot of stuff under the hood. Each plot
command creates a new line object. What you want to do is create just
one (or a few) line objects, each of which handles many points. Below
is the first pass naive implementation to show you how to do this --
you will probably want to improve on this by using a better data
structure to store the growing points, eg a buffer that drops old
points off the end, or a dynamically resizing numpy array which grows
intelligently when it gets full. The main point is that we create a
single line object and add the new data to it....
import time
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.collections as collections
plt.ion() # interactive plotting
fig = plt.figure()
ax = fig.add_subplot(111, autoscale_on=False)
xdata, ydata = [0.], [0.]
line, = ax.plot(xdata, ydata, 'o')
dt = 0.1
for i in range(100):
x = xdata[-1] + dt
y = np.sin(2*np.pi*x)
xdata.append(x)
ydata.append(y)
line.set_data(xdata, ydata)
ax.set_xlim(x-1, x)
ax.set_ylim(-1.1, 1.1)
fig.canvas.draw()
time.sleep(0.1)
plt.show()
|