main.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. maps = {}
  19. if time <= old_time:
  20. # Overwrites current values, so flush
  21. d = {}
  22. else:
  23. for key in d:
  24. maps[key], = plt.plot(d[key][0], d[key][1])
  25. maps[key].set_label(key)
  26. plt.legend()
  27. first = l
  28. while 1:
  29. if first is not None:
  30. l = first
  31. first = None
  32. else:
  33. l = raw_input()
  34. if l == "CLOSE":
  35. import sys
  36. sys.exit(0)
  37. elif l == "ALGEBRAIC_LOOP":
  38. print("Algebraic loop discovered...")
  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.draw()
  55. write_data((0.0, d))