Plotting streaming data in Python
import serial #import serial lib
import numpy #import numpy lib for array operations
import matplotlib.pyplot as plt #import matplotlib
from drawnow import *
arduinoSerialData = serial.Serial('COM6',9600)
plt.ion() #Tell matplotlib you want interactive mode to plot live data
data1Array=[] #Data Arrays to be plotted are initialized
data2Array=[] #Data Arrays to be plotted are initialized
cnt=0
def makeFig():
plt.ylim(0,25) #stop autoscaling by defining limits as: min.=-1 and max.=25
plt.plot(data1Array, 'ro-', label='data1') #plot in red color and dots connected by line
plt.title('Streaming Serial data')
plt.ylabel('Sawtooth data 1')
plt.legend(loc='upper left')
plt2=plt.twinx()
plt2.plot(data2Array,'b^-', label='data2')
plt.ylim(0,25)
plt2.legend(loc='upper right')
plt2.set_ylabel('Sawtooth data 2')
plt2.ticklabel_format(useOffset=False) #Tell matplotlib not to autoscale
while True:
while (arduinoSerialData.inWaiting() == 0): #Wait until data is there
pass
arduinoString = arduinoSerialData.readline();
#print arduinoString
dataArray = arduinoString.split(',') #split the string at the comma
data1 = float(dataArray[0])
data2 = float(dataArray[1])
data1Array.append(data1)
data2Array.append(data2)
#print data1, ",", data2
cnt=cnt+1 #Wait for the first 50 data to get accumulated
if(cnt>50):
data1Array.pop(0) #Pop out the oldest data from the array to plot only 50 latest data
data2Array.pop(0)
drawnow(makeFig)
plt.pause(0.000001)
import numpy #import numpy lib for array operations
import matplotlib.pyplot as plt #import matplotlib
from drawnow import *
arduinoSerialData = serial.Serial('COM6',9600)
plt.ion() #Tell matplotlib you want interactive mode to plot live data
data1Array=[] #Data Arrays to be plotted are initialized
data2Array=[] #Data Arrays to be plotted are initialized
cnt=0
def makeFig():
plt.ylim(0,25) #stop autoscaling by defining limits as: min.=-1 and max.=25
plt.plot(data1Array, 'ro-', label='data1') #plot in red color and dots connected by line
plt.title('Streaming Serial data')
plt.ylabel('Sawtooth data 1')
plt.legend(loc='upper left')
plt2=plt.twinx()
plt2.plot(data2Array,'b^-', label='data2')
plt.ylim(0,25)
plt2.legend(loc='upper right')
plt2.set_ylabel('Sawtooth data 2')
plt2.ticklabel_format(useOffset=False) #Tell matplotlib not to autoscale
while True:
while (arduinoSerialData.inWaiting() == 0): #Wait until data is there
pass
arduinoString = arduinoSerialData.readline();
#print arduinoString
dataArray = arduinoString.split(',') #split the string at the comma
data1 = float(dataArray[0])
data2 = float(dataArray[1])
data1Array.append(data1)
data2Array.append(data2)
#print data1, ",", data2
cnt=cnt+1 #Wait for the first 50 data to get accumulated
if(cnt>50):
data1Array.pop(0) #Pop out the oldest data from the array to plot only 50 latest data
data2Array.pop(0)
drawnow(makeFig)
plt.pause(0.000001)
Comments
Post a Comment