I am trying to plot data in realtime using matplotlib. So far I have only succeeded in showing a static figure window and in frustrating myself. I tried many minor changes based on other answer here but none seem to work. My code looks something as follows. Any help is appreciated.
import matplotlib.pyplot as plt
import numpy as np
class Plotter():
def __init__(self):
self.error = []
self.fig, self.ax = plt.subplots()
self.line, = self.ax.plot(range(len(self.error)), self.error)
def start_plot(self):
plt.ion()
def end_plot(self):
self.error = []
def update(self, worker):
self.error.append(worker.GetMetricValue())
self.line.set_ydata(self.error)
return self.line
def update_plot(self, worker):
self.error.append(worker.Get_metric_value())
self.ax.set_ylim(np.min(self.error)*1.1, (np.max(self.error)+0.1)*1.1)
self.line.set_ydata(self.error)
self.line.set_xdata(range(len(self.error)))
self.fig.canvas.draw()
plt.pause(0.1)
#self.fig.canvas.flush_events()
def get_error(self):
return self.error
class WorkerClass():
def __init__(self):
pass
def Get_metric_value(self):
return np.random.rand()
def main():
worker = WorkerClass()
pltr = Plotter()
pltr.start_plot()
for i in range(10):
print("iteration {}".format(i))
pltr.update_plot(worker)
if __name__ == '__main__':
main()