Sayali Sheode
Sayali Sheode

Reputation: 31

Graph plots are slow than the real time values that are received

I am trying to get data from arduino and trying to then show the data in real time in python using subplot. The values that are coming from arduino uno board are fast and are displayed in the python console also at the same rate but when I am trying to plot the real time data in the graph it is very slowly plotted. It needs to as fast as the rate of values that are coming from the uno board. Please help. Here is my code:

import serial 
import numpy
import matplotlib.pyplot as plt 
from drawnow import *
x = []
y = []
z = []
magnitude = []
arduinoData = serial.Serial('com4', 9600) 
plt.ion() 
count=0
fig = plt.figure()

def makeFig():
    ax1 = fig.add_subplot(4,1,1)
    ax1.plot(x, 'ro-', label='X axis')
    ax2 = fig.add_subplot(4,1,2)
    ax2.plot(y, 'b^-', label='Y axis')
    ax3 = fig.add_subplot(4,1,3)
    ax3.plot(z, 'gp-', label='Y axis')
    ax4 = fig.add_subplot(4,1,4)
    ax4.plot(magnitude, 'yo-', label='X axis')
while True: 
    while (arduinoData.inWaiting()==0):
        pass
    arduinoString = arduinoData.readline()
    dataArray = arduinoString.split(',')   
    xaxis = float( dataArray[0])            
    yaxis = float( dataArray[1])            
    zaxis = float( dataArray[2])    
    mag =float( dataArray[3])   
    x.append(xaxis)                     
    y.append(yaxis)                     
    z.append(zaxis) 
    magnitude.append(mag)
    drawnow(makeFig)
    count = count + 1

Upvotes: 1

Views: 1234

Answers (1)

Paul Cornelius
Paul Cornelius

Reputation: 10946

nowThere are some things you must understand before you can find a good solution. How fast does the data arrive from arduino? How fast is the drawnow function? These timings are not under your control, so if the data arrives faster than the plotting routine can execute then the task as you have defined it is impossible. All Python versions have a time module, and the function time.time() returns the current time in seconds. This can be used to measure the speed of the drawnow function. You may need to cache a chunk of data before updating the plot. Updating the plot a few times a second will give the illusion of real time, and that may be good enough.

To see how fast the graph plots, use:

t = time.time()
drawnow()
print(time.time()-t)  # time in seconds

Upvotes: 2

Related Questions