main.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import sys
  2. import matplotlib.pyplot as plt
  3. import json
  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. plt.pause(0.01)
  16. l = sys.stdin.readline()
  17. time, _ = l.split(" ", 1)
  18. time = float(time)
  19. maps = {}
  20. if time <= old_time:
  21. # Overwrites current values, so flush
  22. d = {}
  23. else:
  24. for key in d:
  25. maps[key], = plt.plot(d[key][0], d[key][1])
  26. maps[key].set_label(key)
  27. plt.legend()
  28. first = l
  29. while 1:
  30. if first is not None:
  31. l = first
  32. first = None
  33. else:
  34. l = sys.stdin.readline()
  35. if l == "CLOSE":
  36. import sys
  37. sys.exit(0)
  38. elif l == "ALGEBRAIC_LOOP":
  39. continue
  40. time, key, value = l.split(" ")
  41. time = float(time)
  42. value = float(value)
  43. if key not in maps:
  44. maps[key], = plt.plot([], [])
  45. maps[key].set_label(key)
  46. plt.legend()
  47. if key not in d:
  48. d[key] = ([], [])
  49. d[key][0].append(time)
  50. d[key][1].append(value)
  51. maps[key].set_xdata(d[key][0])
  52. maps[key].set_ydata(d[key][1])
  53. plt.gca().set_xlim([min(d[key][0]), max(d[key][0])])
  54. plt.pause(0.01)
  55. write_data((0.0, d))