1

I'm new to Python and I'm trying to plot some data taken from an Arduino hooked up to the Serial port through a USB. I have a thermistor connected to the Arduino, and would like to plot a history of the sampled temperatures. So, a graph that keeps expanding on the x-axis and keeps appending new temperature values to the y-axis.

When I run this code, however, I get a blank graph, and it never does anything else. I suspect my problem is not really understanding FuncAnimation very well. Below is my code.

import serial
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

ser = serial.Serial()
ser.baudrate = 38400
ser.port = 'COM4'
ser.open()

fig, ax = plt.subplots()
line, = ax.plot([])
i = 0

def init():
     xdata = []
     ydata = []
     line.set_data(xdata, ydata)
     return line,

def update_data(i)
    newpoint = float(ser.readline())
    oldxdata, oldydata = line.get_data()
    newydata = np.append(oldydata,newpoint)
    newxdata = np.append(oldxdata, i)
    line.set_data(newxdata, newydata)
    i = i + 1
    return line,

ani = animation.FuncAnimation(fig, update_data, init_func = init, fargs=i)

plt.show()    

I should note that after I run the program and close everything out, line.get_ydata returns a collection of temperature points. So, the serial port is working, I just am not graphing it correctly. Thank you.

0

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.