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')) old_time, d = read_data() l = raw_input() time, key, value = l.split(" ") 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))