我正在尝试使用matplotlib实时绘制数据。到目前为止,我仅成功显示了静态图形窗口并让自己感到沮丧。我根据此处的其他答案尝试了许多细微的更改,但似乎没有任何效果。我的代码如下所示。任何帮助表示赞赏。
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()
答案 0 :(得分:0)
这是该代码的更正版本。我建议对动画使用FuncAnimation
,因为它更稳定并且可以在图形的事件循环中运行。
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
class Plotter():
def __init__(self):
self.worker = WorkerClass()
self.error = []
self.fig, self.ax = plt.subplots()
self.line, = self.ax.plot(range(len(self.error)), self.error)
def start_plot(self):
self.ani = FuncAnimation(self.fig, self.update_plot, frames=10,
interval=100, repeat=False)
plt.show()
def end_plot(self):
self.error = []
def update_plot(self, i):
self.error.append(self.worker.Get_metric_value())
self.line.set_data(range(len(self.error)), self.error)
self.ax.relim()
self.ax.autoscale_view()
def get_error(self):
return self.error
class WorkerClass():
def __init__(self):
pass
def Get_metric_value(self):
return np.random.rand()
def main():
pltr = Plotter()
pltr.start_plot()
if __name__ == '__main__':
main()