1 说明:
=====
1.1 pyqtgraph是Python平台上一种功能强大的2D/3D绘图库,相当于matplotlib库,比它更强大 。
1.2 由于内部实现方式上,使用了高速计算的numpy信号处理库以及Qt的GraphicsView框架 。
1.3 环境:
华为笔记本电脑、深度deepin-linux操作系统、python3.8和微软vscode编辑器 。
1.4 对开源的源代码进行删减、修改、注释和整理,突出重点,易于操作 。
文章插图
不在迷茫
2 安装:
=====
2.1 pyqtgraph库安装:
pip install --user pyqtgraph#本机安装#pip3.8 install -i https://mirrors.aliyun.com/pypi/simple pyqtgraph #超快
2.2 PyQt5安装:需要配合pip install PyQt5 #文件太大,速度太慢#本机安装,推荐国内源安装pip3.8 install -i https://mirrors.aliyun.com/pypi/simple PyQt5
2.3 小插曲:本机,每个电脑可能不一样 。#在微软vscode编辑器中python中可以导出模块,报错https://stackoverflow.com/questions/26191513/opengl-error-in-python-pyqtgraph-opengl-items-glscatterplotitem-glscatterplot-it#发现这个包放错了Requirement already satisfied: pyqtgraph in ./.local/lib/python3.8/site-packages (0.10.0)#复制到Requirement already satisfied: numpy in /usr/local/python3.8/lib/python3.8/site-packages (from pyqtgraph) (1.18.2)#root形式进入
2.4 官网:源码来源 。https://github.com/pyqtgraph/pyqtgraphhttp://pyqtgraph.org/
文章插图
官网的源码的一个动画,暂时不是本章重点讲解,本次讲解基础 。
3 sin、cos:
========
3.1 sin和cos,代码放在一起,注释里有:
import pyqtgraph as pgimport numpy as npimport arrayApp = pg.mkQApp()#建立appwin = pg.GraphicsWindow()#建立窗口win.setWindowTitle('pyqtgraph逐点画波形图') #窗口名称win.resize(800, 500)#窗口大小data = https://www.isolves.com/it/cxkf/yy/Python/2020-06-19/array.array('d') #可动态改变数组的大小,double型数组historyLength = 100#横坐标长度p = win.addPlot()#把图p加入到窗口中p.showGrid(x=True, y=True)#把X和Y的表格打开p.setRange(xRange=[0,historyLength], yRange=[-1.2, 1.2], padding=0)p.setLabel(axis='left', text='y / V')#靠左,y轴p.setLabel(axis='bottom', text='x / point') #靠下,x轴p.setTitle('y = sin(x)')#表格的名字curve = p.plot()#绘制一个图形idx = 0#定义函数def plotData():global idx#内部作用域想改变外部域变量tmp = np.sin(np.pi / 50 * idx)#sin动态函数曲线#tmp = np.cos(np.pi / 50 * idx)#cos动态函数曲线if len(data)3.2 效果图:
文章插图
文章插图
3.3 高级一些:
# -*- coding: utf-8 -*-import pyqtgraph as pgfrom pyqtgraph.Qt import QtCore, QtGuiimport numpy as np#数据,可自定义x = np.linspace(-20, 20, 1000)y = np.sin(x) / x #sin#y = np.cos(x) / x#cos,省略plot = pg.plot()plot.setYRange(-1, 2) #y轴取值范围plot.setWindowTitle('pyqtgraph example: text') #表格名称curve = plot.plot(x,y) #导入html格式的text静态标签,本次重点text = pg.TextItem(html='<div style="text-align: center"><span style="color: #FFF;">This is the</span><br><span style="color: #FF0; font-size: 16pt;">PEAK</span></div>', anchor=(-0.3,0.5), angle=20, border='w', fill=(0, 0, 255, 100))plot.addItem(text)text.setPos(0, y.max())#画箭头arrow = pg.ArrowItem(pos=(0, y.max()), angle=-45)plot.addItem(arrow)## 设置动画和曲线curvePoint = pg.CurvePoint(curve)plot.addItem(curvePoint)#text2是动态显示texttext2 = pg.TextItem("test", anchor=(0.5, -1.0))text2.setParentItem(curvePoint)arrow2 = pg.ArrowItem(angle=90)arrow2.setParentItem(curvePoint)index = 0def update():global curvePoint, indexindex = (index + 1) % len(x)curvePoint.setPos(float(index)/(len(x)-1))text2.setText('[%0.1f, %0.1f]' % (x[index], y[index]))timer = QtCore.QTimer()timer.timeout.connect(update)timer.start(10) #every 10msif __name__ == '__main__':QtGui.QApplication.instance().exec_()
3.4 效果图:
文章插图
4 3D-GL模块调用的效果图:
4.1 代码:
# -*- coding: utf-8 -*-#导出模块from pyqtgraph.Qt import QtCore, QtGuiimport pyqtgraph as pgimport pyqtgraph.opengl as glimport numpy as np## Create a GL View widget to display data,GL模块app = QtGui.QApplication([])w = gl.GLViewWidget()w.show()w.setWindowTitle('pyqtgraph example: GLSurfacePlot')w.setCameraPosition(distance=50)## Add a grid to the viewg = gl.GLGridItem()g.scale(2,2,1)g.setDepthValue(10) w.addItem(g)#P1图z = pg.gaussianFilter(np.random.normal(size=(50,50)), (1,1))p1 = gl.GLSurfacePlotItem(z=z, shader='shaded', color=(0.5, 0.5, 1, 1))p1.scale(16./49., 16./49., 1.0)p1.translate(-18, 2, 0)w.addItem(p1)#P2图x = np.linspace(-8, 8, 50)y = np.linspace(-8, 8, 50)z = 0.1 * ((x.reshape(50,1) ** 2) - (y.reshape(1,50) ** 2))p2 = gl.GLSurfacePlotItem(x=x, y=y, z=z, shader='normalColor')p2.translate(-10,-10,0)w.addItem(p2)#P3图z = pg.gaussianFilter(np.random.normal(size=(50,50)), (1,1))x = np.linspace(-12, 12, 50)y = np.linspace(-12, 12, 50)colors = np.ones((50,50,4), dtype=float)colors[...,0] = np.clip(np.cos(((x.reshape(50,1) ** 2) + (y.reshape(1,50) ** 2)) ** 0.5), 0, 1)colors[...,1] = colors[...,0]p3 = gl.GLSurfacePlotItem(z=z, colors=colors.reshape(50*50,4), shader='shaded', smooth=False)p3.scale(16./49., 16./49., 1.0)p3.translate(2, -18, 0)w.addItem(p3)#P4图:动画cols = 90rows = 100x = np.linspace(-8, 8, cols+1).reshape(cols+1,1)y = np.linspace(-8, 8, rows+1).reshape(1,rows+1)d = (x**2 + y**2) * 0.1d2 = d ** 0.5 + 0.1phi = np.arange(0, np.pi*2, np.pi/20.)z = np.sin(d[np.newaxis,...] + phi.reshape(phi.shape[0], 1, 1)) / d2[np.newaxis,...]p4 = gl.GLSurfacePlotItem(x=x[:,0], y = y[0,:], shader='heightColor', computeNormals=False, smooth=False)p4.shader()['colorMap'] = np.array([0.2, 2, 0.5, 0.2, 1, 1, 0.2, 0, 2])p4.translate(10, 10, 0)w.addItem(p4)index = 0def update():global p4, z, indexindex -= 1p4.setData(z=z[index%z.shape[0]])timer = QtCore.QTimer()timer.timeout.connect(update)timer.start(30)if __name__ == '__main__':QtGui.QApplication.instance().exec_()
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 冬天可以喝什么茶呢,什么茶是甜的
- 应用程序加固Tomcat篇
- 宽带已升级到200M,为什么网络还是那么慢?教你一招突破网速限制
- 淘宝店铺手机详情投放是什么意思 淘宝手机详情投放有什么用
- 茶香氤氲是福州,福州复兴千年茉莉花茶
- 普洱熟茶特点是,普洱熟茶是什么茶
- 男人尿血是什么症状
- 男人虚了吃什么补?
- 怎么让阴茎变大变长
- 淘宝极速推极速版和定向版哪个好 淘宝极速推广极速版好还是定向版好