【pyqtgraph繪圖】案例-動態的正余弦波形圖


先看一個簡單的小例子:

完整代碼:

import numpy as np
import pyqtgraph as pg
import sys
from PyQt5.QtWidgets import QWidget,QApplication,QFrame,QGridLayout,QVBoxLayout

class Example(QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
        self.generate_image()

    def initUI(self):
        self.setGeometry(200,200,1000,800)
        self.setWindowTitle("實時刷新正余弦波形圖")
        self.gridLayout = QGridLayout(self)
        self.frame = QFrame(self)
        self.frame.setFrameShape(QFrame.Panel)
        self.frame.setFrameShadow(QFrame.Plain)
        self.frame.setLineWidth(2)
        self.frame.setStyleSheet("background-color:rgb(0,255,255);")
        self.gridLayout.addWidget(self.frame,0,0,1,2)
        self.setLayout(self.gridLayout)

    def generate_image(self):
        verticalLayout = QVBoxLayout(self.frame)
        win = pg.GraphicsLayoutWidget(self.frame)
        verticalLayout.addWidget(win)
        p = win.addPlot(title="動態波形圖")
        p.showGrid(x=True,y=True)
        p.setLabel(axis="left",text="Amplitude / V")
        p.setLabel(axis="bottom",text="t / s")
        p.setTitle("y = sin(x)")
        p.addLegend()
        # mkpen('y', width=3, style=QtCore.Qt.DashLine)
        self.curve1 = p.plot(pen=pg.mkPen("r",width=2),name="y1")  #設置pen 格式
        self.Fs = 1024.0 #采樣頻率
        self.N = 1024    #采樣點數
        self.f0 = 4.0    #信號頻率
        self.pha = 0     #初始相位
        self.t = np.arange(self.N) / self.Fs    #時間向量 1*1024的矩陣
        self.curve1.setData(self.t, np.sin(8 * np.pi * self.t + self.pha * np.pi / 180.0))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

實時刷新正余弦波形的例子

下面舉一個復雜一點的例子,這個例子在一個波形圖上實時連續刷新兩條曲線,分別為正弦曲線和余弦曲線,運行結果的動態圖如下:

效果如下:

完整代碼:

import numpy as np
import pyqtgraph as pg
import sys
from PyQt5.QtWidgets import QWidget,QApplication,QFrame,QGridLayout,QLabel,QPushButton,QVBoxLayout
from PyQt5.QtCore import Qt,QTimer

class Example(QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
        self.generate_image()

    def initUI(self):
        self.setGeometry(200,200,1000,800)
        self.setWindowTitle("實時刷新正余弦波形圖")
        self.gridLayout = QGridLayout(self)
        self.frame = QFrame(self)
        self.frame.setFrameShape(QFrame.Panel)
        self.frame.setFrameShadow(QFrame.Plain)
        self.frame.setLineWidth(2)
        self.frame.setStyleSheet("background-color:rgb(0,255,255);")
        self.label = QLabel(self)
        self.label.setText("正弦函數&余弦函數")
        self.label.setAlignment(Qt.AlignCenter)
        self.button = QPushButton(self)
        self.button.setText("生成波形圖")
        self.button.clicked.connect(self.btnClick)
        self.gridLayout.addWidget(self.frame,0,0,1,2)
        self.gridLayout.addWidget(self.label,1,0,1,1)
        self.gridLayout.addWidget(self.button,1,1,1,1)

        self.setLayout(self.gridLayout)

    def generate_image(self):
        verticalLayout = QVBoxLayout(self.frame)
        win = pg.GraphicsLayoutWidget(self.frame)
        verticalLayout.addWidget(win)
        p = win.addPlot(title="動態波形圖")
        p.showGrid(x=True,y=True)
        p.setLabel(axis="left",text="Amplitude / V")
        p.setLabel(axis="bottom",text="t / s")
        p.setTitle("y1 = sin(x)  y2 = cos(x)")
        p.addLegend()

        self.curve1 = p.plot(pen="r",name="y1")
        self.curve2 = p.plot(pen="g",name="y2")

        self.Fs = 1024.0 #采樣頻率
        self.N = 1024    #采樣點數
        self.f0 = 4.0    #信號頻率
        self.pha = 0     #初始相位
        self.t = np.arange(self.N) / self.Fs    #時間向量 1*1024的矩陣

    def plotData(self):
        self.pha += 10
        self.curve1.setData(self.t , np.sin(8 * np.pi  * self.t+ self.pha * np.pi/180.0))
        self.curve2.setData(self.t , np.cos(8 * np.pi  * self.t + self.pha * np.pi/180.0))

    def btnClick(self):
        self.button.setText("再次點擊加速!")
        timer = QTimer(self)
        timer.timeout.connect(self.plotData)
        timer.start(100)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM