您好,登錄后才能下訂單哦!
怎么在Pyqt中使用matplotlib實時畫圖案?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
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_()
上圖中的散點為靜止的,下面的圖為動態的,類似行波,一直在行走,是應為用了**self._dynamic_ax.plot(t, np.sin(t + time.time()))**函數,但是這個和我想得實時畫圖不太一樣,在項目中要根據生成的數據實時繪圖,因此x軸的元素和y軸的元素個數是逐漸增加的。
通過閱讀上述 _update_canvas 函數代碼以及 dynamic_canvas.new_timer 可以使得每次調用_update_canvas是的相應的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軸數組和y軸數組 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個點就停止,根據實際情況確定終止條件 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實時繪圖出現無法刷新問題
pyqtgraph實時繪圖時,會概率出現無法實時刷新繪制圖,原因是
while True: ...... update() # 通過 plotitem.setData()更新數據 ......
這里使用的是while循環,不斷的更新數據概率出現繪圖不刷新和操作不響應(最小化操作會高概率出現該問題)
我使用的是PlotWidget,remove后再addwidget,然后再重新繪制
不使用while循環,使用QTime定時器
t = QTimer() t.timeout.connect(self.update) t.start(10)
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。