Here is untested code with some changes to make it better
import serial import numpy as np import adafruit_max31856 import matplotlib.pyplot as plt from drawnow import drawnow import datetime import math import csv import os from collections import deque from decimal import Decimal # instead of list, use collections.deque # deque has optional maxlen argument that set max number of elements # if append new element and len exceed maxlen elemnt from the other side is removed temp1 = deque(maxlen=50) # make deque with length 50 temp2 = deque(maxlen=50) # make deque with length 50 plt.ion() def make_fig(): plt.title('My Super Beautiful Live Streaming Sensor Data :-)') plt.grid(True)#Turn the grid on plt.plot() plt.xlabel('Time') plt.ylabel('Temperature') plt.plot(temp1, 'ro-',label='Temp1') plt.plot(temp2, 'b^-',label='Temp2') plt.legend(loc='upper right') plt.show() if __name__ == '__main__': ## user-defined params serialPort = 'COM4' out_path = r'C:\Users\Alphinity\Desktop\python3\' file_name = "signalSerial.csv" ser = serial.Serial('COM4', 115200) out_file = os.path.join(out_path, file_name) print (f"Writing the serial stream into file: {out_file}") print (f" [to see the stream: tail -f {out_file} ]") print (" [to exit: ctrl+c (the elegant way :) ]") while True: line = ser.readline().decode().strip() if line: line = line.split(',') # get list of 2 strings line = [Decimal(temp.strip()) for temp in line] # make line list of two Decimal objects t1, t2 = line # unpack line into two variables t1 and t2 temp1.append(t1) temp2.append(t2) drawnow(make_fig) plt.pause(.000001) with open(out_fiile, 'a') as f: wrtr = csv.writer(f) wrtr.writerow(line) # as alternative to last 3 lines with open(out_fiile, 'a') as f: f.write(f'{t1},{t2}\n')I didn't change your drawing of the plot and definitely program structure could be better
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs