0

I'm plotting live data i'm getting from an arduino. on the x axis i'm displaying the time but the axis is becoming too long that everything becomes unreadable. my lists have a max lengt of 10. when it goes over this amount i start shifting the data one place left and add the new data at the back but this doesn't plot like i would like to. when i print the values i see my shiting is working but i'm missing something when plotting. this is my code:

import serial
import datetime as dt
import matplotlib.pyplot as plt




Time = []
Data = []
Data = Data[:10]
Time = Time[:10]
start = 0
j = 0
index = 0
max_Array = 9
array_Plaats = 0
old_Packet = 5
ser = serial.Serial('COM3', 9600, timeout=2)
ser.close()

plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)


def Array():
    global max_Array
    global array_Plaats
    global start
    global packet_Deco
    if (array_Plaats <= max_Array and start == 0):
        Data.append(packet_Deco)
        array_Plaats += 1
        Time.append(dt.datetime.now().strftime('%H:%M:%S'))

    else:
        start = 1
        global index
        print(index)
        if index == max_Array:
            index = 0
        while index < max_Array:
            j = index + 1
            Data[index] = Data[j]
            Time[index] = Time[j]
            index = index + 1
        Data[max_Array] = packet_Deco
        Time[max_Array] = dt.datetime.now().strftime('%H:%M:%S')

ser.open()

while True:
    global packet_Deco
    packet = ser.readline()

    if (packet != old_Packet):
        old_Packet = packet
        packet_Deco = int(packet.decode('utf'))
        #print(packet_Deco)
        Array()
        print(Data)
        print(len(Time))
        line1, = ax.plot(Time, Data, 'r-')
        plt.pause(0.0001)
        fig.canvas.draw()
        fig.canvas.flush_events()
  • For me it looks like your variables Time and Data are kept constant through whole while loop. Maybe you should update them within the loop to see some change in the plot? When you get it to update, this https://matplotlib.org/3.5.1/api/_as_gen/matplotlib.axes.Axes.set_xlim.html might help you to roll the xaxis forward when you get more data – paloman Feb 28 '22 at 14:29
  • the lists shouldn't stay constant. when i print them out they change the way i want them to but the plot doesn't. ifi use Axes.set_xlim shouldi put it in the loop or outside the loop and what data should i enter, my x axis are strings so i'm guessing normal integers wouldn't work –  Feb 28 '22 at 14:46
  • Why are the x-axis values strings if you have a variable "Time"? Convert them into [matplotlib dates](https://matplotlib.org/stable/api/dates_api.html) or datetime objects. Otherwise, matplotlib will generate a label for every single tick, and that will slow the updated plotting severely down with increasing numbers of labels. Then, you should plot the line object outside the loop and only update its values together with the axis limits inside the loop as [shown, for instance, here](https://stackoverflow.com/a/71090828/8881141) or [here](https://stackoverflow.com/a/71128728/8881141). – Mr. T Feb 28 '22 at 15:03
  • i figured out how to have the axis shift left, it's the same method used in the link you sent me but changed to my code and that works. the plotting is incredibly slow indeed. if i want to convert this what would you recommend i use, there is a lot ot choose from so i don't really know where to start –  Feb 28 '22 at 17:09

0 Answers0