I am on python 3.7. I am trying to read data from a serial port, it would be 7 different bytes. Then I would like to plot each different byte on a different subplot. I want to read the serial port every 500ms and each time I read add the new data to the subplots. Every read is giving one more data to plot on every subplot. That's basically sensor reading.
Here is the code I have written:
from time import sleep import serial import matplotlib.pyplot as plt
f=plt.figure(1)
ax=[0 for x in range(7)]
for i in range(0,7):
ax[i]=f.add_subplot(4,2,1+i)
ser = serial.Serial('COM3', 115200) # Establish the connection on a specific port
counter = 0
byte=ser.readline() #first line not to be plotted
while True:
counter +=1
ser.write(b'9') # send a command to the arduino
byte=ser.read(7) #read 7 bytes back
for i in range(0,7):
ax[i].plot(counter, byte[i]) # Trying to plot the new values to each different subplots
plt.pause(0.01)
sleep(.5) # Delay for one half of a second
The figure is showing and the x axis and y axis are adapting to the value I want to plt but there is no data at all on the plot. If I use scatter instead of plot it works, but then it is less versatile and I can't draw te type of graph I want. I also try to reproduce the problem without using a serial data but just displaying points of a list one after the other like that:
import matplotlib.pyplot as plt
from time import sleep
f=plt.figure()
series=[[4,3,2,1],[8,7,6,5],[12,11,10,9]]
counter=0
ax=[0 for x in range(7)]
for i in range(0,3):
ax[i]=f.add_subplot(4,2,1+i)
for j in range (0,4):
counter=counter+1
for i in range(0,3):
ax[i].plot(counter,series[i][j])
plt.pause(0.01)
sleep(1)
And it is doing exactly the same thing, the final image I have on the graph is that:
Which shows axis took what I wanted to plot but did not plot anything.
The point is I do not want to clear the full plot and redraw everything because for the data sensor I will have about 30days of data to display in continuous.
What am I doing wrong with the code I have written?
EDIT: After comment of ImportanceOfBeingErnest I have tried implementing the answer given here. The code is then:
from time import sleep
import serial
import matplotlib.pyplot as plt
import numpy
plt.ion()
f=plt.figure()
ax=[0 for x in range(7)]
lines=[0 for x in range(7)]
for i in range(0,7):
ax[i]=f.add_subplot(4,2,1+i)
lines[i]=ax[0].plot([],[])
def update_line(hl, new_datax, new_datay):
hl.set_xdata(numpy.append(hl.get_xdata(), new_datax))
hl.set_ydata(numpy.append(hl.get_ydata(), new_datay))
plt.draw()
ser = serial.Serial('COM3', 115200) # Establish the connection on a specific port
counter = 0
byte=ser.readline() #first line not to be plotted
while True:
counter +=1
ser.write(b'9') # send a command to the arduino
byte=ser.read(7) #read 7 bytes back
for i in range(0,7):
update_line(lines[i][0], counter, byte[i]) # Trying to plot the new values to each different subplots
plt.pause(0.01)
sleep(.5) # Delay for one half of a second
But it still does not show anything. I kind of guess I am missing a plot and/or clear somewhere but after trying several options can't get it to work.