This post will focus on remedying such slow downs by constraining the data set to a manageable size. We'll also demonstrate some of the multi-plot capabilities of MatlibPlot.
Let's get started;
Reviewing the functionality from the past post, we can plot a 'live', ever-growing data set by something of the following sort:
#!/usr/bin/python
from pylab import *;
import time;
import collections;
import re;
def log(M):
print "__(log) " + M;
schedEventLoopTime=0.01;
MaxX=50.0;
MaxLen=10;
def test00():
#--plots a dynamic, continuously growing 2D data set in an autoscaling simple graph
plt.ion();
fig=plt.figure(1);
ax1=fig.add_subplot(111);
l1,=ax1.plot(100,100,'r-');
D=[];
i=0.0;
while (i < MaxX):
D.append((i,sin(i)));
T=[x[0] for x in D];
L=[x[1] for x in D];
l1.set_xdata(T);
l1.set_ydata(L);
ax1.relim();
ax1.autoscale_view();
plt.draw();
i+=0.10;
plt.pause(schedEventLoopTime);
show(block=False);
plt.close();
#---main---
log("main process initializing");
test00();
log("main process terminating");
def test02():
#--plots a dynamic, fixed-length 2D data set in an autoscaling simple graph
plt.ion();
fig=plt.figure(1);
ax1=fig.add_subplot(111);
l1,=ax1.plot(100,100,'r-');
D = collections.deque(maxlen=MaxLen);
i=0.0;
while (i < MaxX):
D.append((i,sin(i)));
T=[x[0] for x in D];
L=[x[1] for x in D];
l1.set_xdata(T);
l1.set_ydata(L);
plt.ylim([min(L),max(L)]);
plt.xlim([min(T),max(T)]);
plt.draw();
i+=0.10;
plt.pause(schedEventLoopTime);
show(block=False);
plt.close();
The use of the collection allows pre-allocation of a fixed array, appending to a full array will simply push out the oldest value. The 'live' plot is constrained to a fixed data size, resulting in a plot resembling this;
We'll use the same fixed-sizing concept in a multi-plot example;
def test03():
#--plots a dynamic, fixed-length 2D data set in an autoscaling multi-plot graph
plt.ion();
fig=plt.figure(1);
ax1=fig.add_subplot(311);
ax2=fig.add_subplot(312);
ax3=fig.add_subplot(313);
l1,=ax1.plot(100,100,'r-');
l2,=ax2.plot(100,100,'r-');
l3,=ax3.plot(100,100,'r-');
time.sleep(3);
D = collections.deque(maxlen=MaxLen);
i=0.0;
while (i < MaxX):
D.append((i,sin(i),cos(i),cos(i*2)));
T1=[x[0] for x in D];
L1=[x[1] for x in D];
L2=[x[2] for x in D];
L3=[x[3] for x in D];
l1.set_xdata(T1);
l1.set_ydata(L1);
l2.set_xdata(T1);
l2.set_ydata(L2);
l3.set_xdata(T1);
l3.set_ydata(L3);
ax1.set_xlim([min(T1),max(T1)]);
ax1.set_ylim([min(L1),max(L1)]);
ax2.set_xlim([min(T1),max(T1)]);
ax2.set_ylim([min(L2),max(L2)]);
ax3.set_xlim([min(T1),max(T1)]);
ax3.set_ylim([min(L3),max(L3)]);
plt.draw();
i+=0.10;
plt.pause(schedEventLoopTime);
show(block=False);
plt.close();
That's all for now, happy coding.
No comments:
Post a Comment