浏览代码

Listen to a CLOSE and ALGEBRAIC_LOOP message as well

Yentl Van Tendeloo 7 年之前
父节点
当前提交
7417180f6d
共有 1 个文件被更改,包括 41 次插入36 次删除
  1. 41 36
      interface/simple_plot/main.py

+ 41 - 36
interface/simple_plot/main.py

@@ -12,47 +12,52 @@ def read_data():
 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, key, value = l.split(" ")
+time, _ = l.split(" ", 1)
 time = float(time)
-value = float(value)
 
 if time <= old_time:
     # Overwrites current values, so flush
     d = {}
 
-d.setdefault(key, []).append((time, value))
-
-fig, ax = plt.subplots()
-line, = ax.plot([], [])
-
-try:
-    def update_data():
-        while 1:
-            l = raw_input()
-            plt.clf()
-            time, key, value = l.split(" ")
-            time = float(time)
-            value = float(value)
-            d.setdefault(key, []).append((time, value))
-
-            keys = []
-            for key in d:
-                plt.plot([i[0] for i in d[key]], [i[1] for i in d[key]])
-                keys.append(key)
-            plt.legend(keys)
-
-    def update_value(i):
-        pass
-
-    thrd = threading.Thread(target=update_data)
-    thrd.daemon = True
-    thrd.start()
-
-    ani = FuncAnimation(fig, update_value, interval=1)
-    plt.show()
-
-except:
-    print("Writing data...")
-    write_data((time, d))
+first = l
+
+maps = {}
+
+while 1:
+    if first is not None:
+        l = first
+        first = None
+    else:
+        print("Wait for input")
+        l = raw_input()
+
+    print("Plotting values: " + l)
+    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 d:
+        maps[key], = plt.plot([], [])
+        maps[key].set_label(key)
+        plt.legend()
+        d[key] = ([], [])
+
+    print("Update")
+    d[key][0].append(time)
+    d[key][1].append(value)
+    maps[key].set_xdata(d[key][0])
+    maps[key].set_ydata(d[key][1])
+    print("Drawing")
+    plt.draw()
+    write_data((0.0, d))