Python3.x+pyqtgraph实现数据可视化教程
1、pyqtgraph库数据可视化效果还不错,特别是窗体程序中图像交互性较好;安装也很⽅便,⽤ pip 安装。
2、在Python中新建⼀个 .py ⽂件,然后写⼊如下代码并执⾏可以得到官⽅提供的很多案例(含代码),出现如下界⾯图像:
amples
图1
图2
图3
4、程序默认是⿊⾊背景,这个是可以修改的。⽐如,在程序的开头部分写⼊如下代码就可以修改背景:
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
5、⼀个修改背景颜⾊的完整案例如下,可以直接运⾏程序:
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
# 如下2⾏代码是我⾃⼰加⼊的,⽬的是修改默认的⿊⾊背景为其它颜⾊背景
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
from pyqtgraph.Point import Point
#generate layout
app = QtGui.QApplication([])
win = pg.GraphicsWindow()
win.setWindowTitle('pyqtgraph example: crosshair')
label = pg.LabelItem(justify='right')
win.addItem(label)
p1 = win.addPlot(row=1, col=0)
p2 = win.addPlot(row=2, col=0)
region = pg.LinearRegionItem()
region.setZValue(10)
# Add the LinearRegionItem to the ViewBox, but tell the ViewBox to exclude this
# item when doing auto-range calculations.
p2.addItem(region, ignoreBounds=True)
#pg.dbg()
p1.setAutoVisible(y=True)
#create numpy arrays
#make the numbers large to show that the xrange shows data from 10000 to all the way 0
data1 = 10000 + 15000 * pg.gaussianFilter(np.random.random(size=10000), 10) + 3000 * np.random.random(size=10000) data2 = 15000 + 15000 * pg.gaussianFilter(np.random.random(size=10000), 10) + 3000 * np.random.random(size=10000)
p1.plot(data1, pen="r")
p1.plot(data2, pen="g")
p2.plot(data1, pen="w")
def update():
region.setZValue(10)
minX, maxX = Region()
p1.setXRange(minX, maxX, padding=0)
t(update)
def updateRegion(window, viewRange):
rgn = viewRange[0]
region.setRegion(rgn)
t(updateRegion)
region.setRegion([1000, 2000])
#cross hair
vLine = pg.InfiniteLine(angle=90, movable=False)
hLine = pg.InfiniteLine(angle=0, movable=False)
p1.addItem(vLine, ignoreBounds=True)
p1.addItem(hLine, ignoreBounds=True)
vb = p1.vb
def mouseMoved(evt):
pos = evt[0] ## using signal proxy turns original arguments into a tuple
if p1.sceneBoundingRect().contains(pos):
mousePoint = vb.mapSceneToView(pos)
index = int(mousePoint.x())
if index > 0 and index < len(data1):
label.setText("<span style='font-size: 12pt'>x=%0.1f, <span style='color: red'>y1=%0.1f</span>, <span style='color: green'>y2=%0.1f</span>" % (mousePoint.x(), data1[index], data2[index]))  vLine.setPos(mousePoint.x())
hLine.setPos(mousePoint.y())
proxy = pg.SignalProxy(p1.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved)
#p1.scene().t(mouseMoved)
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
知识补充:python图形化实例分享--pyqt5与pyqtgraph嵌⼊绘图
序⾔
之前也写过⼀些图形化界⾯的程序,基本上都是⽤wxPython写的,确实简单粗暴易上⼿。这次的任务是要写⼀个绘图的程序,wx模块就显得不太友好了,我
就去⽹上了⼀些资料,发现PyQtGraph画这种K线图、波形图等图形真是太简单了,更多的关于wx、qt等模块的细节学习可以看我后⾯的参考资料,我这⾥
就分享⼀下我本程序的⼼得,和对有些⽅法使⽤上⾃⼰的理解
项⽬开始
引⽤头⽂件
pyqt5_draw_1 这是主程序⽂件,负责主窗⼝图形化界⾯
import sys # 与PyQt5配合使⽤
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QGridLayout,
QHBoxLayout, QVBoxLayout,QLabel, QComboBox,QPushButton,
QDateEdit, QSpacerItem,QFrame, QSizePolicy, QSplitter,
QRadioButton, QGroupBox,QCheckBox,QLineEdit, QAction)
# 上⾯是QT图形化要引⽤的所有包
from PyQt5.QtCore import Qt, QDate, QRect # 对齐、时间等
from PyQt5.QtCore import QThread, pyqtSignal # 多线程管理
import pyqtgraph as pg # 绘图包
from Tmp_Data import * # ⾃定义⽂件,下⾯有介绍
from Mythreading import * # ⾃定义⽂件,下⾯有介绍
from pyqt5_graph import * # ⾃定义⽂件,下⾯有介绍
如果PyQt5、pyqtgraph未安装的,最简单的安装⽅式就⽤python⾃带的pip⼯具安装,如果没有pip的或不会安装可直接百度
c:\> pip install PyQt5 pyqtgraph
图形化主界⾯搭建
# pyqt5_draw_1.py ⽂件名
import sys
import cgitb
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QGridLayout,
QHBoxLayout, QVBoxLayout,QLabel, QComboBox,QPushButton,
QDateEdit, QSpacerItem,QFrame, QSizePolicy, QSplitter,
QRadioButton, QGroupBox,QCheckBox,QLineEdit, QAction)
from PyQt5.QtCore import Qt, QDate, QRect
from TmpData import *
from Mythreading import *
from pyqt5_graph import *
class Qt_Test_Frame(QMainWindow):
Items = []
def __init__(self):
#super(Qt_Test_Frame, self).__init__(*args, **kw)
super().__init__()
# 初始化界⾯
self._initUI()
self.show()
def _initUI(self):
self.setWindowTitle("QT图形界⾯测试")
wwg = QWidget()
# 全局布局
wlayout = QVBoxLayout()
h1_wlayout = QHBoxLayout()
h2_wlayout = QHBoxLayout()
h3_wlayout = QHBoxLayout()
v4_wlayout = QVBoxLayout()
v5_wlayout = QVBoxLayout()
self.statusBar().showMessage("状态栏")
# 第⼀层
self._frist_story(h1_wlayout)
# 第⼆层
self._second_story(h2_wlayout)
# 第三层左
self._third_left(v4_wlayout, v5_wlayout)
# 第三层右
self._fouth_right(v5_wlayout)
# 加载
splt = self._my_line()
splt2 = self._my_line(False)
wlayout.addSpacing(10) # 增加布局间距
wlayout.addLayout(h1_wlayout)
wlayout.addSpacing(10) # 增加布局间距
wlayout.addLayout(h2_wlayout)
wlayout.addSpacing(10) # 增加布局间距
wlayout.addWidget(splt)
wlayout.addLayout(h3_wlayout)
wlayout.addWidget(self.statusBar())
h3_wlayout.addLayout(v4_wlayout, 0)
h3_wlayout.addWidget(splt2)
h3_wlayout.addLayout(v5_wlayout, 2)
#wlayout.setAlignment(Qt.AlignTop)
wwg.setLayout(wlayout)
self.setCentralWidget(wwg)
def _frist_story(self, h1_wlayout):
# 第⼀层布局
self.h1_combox1 = QComboBox(minimumWidth=100)
self.h1_combox1.addItems(wind_field)
self.h1_combox2 = QComboBox(minimumWidth=100)
self.h1_combox2.addItems(wind_mach_chooice(self.h1_combox1.currentText()))  self.h1_combox3 = QComboBox(minimumWidth=100)numpy教程 pdf
self.h1_combox3.addItems(wind_blade)
self.h1_combox4 = QComboBox(minimumWidth=100)
self.h1_combox4.addItems(signal_type)
# ⾏为测试暂时⽆法使⽤
h1_cb1_action = QAction("风场选择", self)
h1_cb1_action.setStatusTip("请选择风场")
self.h1_combox1.addAction(h1_cb1_action)
h1_wlayout.addItem(QSpacerItem(20, 20))
h1_wlayout.addWidget(QLabel("风场"),0)
h1_wlayout.addWidget(self.h1_combox1,0)
h1_wlayout.addItem(QSpacerItem(40, 20))
h1_wlayout.addWidget(QLabel("风机"), 0)
h1_wlayout.addWidget(self.h1_combox2, 0)
h1_wlayout.addItem(QSpacerItem(40, 20))
h1_wlayout.addWidget(QLabel("叶⽚ID"), 0)
h1_wlayout.addWidget(self.h1_combox3, 0)
h1_wlayout.addItem(QSpacerItem(40, 20))
h1_wlayout.addWidget(QLabel("信号类型"), 0)
h1_wlayout.addWidget(self.h1_combox4, 0)
h1_wlayout.setAlignment(Qt.AlignLeft)
# 事件绑定
self.h1_t(self._wind_chooice)
def _second_story(self, h2_wlayout):
# 第⼆层布局
self.h2_date1 = QDateEdit(QDate.currentDate())
self.h2_date1.setCalendarPopup(True)
self.h2_date2 = QDateEdit(QDate.currentDate())
self.h2_date2.setCalendarPopup(True)
self.h2_button = QPushButton("运⾏")
self.h2_button2 = QPushButton("停⽌")
h2_wlayout.addItem(QSpacerItem(20, 20))
h2_wlayout.addWidget(QLabel("起始"),0)
h2_wlayout.addWidget(self.h2_date1)
h2_wlayout.addItem(QSpacerItem(50, 20))
h2_wlayout.addWidget(QLabel("结束"), 0)
h2_wlayout.addWidget(self.h2_date2)
h2_wlayout.addItem(QSpacerItem(70, 20))
h2_wlayout.addWidget(self.h2_button)
h2_wlayout.addWidget(self.h2_button2)
h2_wlayout.setAlignment(Qt.AlignLeft)
# 事件绑定
self.h2_t(lambda: self._start_func())
self.h2_t(lambda: self._stop_func())
def _third_left(self, v4_wlayout, v5_wlayout):
# 第三层布局
# 分量布局
v4_group_imf = QGridLayout()
vbox1 = QGroupBox("分量值")
self.radio_1 = QRadioButton("分量1")
self.radio_2 = QRadioButton("分量2")
self.radio_3 = QRadioButton("分量3")
self.radio_4 = QRadioButton("分量4")
self.radio_5 = QRadioButton("分量5")
self.radio_6 = QRadioButton("分量6")
self.radio_7 = QRadioButton("分量7")
self.radio_8 = QRadioButton("分量8")
self.radio_9 = QRadioButton("分量9")
self.radio_1.setChecked(True)
self.radio_val = self.()
# 优先级布局
v4_group_prior = QGridLayout()
vbox2 = QGroupBox("优先级")
cb1 = QCheckBox("叶⽚1")
cb2 = QCheckBox("叶⽚2")
cb3 = QCheckBox("叶⽚3")
self.v4_lineEdit = QLineEdit()
# 时间布局
v4_group_time = QGridLayout()
vbox3 = QGroupBox("时间选择")
self.v4_combox1 = QComboBox(minimumWidth=100)
self.v4_combox1.addItem("空")
# 按键
v4_button = QPushButton("显⽰图形")
# 写⼊⽹格格布局
v4_group_imf.addWidget(self.radio_1, 0, 0)
v4_group_imf.addWidget(self.radio_2, 0, 1)
v4_group_imf.addWidget(self.radio_3, 1, 0)
v4_group_imf.addWidget(self.radio_4, 1, 1)
v4_group_imf.addWidget(self.radio_5, 2, 0)
v4_group_imf.addWidget(self.radio_6, 2, 1)
v4_group_imf.addWidget(self.radio_7, 3, 0)
v4_group_imf.addWidget(self.radio_8, 3, 1)
v4_group_imf.addWidget(self.radio_9, 4, 0)
v4_group_prior.addWidget(cb1, 1, 0)
v4_group_prior.addWidget(cb2, 2, 0)
v4_group_prior.addWidget(cb3, 3, 0)
v4_group_prior.addWidget(QLabel("选择是:"),4,0)
v4_group_prior.addWidget(self.v4_lineEdit, 5, 0)
v4_group_time.addWidget(self.v4_combox1)
# 写⼊左侧布局
vbox1.setLayout(v4_group_imf)
vbox2.setLayout(v4_group_prior)
vbox3.setLayout(v4_group_time)
v4_wlayout.addItem(QSpacerItem(50, 20))
v4_wlayout.addWidget(vbox1)
v4_wlayout.addItem(QSpacerItem(50, 20))
v4_wlayout.addWidget(vbox2)
v4_wlayout.addItem(QSpacerItem(50, 20))
v4_wlayout.addWidget(vbox3)
v4_wlayout.addItem(QSpacerItem(50, 20))
v4_wlayout.addWidget(v4_button)
v4_wlayout.addItem(QSpacerItem(50, 20))
# 事件绑定
self.t(lambda: self._changestyle(self.radio_1))  self.t(lambda: self._changestyle(self.radio_2))  self.t(lambda: self._changestyle(self.radio_3))  self.t(lambda: self._changestyle(self.radio_4))  self.t(lambda: self._changestyle(self.radio_5))  self.t(lambda: self._changestyle(self.radio_6))  self.t(lambda: self._changestyle(self.radio_7))  self.t(lambda: self._changestyle(self.radio_8))  self.t(lambda: self._changestyle(self.radio_9))  t(lambda: self._prior_func(cb1))
t(lambda: self._prior_func(cb2))
t(lambda: self._prior_func(cb3))
v4_t(lambda: self._show_func(v5_wlayout))
def _fouth_right(self, v5_wlayout):
# 加载波形图
v5_wlayout.p_plt)
def _my_line(self, var=True):
# var 为True时,为横线,否则为竖线
line = QFrame(self)
line_var = QFrame.HLine
sp_var = Qt.Horizontal
if not var:
line_var = QFrame.VLine

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。