需求分析:
項(xiàng)目中根據(jù)測(cè)得的數(shù)據(jù)在界面上實(shí)時(shí)繪制
運(yùn)行環(huán)境:
Python 3.7 + Matplotlib 3.0.2 + PyQt 5
matplot官網(wǎng)給的相應(yīng)的例子:
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_()
上圖中的散點(diǎn)為靜止的,下面的圖為動(dòng)態(tài)的,類(lèi)似行波,一直在行走,是應(yīng)為用了**self._dynamic_ax.plot(t, np.sin(t + time.time()))**函數(shù),但是這個(gè)和我想得實(shí)時(shí)畫(huà)圖不太一樣,在項(xiàng)目中要根據(jù)生成的數(shù)據(jù)實(shí)時(shí)繪圖,因此x軸的元素和y軸的元素個(gè)數(shù)是逐漸增加的。
通過(guò)閱讀上述 _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: #畫(huà)200個(gè)點(diǎn)就停止,根據(jù)實(shí)際情況確定終止條件
self._timer.stop()
self._dynamic_ax.clear()
self.x.append(np.pi/100*self.n) #x加入一個(gè)值,后一個(gè)值比前一個(gè)大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_()
上面的圖仍然靜止,下面的可以實(shí)時(shí)顯示
補(bǔ)充:pyqtgraph實(shí)時(shí)繪圖出現(xiàn)無(wú)法刷新問(wèn)題
pyqtgraph實(shí)時(shí)繪圖時(shí),會(huì)概率出現(xiàn)無(wú)法實(shí)時(shí)刷新繪制圖,原因是
while True:
......
update() # 通過(guò) plotitem.setData()更新數(shù)據(jù)
......
這里使用的是while循環(huán),不斷的更新數(shù)據(jù)概率出現(xiàn)繪圖不刷新和操作不響應(yīng)(最小化操作會(huì)高概率出現(xiàn)該問(wèn)題)
解決方法1:
我使用的是PlotWidget,remove后再addwidget,然后再重新繪制
解決方法2:
不使用while循環(huán),使用QTime定時(shí)器
t = QTimer()
t.timeout.connect(self.update)
t.start(10)
兩種方法都可以解決這個(gè)問(wèn)題,推薦方法2
據(jù)說(shuō)使用while循環(huán),需要在更新數(shù)據(jù)之后調(diào)用pg.QtGui.QApplication.processEvents()才能確保正常,這個(gè)本人試了不行,可能是我這邊的原因吧
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- python學(xué)習(xí)之使用Matplotlib畫(huà)實(shí)時(shí)的動(dòng)態(tài)折線圖的示例代碼
- python中plot實(shí)現(xiàn)即時(shí)數(shù)據(jù)動(dòng)態(tài)顯示方法
- 利用matplotlib實(shí)現(xiàn)根據(jù)實(shí)時(shí)數(shù)據(jù)動(dòng)態(tài)更新圖形