from time import sleep import matplotlib.pyplot as plotter import threading import random import matplotlib.animation as animation class WorkerThreadClass(threading.Thread): _mutex = threading.RLock() x_value_count = 11 dynamic_array = {} dynamic_array['x_value_array'] = [0,1,2,3,4,5,6,7,8,9,10] dynamic_array['x_value_axis_labels'] = ["A", "B", "C", "D", "E", "F", "G","H","I","J","K"] dynamic_array['y_value_array'] = [1,2,4,8,16,32,64,128,256,512,1024] dynamic_array['y_value_axis_labels'] = ["A", "B", "C", "D", "E", "F", "G","H","I","J","K"] def __init__(self, subplot): self.subplot = subplot self.stop_flag = "" threading.Thread.__init__(self) subplot.clear() with self._mutex: subplot.set_xticks(range(len(self.dynamic_array['x_value_axis_labels'])), self.dynamic_array['x_value_axis_labels']) subplot.set_yticks(range(len(self.dynamic_array['y_value_axis_labels'])), self.dynamic_array['y_value_axis_labels']) subplot.plot(self.dynamic_array['x_value_array'], self.dynamic_array['y_value_array']) # End With # End Function def run(self): while(self.stop_flag != "stop"): sleep(1) with self._mutex: self.dynamic_array['x_value_array'].pop(0) self.dynamic_array['y_value_array'].pop(0) new_y_value = random.random() * 80 self.dynamic_array['x_value_array'].append(self.x_value_count) self.dynamic_array['y_value_array'].append(new_y_value) # End With self.x_value_count = self.x_value_count + 1 # End While # End Function def interval_action(self, i): subplot.clear() with self._mutex: self.subplot.set_xticks(range(len(self.dynamic_array['x_value_axis_labels'])), self.dynamic_array['x_value_axis_labels']) self.subplot.set_yticks(range(len(self.dynamic_array['y_value_axis_labels'])), self.dynamic_array['y_value_axis_labels']) self.subplot.plot(self.dynamic_array['x_value_array'], self.dynamic_array['y_value_array']) # End With # End Function # End Class #################################################################################### class CommandInterfaceThread(threading.Thread): def __init__(self, plotter_thread): threading.Thread.__init__(self) self.plotter_thread = plotter_thread #End Function def run(self): stop_flag = "" while(stop_flag != "stop"): stop_flag = raw_input() self.plotter_thread.stop_flag = stop_flag # End While #End Function # End Class #################################################################################### plot_figure = plotter.figure() subplot = plot_figure.add_subplot(1,1,1) worker_thread = WorkerThreadClass(subplot) command_interface_thread = CommandInterfaceThread(worker_thread) command_interface_thread.start() worker_thread.start() interval = animation.FuncAnimation(plot_figure, worker_thread.interval_action, interval=50) plotter.show()