欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

示例詳解pyqtgraph繪制實時更新數(shù)據(jù)的圖

 更新時間:2024年12月12日 10:48:32   作者:mj412828668  
PyQtGraph是一個基于PyQt和NumPy的Python庫,它專為實時數(shù)據(jù)可視化而設(shè)計,本文通過實例代碼給大家介紹pyqtgraph繪制實時更新數(shù)據(jù)的圖,感興趣的朋友一起看看吧

        PyQtGraph是一個基于PyQt和NumPy的Python庫,它專為實時數(shù)據(jù)可視化而設(shè)計。以繪制0~2π范圍的y=sin(x)為例,基本用法的代碼如下:

# coding=utf-8
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
# pyqtgraph中的顏色表示,blue:b,green:g,red:r,cyan:c,magenta:m,yellow:y,black:k,white:w
# pyqtgraph中的顏色表示也可用十六進(jìn)制,如白色'#FFFFFF'
# 字體大小的單位有px(pixel,像素)和pt(point,點),px適用于屏幕顯示,其大小是動態(tài)的,而pt適用于印刷,大小是固定的。
def update():
    global x, y
    y = np.roll(y, -1)  # 滾動數(shù)組,模擬數(shù)據(jù)更新
    curve.setData(x, y)  # 更新曲線數(shù)據(jù)
def mouseover(pos):
    global win
    act_pos = curve.mapFromScene(pos)
    if type(act_pos) != QtCore.QPointF:
        return
    # print(act_pos.x(), act_pos.y())
    win.setWindowTitle("(x = {:.4f}, y = {:.4f})".format(act_pos.x(), act_pos.y()))
if __name__ == '__main__':
    # 創(chuàng)建應(yīng)用
    app = QtGui.QApplication([])
    # 設(shè)置窗口界面屬性
    win = pg.GraphicsLayoutWidget(show=True, title="實時數(shù)據(jù)更新示例")    # 設(shè)置程序界面的標(biāo)題
    win.resize(800, 600)    # 設(shè)置程序界面的窗口大小
    # win.setBackground("b")    # 設(shè)置背景顏色為藍(lán)色。默認(rèn)背景是黑色,background='default'
    # 添加繪圖區(qū)域
    plot = win.addPlot(title="實時更新的正弦波形")    # 設(shè)置繪圖的標(biāo)題
    plot.showGrid(x=True, y=True)     # x軸和y軸都顯示網(wǎng)格
    x_styles = {'color': 'r', 'font-size': '30px', 'units': 'radian'}  # 設(shè)置標(biāo)簽文字、顏色、字體大小、單位
    y_styles = {'color': 'r', 'font-size': '30px'}    # 設(shè)置標(biāo)簽文字、顏色、字體大小
    plot.setLabel('left', 'y=sin(x)',  **y_styles)    # 左邊坐標(biāo)軸,Y軸
    plot.setLabel('bottom', 'x', **x_styles)    # 下邊坐標(biāo)軸,X軸
    plot.getAxis('left').setPen('#0000FF')    # 坐標(biāo)軸上色
    plot.getAxis('bottom').setPen('#0000FF')    # 坐標(biāo)軸上色
    plot.setXRange(min=0, max=7)    # 設(shè)置X軸刻度范圍
    plot.setYRange(min=-1.2, max=1.2)    # 設(shè)置Y軸刻度范圍
    plot.setMouseEnabled(x=False, y=False)    # 設(shè)置禁用鼠標(biāo)交互,默認(rèn)是啟用的
    curve = plot.plot(pen="y")   # 創(chuàng)建一條黃色的線
    # 數(shù)據(jù)初始化
    x = np.linspace(0, 2 * np.pi, 500)
    y = np.sin(x)
    # 綁定鼠標(biāo)移動的信號和槽, 移動鼠標(biāo)時就在窗口標(biāo)題上顯示當(dāng)前鼠標(biāo)停留位置的坐標(biāo)
    curve.scene().sigMouseMoved.connect(mouseover)
    # 設(shè)置定時器,每20毫秒更新一次,相當(dāng)于FPS=50
    timer = QtCore.QTimer()
    timer.timeout.connect(update)
    timer.start(20)    # 每20毫秒更新一次
    # 開始事件循環(huán)
    QtGui.QApplication.instance().exec_()

運行效果截圖如下:

到此這篇關(guān)于pyqtgraph繪制實時更新數(shù)據(jù)的圖的文章就介紹到這了,更多相關(guān)pyqtgraph實時更新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論