Self-updating graphs over time with Python and matplotlib -
i'm not familiar matplotlib in python. want achieve plot data on time using text file receives new data every period.
the text file format following:
data,time
1,2015-07-05 11:20:00
what have far:
import matplotlib.pyplot plt import matplotlib.dates md import dateutil pulldata = open('sampletext.txt', 'r').read() dataarray = pulldata.split('\n') datestrings = [] plt_data = [] eachline in dataarray: if len(eachline)>1: y,x = eachline.split(',') plt_data.append(int(y)) datestrings.append(x) dates = [dateutil.parser.parse(s) s in datestrings] plt.subplots_adjust(bottom=0.2) plt.xticks( rotation=25 ) ax=plt.gca() ax.set_xticks(dates) xfmt = md.dateformatter('%m-%d %h:%m') ax.xaxis.set_major_formatter(xfmt) plt.plot(dates,plt_data, "o-") plt.show()
this pretty through different tutorials/previous questions.
as may see, code works plotting data on time, don't understand how can adapt in way graph update new data.
supposing text file being written to, , appended new data.
you can use inotify alerts when file modified , 'hang' function event update graph.
have @ this page, there several examples on how use pyinotify capture events. minimal example
provided, shows 'structure' of program:
import pyinotify # instantiate new watchmanager (will used store watches). wm = pyinotify.watchmanager() # associate watchmanager notifier (will used report , # process events). notifier = pyinotify.notifier(wm) # add new watch on /tmp all_events. wm.add_watch('/tmp', pyinotify.all_events) # loop forever , handle events. notifier.loop()
most importantly, pyinotify.all_events can changed listen events you're interested in (such file modifications, or creation if file not exist yet)
Comments
Post a Comment