怎么在Pyqt中使用matplotlib實時畫圖案?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習下,希望你能有所收獲。
為襄州等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及襄州網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、襄州網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!Python 3.7 + Matplotlib 3.0.2 + PyQt 5
import sys import time import numpy as np from matplotlib.backends.qt_compat import QtCore, QtWidgets, is_pyqt5 if is_pyqt5(): from matplotlib.backends.backend_qt5agg import ( FigureCanvas, NavigationToolbar2QT as NavigationToolbar) else: from matplotlib.backends.backend_qt4agg import ( FigureCanvas, NavigationToolbar2QT as NavigationToolbar) from matplotlib.figure import Figure class ApplicationWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self._main = QtWidgets.QWidget() self.setCentralWidget(self._main) layout = QtWidgets.QVBoxLayout(self._main) static_canvas = FigureCanvas(Figure(figsize=(5, 3))) layout.addWidget(static_canvas) self.addToolBar(NavigationToolbar(static_canvas, self)) dynamic_canvas = FigureCanvas(Figure(figsize=(5, 3))) layout.addWidget(dynamic_canvas) self.addToolBar(QtCore.Qt.BottomToolBarArea, NavigationToolbar(dynamic_canvas, self)) self._static_ax = static_canvas.figure.subplots() t = np.linspace(0, 10, 501) self._static_ax.plot(t, np.tan(t), ".") self._dynamic_ax = dynamic_canvas.figure.subplots() self._timer = dynamic_canvas.new_timer( 100, [(self._update_canvas, (), {})]) self._timer.start() def _update_canvas(self): self._dynamic_ax.clear() t = np.linspace(0, 10, 101) # Shift the sinusoid as a function of time. self._dynamic_ax.plot(t, np.sin(t + time.time())) self._dynamic_ax.figure.canvas.draw() if __name__ == "__main__": qapp = QtWidgets.QApplication(sys.argv) app = ApplicationWindow() app.show() qapp.exec_()
上圖中的散點為靜止的,下面的圖為動態(tài)的,類似行波,一直在行走,是應(yīng)為用了**self._dynamic_ax.plot(t, np.sin(t + time.time()))**函數(shù),但是這個和我想得實時畫圖不太一樣,在項目中要根據(jù)生成的數(shù)據(jù)實時繪圖,因此x軸的元素和y軸的元素個數(shù)是逐漸增加的。
通過閱讀上述 _update_canvas 函數(shù)代碼以及 dynamic_canvas.new_timer 可以使得每次調(diào)用_update_canvas是的相應(yīng)的x的元素和y軸的元素增加更改后的代碼如下:
import sys import time import numpy as np from matplotlib.backends.qt_compat import QtCore, QtWidgets, is_pyqt5 if is_pyqt5(): from matplotlib.backends.backend_qt5agg import ( FigureCanvas, NavigationToolbar2QT as NavigationToolbar) else: from matplotlib.backends.backend_qt4agg import ( FigureCanvas, NavigationToolbar2QT as NavigationToolbar) from matplotlib.figure import Figure class ApplicationWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self._main = QtWidgets.QWidget() self.setCentralWidget(self._main) layout = QtWidgets.QVBoxLayout(self._main) static_canvas = FigureCanvas(Figure(figsize=(5, 3))) layout.addWidget(static_canvas) self.addToolBar(NavigationToolbar(static_canvas, self)) dynamic_canvas = FigureCanvas(Figure(figsize=(5, 3))) layout.addWidget(dynamic_canvas) self.addToolBar(QtCore.Qt.BottomToolBarArea, NavigationToolbar(dynamic_canvas, self)) self._static_ax = static_canvas.figure.subplots() t = np.linspace(0, 10, 501) self._static_ax.plot(t, np.tan(t), ".") self.x = [] #建立空的x軸數(shù)組和y軸數(shù)組 self.y = [] self.n = 0 self._dynamic_ax = dynamic_canvas.figure.subplots() self._timer = dynamic_canvas.new_timer( 100, [(self._update_canvas, (), {})]) self._timer.start() def _update_canvas(self): self.n += 1 if self.n == 200: #畫200個點就停止,根據(jù)實際情況確定終止條件 self._timer.stop() self._dynamic_ax.clear() self.x.append(np.pi/100*self.n) #x加入一個值,后一個值比前一個大pi/100 xx = np.array(self.x) # t = np.linspace(0, 10, 101) # Shift the sinusoid as a function of time. self._dynamic_ax.plot(xx, np.sin(xx)) self._dynamic_ax.set_xlim(0,7) self._dynamic_ax.set_ylim(-1,1) self._dynamic_ax.figure.canvas.draw() if __name__ == "__main__": qapp = QtWidgets.QApplication(sys.argv) app = ApplicationWindow() app.show() qapp.exec_()
上面的圖仍然靜止,下面的可以實時顯示
補充:pyqtgraph實時繪圖出現(xiàn)無法刷新問題
pyqtgraph實時繪圖時,會概率出現(xiàn)無法實時刷新繪制圖,原因是
while True: ...... update() # 通過 plotitem.setData()更新數(shù)據(jù) ......
這里使用的是while循環(huán),不斷的更新數(shù)據(jù)概率出現(xiàn)繪圖不刷新和操作不響應(yīng)(最小化操作會高概率出現(xiàn)該問題)
我使用的是PlotWidget,remove后再addwidget,然后再重新繪制
不使用while循環(huán),使用QTime定時器
t = QTimer() t.timeout.connect(self.update) t.start(10)
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。