1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import matplotlib.pyplot as plt
- import json
- from matplotlib.animation import FuncAnimation
- import threading
- def read_data():
- try:
- return json.load(open("/tmp/values.pickle", 'r'))
- except:
- return (0.0, {})
- def write_data(values):
- json.dump(values, open("/tmp/values.pickle", 'w'))
- plt.ion()
- plt.figure()
- old_time, d = read_data()
- l = raw_input()
- time, _ = l.split(" ", 1)
- time = float(time)
- maps = {}
- if time <= old_time:
- # Overwrites current values, so flush
- d = {}
- else:
- for key in d:
- maps[key], = plt.plot(d[key][0], d[key][1])
- maps[key].set_label(key)
- plt.legend()
- first = l
- while 1:
- if first is not None:
- l = first
- first = None
- else:
- l = raw_input()
- if l == "CLOSE":
- import sys
- sys.exit(0)
- elif l == "ALGEBRAIC_LOOP":
- print("Algebraic loop discovered...")
- continue
- time, key, value = l.split(" ")
- time = float(time)
- value = float(value)
- if key not in maps:
- maps[key], = plt.plot([], [])
- maps[key].set_label(key)
- plt.legend()
- if key not in d:
- d[key] = ([], [])
- d[key][0].append(time)
- d[key][1].append(value)
- maps[key].set_xdata(d[key][0])
- maps[key].set_ydata(d[key][1])
- plt.gca().set_xlim([min(d[key][0]), max(d[key][0])])
- plt.draw()
- write_data((0.0, d))
|