Python3.x+pyqtgraph实现数据可视化教程
1、pyqtgraph库数据可视化效果还不错,特别是窗体程序中图像交互性较好;安装也很⽅便,⽤ pip 安装。
2、在Python中新建⼀个 .py ⽂件,然后写⼊如下代码并执⾏可以得到官⽅提供的很多案例(含代码),出现如下界⾯图像:
amples
图1
图2
图3
4、程序默认是⿊⾊背景,这个是可以修改的。⽐如,在程序的开头部分写⼊如下代码就可以修改背景:
pg.tConfigOption('background', 'w')
pg.tConfigOption('foreground', 'k')
5、⼀个修改背景颜⾊的完整案例如下,可以直接运⾏程序:
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
# 如下2⾏代码是我⾃⼰加⼊的,⽬的是修改默认的⿊⾊背景为其它颜⾊背景
pg.tConfigOption('background', 'w')
pg.tConfigOption('foreground', 'k')
from pyqtgraph.Point import Point
#generate layout
app = QtGui.QApplication([])
win = pg.GraphicsWindow()
win.tWindowTitle('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.tZValue(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.tAutoVisible(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.tZValue(10)
minX, maxX = Region()
p1.tXRange(minX, maxX, padding=0)
t(update)
def updateRegion(window, viewRange):
rgn = viewRange[0]
region.tRegion(rgn)
t(updateRegion)
region.tRegion([1000, 2000])
#cross hair
vLine = pg.InfiniteLine(angle=90, movable=Fal)
hLine = pg.InfiniteLine(angle=0, movable=Fal)
p1.addItem(vLine, ignoreBounds=True)
p1.addItem(hLine, ignoreBounds=True)
vb = p1.vb
def mouMoved(evt):
pos = evt[0] ## using signal proxy turns original arguments into a tuple
if p1.sceneBoundingRect().contains(pos):
mouPoint = vb.mapSceneToView(pos)
index = int(mouPoint.x())
if index > 0 and index < len(data1):
label.tText("<span style='font-size: 12pt'>x=%0.1f, <span style='color: red'>y1=%0.1f</span>, <span style='color: green'>y2=%0.1f</span>" % (mouPoint.x(), data1[index], data2[index])) vLine.tPos(mouPoint.x())
hLine.tPos(mouPoint.y())
proxy = pg.SignalProxy(p1.scene().sigMouMoved, rateLimit=60, slot=mouMoved)
#p1.scene().t(mouMoved)
盾组词语## 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__(lf):
#super(Qt_Test_Frame, lf).__init__(*args, **kw)
super().__init__()
# 初始化界⾯
lf._initUI()
lf.show()
def _initUI(lf):
lf.tWindowTitle("QT图形界⾯测试")
wwg = QWidget()
# 全局布局
wlayout = QVBoxLayout()
h1_wlayout = QHBoxLayout()
h2_wlayout = QHBoxLayout()
h3_wlayout = QHBoxLayout()
v4_wlayout = QVBoxLayout()
v5_wlayout = QVBoxLayout()
lf.statusBar().showMessage("状态栏")
# 第⼀层
lf._frist_story(h1_wlayout)
# 第⼆层
lf._cond_story(h2_wlayout)
# 第三层左
lf._third_left(v4_wlayout, v5_wlayout)
# 第三层右
lf._fouth_right(v5_wlayout)
# 加载
splt = lf._my_line()
splt2 = lf._my_line(Fal)
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(lf.statusBar())
h3_wlayout.addLayout(v4_wlayout, 0)
h3_wlayout.addWidget(splt2)
h3_wlayout.addLayout(v5_wlayout, 2)
#wlayout.tAlignment(Qt.AlignTop)
wwg.tLayout(wlayout)
lf.tCentralWidget(wwg)
def _frist_story(lf, h1_wlayout):
# 第⼀层布局
lf.h1_combox1 = QComboBox(minimumWidth=100)
lf.h1_combox1.addItems(wind_field)
lf.h1_combox2 = QComboBox(minimumWidth=100)
lf.h1_combox2.addItems(wind_mach_chooice(lf.h1_combox1.currentText())) lf.h1_combox3 = QComboBox(minimumWidth=100)
lf.h1_combox3.addItems(wind_blade)
lf.h1_combox4 = QComboBox(minimumWidth=100)
lf.h1_combox4.addItems(signal_type)
# ⾏为测试暂时⽆法使⽤
h1_cb1_action = QAction("风场选择", lf)
h1_cb1_action.tStatusTip("请选择风场")
lf.h1_combox1.addAction(h1_cb1_action)
h1_wlayout.addItem(QSpacerItem(20, 20))
h1_wlayout.addWidget(QLabel("风场"),0)
h1_wlayout.addWidget(lf.h1_combox1,0)
h1_wlayout.addItem(QSpacerItem(40, 20))一年级成语大全
h1_wlayout.addWidget(QLabel("风机"), 0)
h1_wlayout.addWidget(lf.h1_combox2, 0)
h1_wlayout.addItem(QSpacerItem(40, 20))
h1_wlayout.addWidget(QLabel("叶⽚ID"), 0)我舍不得你走
h1_wlayout.addWidget(lf.h1_combox3, 0)
h1_wlayout.addItem(QSpacerItem(40, 20))
h1_wlayout.addWidget(QLabel("信号类型"), 0)
h1_wlayout.addWidget(lf.h1_combox4, 0)
h1_wlayout.tAlignment(Qt.AlignLeft)
# 事件绑定
lf.h1_t(lf._wind_chooice)
def _cond_story(lf, h2_wlayout):
# 第⼆层布局
lf.h2_date1 = QDateEdit(QDate.currentDate())
lf.h2_date1.tCalendarPopup(True)
lf.h2_date2 = QDateEdit(QDate.currentDate())
lf.h2_date2.tCalendarPopup(True)
lf.h2_button = QPushButton("运⾏")
lf.h2_button2 = QPushButton("停⽌")
h2_wlayout.addItem(QSpacerItem(20, 20))
h2_wlayout.addWidget(QLabel("起始"),0)
h2_wlayout.addWidget(lf.h2_date1)
h2_wlayout.addItem(QSpacerItem(50, 20))
h2_wlayout.addWidget(QLabel("结束"), 0)
h2_wlayout.addWidget(lf.h2_date2)
h2_wlayout.addItem(QSpacerItem(70, 20))
h2_wlayout.addWidget(lf.h2_button)
h2_wlayout.addWidget(lf.h2_button2)
h2_wlayout.tAlignment(Qt.AlignLeft)
# 事件绑定
lf.h2_t(lambda: lf._start_func())
lf.h2_t(lambda: lf._stop_func())
def _third_left(lf, v4_wlayout, v5_wlayout):
# 第三层布局
# 分量布局
v4_group_imf = QGridLayout()
vbox1 = QGroupBox("分量值")
lf.radio_1 = QRadioButton("分量1")
lf.radio_2 = QRadioButton("分量2")
lf.radio_3 = QRadioButton("分量3")
lf.radio_4 = QRadioButton("分量4")
lf.radio_5 = QRadioButton("分量5")
lf.radio_6 = QRadioButton("分量6")
lf.radio_7 = QRadioButton("分量7")
lf.radio_8 = QRadioButton("分量8")
lf.radio_9 = QRadioButton("分量9")
lf.radio_1.tChecked(True)
lf.radio_val = lf.()大和族
# 优先级布局
万峰湖景区
v4_group_prior = QGridLayout()
vbox2 = QGroupBox("优先级")
cb1 = QCheckBox("叶⽚1")
cb2 = QCheckBox("叶⽚2")
cb3 = QCheckBox("叶⽚3")
lf.v4_lineEdit = QLineEdit()
# 时间布局
v4_group_time = QGridLayout()
vbox3 = QGroupBox("时间选择")
lf.v4_combox1 = QComboBox(minimumWidth=100)
lf.v4_combox1.addItem("空")
# 按键
v4_button = QPushButton("显⽰图形")
# 写⼊⽹格格布局
v4_group_imf.addWidget(lf.radio_1, 0, 0)
v4_group_imf.addWidget(lf.radio_2, 0, 1)
v4_group_imf.addWidget(lf.radio_3, 1, 0)
v4_group_imf.addWidget(lf.radio_4, 1, 1)
v4_group_imf.addWidget(lf.radio_5, 2, 0)
v4_group_imf.addWidget(lf.radio_6, 2, 1)
v4_group_imf.addWidget(lf.radio_7, 3, 0)
v4_group_imf.addWidget(lf.radio_8, 3, 1)
v4_group_imf.addWidget(lf.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(lf.v4_lineEdit, 5, 0)
v4_group_time.addWidget(lf.v4_combox1)
# 写⼊左侧布局
vbox1.tLayout(v4_group_imf)
vbox2.tLayout(v4_group_prior)
vbox3.tLayout(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))
# 事件绑定冷静之靴
lf.t(lambda: lf._changestyle(lf.radio_1)) lf.t(lambda: lf._changestyle(lf.radio_2)) lf.t(lambda: lf._changestyle(lf.radio_3)) lf.t(lambda: lf._changestyle(lf.radio_4)) lf.t(lambda: lf._changestyle(lf.radio_5)) lf.t(lambda: lf._changestyle(lf.radio_6)) lf.t(lambda: lf._changestyle(lf.radio_7)) lf.t(lambda: lf._changestyle(lf.radio_8)) lf.t(lambda: lf._changestyle(lf.radio_9)) t(lambda: lf._prior_func(cb1))
t(lambda: lf._prior_func(cb2))
t(lambda: lf._prior_func(cb3))
v4_t(lambda: lf._show_func(v5_wlayout))
def _fouth_right(lf, v5_wlayout):
# 加载波形图
v5_wlayout.p_plt)
def _my_line(lf, var=True):
# var 为True时,为横线,否则为竖线
line = QFrame(lf)
line_var = QFrame.HLine
sp_var = Qt.Horizontal
if not var:
line_var = QFrame.VLine