main.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. plt.ion()
  13. plt.figure()
  14. old_time, d = read_data()
  15. l = raw_input()
  16. time, _ = l.split(" ", 1)
  17. time = float(time)
  18. if time <= old_time:
  19. # Overwrites current values, so flush
  20. d = {}
  21. first = l
  22. maps = {}
  23. while 1:
  24. if first is not None:
  25. l = first
  26. first = None
  27. else:
  28. l = raw_input()
  29. if l == "CLOSE":
  30. import sys
  31. sys.exit(0)
  32. elif l == "ALGEBRAIC_LOOP":
  33. print("Algebraic loop discovered...")
  34. continue
  35. time, key, value = l.split(" ")
  36. time = float(time)
  37. value = float(value)
  38. if key not in d:
  39. maps[key], = plt.plot([], [])
  40. maps[key].set_label(key)
  41. plt.legend()
  42. d[key] = ([], [])
  43. d[key][0].append(time)
  44. d[key][1].append(value)
  45. maps[key].set_xdata(d[key][0])
  46. maps[key].set_ydata(d[key][1])
  47. plt.draw()
  48. write_data((0.0, d))