0

I am learning to plot live data from serial port using matplotlib.

I am able to send data correctly to my serial port.

However, the value which I send on the serial port is not plotted correctly in my graph.

The cnt (x-axis) gets plotted correctly, however the temperature (y-axis) plots incorrect values.

Following is my (shabby) python code:-

import serial
import numpy
import matplotlib.pyplot as plt
from drawnow import *

import binascii
import serial
import time

x = []
y = []
plt.ion()
cnt=0

z1baudrate = 9600
z1port = 'COM6'

z1serial = serial.Serial(port=z1port, baudrate=z1baudrate)
z1serial.timeout = 1

print (z1serial.is_open)

def makeFig():
    plt.ylim(0,150)
    plt.title('Live Data')
    plt.grid(True)
    plt.ylabel('Temperature')
    plt.plot(x, 'ro-', label='F')
    plt.legend(loc='upper left')


if z1serial.is_open:
    while True:
        size = z1serial.inWaiting()
        if size:
            data = z1serial.read(1)
            data = (ord(data))
            print (data)
            if data:
                cnt = cnt+1
                x.append(cnt)
                y.append(data)
                drawnow(makeFig)
                plt.pause(.000001)
                cnt=cnt+1
                if(cnt>50):
                  x.pop(0)
                  y.pop(0)
            z1serial.flushInput()
            z1serial.flushOutput()

        else:
            print ('no data')
        time.sleep(1)
else:
    print ('z1serial not open')

How can I correctly plot the serial data (temperature) vs count (cnt) graph?

Thanks!

1 Answer 1

1
plt.plot(x, y,'ro-', label='F')

You forgot to put the y in the plot it seems

Sign up to request clarification or add additional context in comments.

1 Comment

That did it! Thanks a ton.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.