I'm attempting to plot a live (and continuous) feed of data that is being dumped into a csv from another program. I got the plot working but then realized my data only plotted until the time I executed the code. I believe there is something wrong with my parse function (kind of new at this stuff) but I can't figure out what.
import threading
import csv
import dateutil.parser
import datetime
import time
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import animation
log = "sample.csv"
data = ""
mytime = ""
thickness = ""
times = ""
#attempt at continuously reading my data source
def parser():
with open(log, 'r') as f:
global data
reader = csv.reader(f, delimiter=',')
data = reader.readlines()
time_thickness_data = []
while 1:
last_pos = f.tell()
next_line = f.readline()
if not next_line:
time.sleep(1)
f.seek(last_pos)
else:
mytime, thickness = splitter(next_line)
time_thickness_data.append([mytime, thickness])
def splitter(line):
mytime = line[0]
thickness = line[3]
return (mytime, thickness)
times = []
for date in mytime:
_ = dateutil.parser.parse(date)
times.append(datetime.datetime.strftime(_,'%H'))
def main():
a = threading.Thread(target=parser)
b = threading.Thread(target=splitter)
a.start()
b.start()
if __name__ == "__main__":
main()
#goes on to animated plot using times for x axis and thickness for y
fig = plt.figure()
axes = fig.add_subplot(111)
line, = axes.plot([], [], '.')
plt.show(block=False) #i had to use this to get my plot to show up
def init():
line.set_data([],[])
return line,
def animate(i):
a = 50 * i
xdata = times[:a]
ydata = thickness[:a]
line.set_data(xdata, ydata)
plt.draw()
axes.relim()
axes.autoscale_view(True,True,True)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,
interval=1, blit=True)
plt.show()
Sample row from csv looks like this:
2015-07-25 14:54:50.786774,1,0,439.85,,,,0,0,
mytime, thickness = splitter(next_line)does (1 param), when you declaredef splitter():(0 param)?