main.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. print("Wait for input")
  29. l = raw_input()
  30. print("Plotting values: " + l)
  31. if l == "CLOSE":
  32. import sys
  33. sys.exit(0)
  34. elif l == "ALGEBRAIC_LOOP":
  35. print("Algebraic loop discovered...")
  36. continue
  37. time, key, value = l.split(" ")
  38. time = float(time)
  39. value = float(value)
  40. if key not in d:
  41. maps[key], = plt.plot([], [])
  42. maps[key].set_label(key)
  43. plt.legend()
  44. d[key] = ([], [])
  45. print("Update")
  46. d[key][0].append(time)
  47. d[key][1].append(value)
  48. maps[key].set_xdata(d[key][0])
  49. maps[key].set_ydata(d[key][1])
  50. print("Drawing")
  51. plt.draw()
  52. write_data((0.0, d))