【发布时间】:2017-01-19 05:00:51
【问题描述】:
我正在从设备中获取数据并想要绘制它的电压,并将该图嵌入到 UI 中。我在这里使用了示例:http://matplotlib.org/examples/user_interfaces/embedding_in_qt4.html
这个例子运行良好,但是当我添加 2 个或更多图表时,整个 UI 变得非常慢(使用 RPi3)并且 CPU 使用率非常高。我意识到这可能是因为图表不断被清除和重新绘制。
我的代码如下所示:
class MyMplCanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=2, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
self.compute_initial_figure()
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
def compute_initial_figure(self):
pass
class MyDynamicMplCanvas(MyMplCanvas):
def __init__(self, *args, **kwargs):
MyMplCanvas.__init__(self, *args, **kwargs)
def compute_initial_figure(self):
self.axes.cla()
def update_figure(self,voltage):
self.axes.cla()
self.axes.plot(np.linspace(0,len(voltage)-1,num = len(voltage)), voltage, 'b')
self.axes.set_xlabel("Time")
self.draw()
class worker_thread(QThread):
... #worker thread stuff here
class Monitor(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.exit_button.clicked.connect(exit)
self.ui.go_button.clicked.connect(self.start_monitoring)
self.ui.print_button.clicked.connect(self.test_print)
self.ac_graph = QtGui.QWidget(self)
self.ac_1_graph = MyDynamicMplCanvas(self.ac_graph,width = 10, height =3 , dpi = 60)
self.ui.mplvl.addWidget(self.ac_1_graph)
self.ac_1_graph.axes.set_xlabel("Time")
self.dc_graph = QtGui.QWidget(self)
self.dc_2_graph = MyDynamicMplCanvas(self.dc_graph,width = 10, height =3 , dpi = 60)
self.ui.mplvl_2.addWidget(self.dc_2_graph)
self.ac1_voltage_values = []
self.ac1_current_values = []
self.dc2_voltage_values = []
def start_monitoring(self):
self.worker_thread = worker_thread()
self.connect(self.worker_thread,SIGNAL('grid_done'), self.update_ac_dc_info)
def update_plot_values(self, y_value, y_list):
y_list.append(y_value)
if (len(y_list) == 61):
del y_list[0]
return y_list
def update_ac_dc_info(self,grid_info):
self.ac1_voltage_values = self.update_plot_values((grid_info['ac1_voltage']/10),self.ac1_voltage_values)
self.ac_1_graph.update_figure(self.ac1_voltage_values)
基本上,当数据从我的设备返回时,我会从 worker_thread 发出一个信号,触发我的 UI 在主线程和绘图中更新。在这一点上,我如何让 matplotlib 只接受新出现的点而不重新绘制整个事情?我读过的许多示例都使用了我无法使用的 pyplot,因为我需要将其嵌入到现有的 UI 中。
【问题讨论】:
标签: python qt matplotlib pyqt