I have been trying to plot CSV stream (been weeks - because of my limited understanding on Python & programming).
Previously, I have asked a question here & thankfully I received the answer, however, I realised that using matplotlib (for loop) generated very slow fps. Here's the current code:
def follow(thefile):
thefile.seek(0,2) #file handling on the data stream
while True:
line = thefile.readline()
if not line:
time.sleep(0.1)
continue
yield line
logfile = open("C:/Users/r/Desktop/BL/wed10.csv", "r")
logrecords = []
Nmax = 2
fig, ax = plt.subplots()
dataSet = []
tstart= time.time()
for record in follow(logfile):
logrecords.append(record)
try:
values = [float(value) if value else 0.0 for value in record.split(',')]
values2 = [val2 if val2<1.0 else 0.0 for val2 in values]
except ValueError:
continue # read another record
ax.plot(values2, color='black', label="%02d'%.2fs"%divmod(time.time()%3600, 60))
plt.legend()
if len(ax.lines) == Nmax : ax.lines[0].remove()
plt.pause(0.00001)
It works, but very slow. I am thinking to use FuncAnimation/ArtistAnimation
instead, but I am not sure how to combine it with stream csv.
I would like to plot each row (1 plot for each row) & animated to another row.
How can I modify the 'for loop plotting' to FuncAnimation or ArtistAnimation? Thank you.