main.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import matplotlib.pyplot as plt
  2. import json
  3. from matplotlib.animation import FuncAnimation
  4. import threading
  5. def read_data():
  6. try:
  7. return json.load(open("/tmp/values.pickle", 'r'))
  8. except:
  9. return (0.0, {})
  10. def write_data(values):
  11. json.dump(values, open("/tmp/values.pickle", 'w'))
  12. old_time, d = read_data()
  13. l = raw_input()
  14. time, key, value = l.split(" ")
  15. time = float(time)
  16. value = float(value)
  17. if time <= old_time:
  18. # Overwrites current values, so flush
  19. d = {}
  20. d.setdefault(key, []).append((time, value))
  21. fig, ax = plt.subplots()
  22. line, = ax.plot([], [])
  23. try:
  24. def update_data():
  25. while 1:
  26. l = raw_input()
  27. plt.clf()
  28. time, key, value = l.split(" ")
  29. time = float(time)
  30. value = float(value)
  31. d.setdefault(key, []).append((time, value))
  32. keys = []
  33. for key in d:
  34. plt.plot([i[0] for i in d[key]], [i[1] for i in d[key]])
  35. keys.append(key)
  36. plt.legend(keys)
  37. def update_value(i):
  38. pass
  39. thrd = threading.Thread(target=update_data)
  40. thrd.daemon = True
  41. thrd.start()
  42. ani = FuncAnimation(fig, update_value, interval=1)
  43. plt.show()
  44. except:
  45. print("Writing data...")
  46. write_data((time, d))