Pyqtgraph是Python平台上一种功能强大的2D/3D绘图库( 二 )

4.2 效果图:

Pyqtgraph是Python平台上一种功能强大的2D/3D绘图库

文章插图
 
4.3 代码:
from pyqtgraph.Qt import QtCore, QtGuiimport pyqtgraph.opengl as glapp = QtGui.QApplication([])w = gl.GLViewWidget()w.opts['distance'] = 20w.show()w.setWindowTitle('pyqtgraph example: GLViewWidget')ax = gl.GLAxisItem()ax.setSize(5,5,5)w.addItem(ax)b = gl.GLBoxItem()w.addItem(b)ax2 = gl.GLAxisItem()ax2.setParentItem(b)b.translate(1,1,1)if __name__ == '__main__':#import sys#if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):#QtGui.QApplication.instance().exec_()QtGui.QApplication.instance().exec_() #采用简化的,注意4.4 效果图:
Pyqtgraph是Python平台上一种功能强大的2D/3D绘图库

文章插图
【Pyqtgraph是Python平台上一种功能强大的2D/3D绘图库】 
4.5 立方体代码:
# -*- coding: utf-8 -*-#导出模块from pyqtgraph.Qt import QtCore, QtGuiimport pyqtgraph as pgimport pyqtgraph.opengl as glimport numpy as np#基本初始化定义app = QtGui.QApplication([])w = gl.GLViewWidget()w.show()w.setWindowTitle('pyqtgraph example: GLMeshItem') #标题名w.setCameraPosition(distance=40) #设置摄像机位置g = gl.GLGridItem()g.scale(2,2,1)w.addItem(g)## Example 1:verts = np.array([[0, 0, 0],[2, 0, 0],[1, 2, 0],[1, 1, 1],])faces = np.array([[0, 1, 2],[0, 1, 3],[0, 2, 3],[1, 2, 3]])colors = np.array([[1, 0, 0, 0.3],[0, 1, 0, 0.3],[0, 0, 1, 0.3],[1, 1, 0, 0.3]])## Mesh item will automatically compute face normals.m1 = gl.GLMeshItem(vertexes=verts, faces=faces, faceColors=colors, smooth=False)m1.translate(5, 5, 0)m1.setGLOptions('additive')w.addItem(m1)## Example 2:## Array of vertex positions, three per face,3角体verts = np.empty((36, 3, 3), dtype=np.float32)theta = np.linspace(0, 2*np.pi, 37)[:-1]verts[:,0] = np.vstack([2*np.cos(theta), 2*np.sin(theta), [0]*36]).Tverts[:,1] = np.vstack([4*np.cos(theta+0.2), 4*np.sin(theta+0.2), [-1]*36]).Tverts[:,2] = np.vstack([4*np.cos(theta-0.2), 4*np.sin(theta-0.2), [1]*36]).T## Colors are specified per-vertexcolors = np.random.random(size=(verts.shape[0], 3, 4))m2 = gl.GLMeshItem(vertexes=verts, vertexColors=colors, smooth=False, shader='balloon',drawEdges=True, edgeColor=(1, 1, 0, 1))m2.translate(-5, 5, 0)w.addItem(m2)## Example 3:## sphere,球md = gl.MeshData.sphere(rows=10, cols=20)colors = np.ones((md.faceCount(), 4), dtype=float)colors[::2,0] = 0colors[:,1] = np.linspace(0, 1, colors.shape[0])md.setFaceColors(colors)m3 = gl.GLMeshItem(meshdata=https://www.isolves.com/it/cxkf/yy/Python/2020-06-19/md, smooth=False)#, shader='balloon')m3.translate(5, -5, 0)w.addItem(m3)# Example 4:# wireframe,火焰md = gl.MeshData.sphere(rows=4, cols=8)m4 = gl.GLMeshItem(meshdata=md, smooth=False, drawFaces=False, drawEdges=True, edgeColor=(1,1,1,1))m4.translate(0,10,0)w.addItem(m4)# Example 5:# cylinder,圆柱体md = gl.MeshData.cylinder(rows=10, cols=20, radius=[1., 2.0], length=5.)md2 = gl.MeshData.cylinder(rows=10, cols=20, radius=[2., 0.5], length=10.)colors = np.ones((md.faceCount(), 4), dtype=float)colors[::2,0] = 0colors[:,1] = np.linspace(0, 1, colors.shape[0])md.setFaceColors(colors)m5 = gl.GLMeshItem(meshdata=md, smooth=True, drawEdges=True, edgeColor=(1,0,0,1), shader='balloon')colors = np.ones((md.faceCount(), 4), dtype=float)colors[::2,0] = 0colors[:,1] = np.linspace(0, 1, colors.shape[0])md2.setFaceColors(colors)m6 = gl.GLMeshItem(meshdata=md2, smooth=True, drawEdges=False, shader='balloon')m6.translate(0,0,7.5)m6.rotate(0., 0, 1, 1)w.addItem(m5)w.addItem(m6)QtGui.QApplication.instance().exec_()#这种方法:注意有三种方法结尾4.6 效果图:
Pyqtgraph是Python平台上一种功能强大的2D/3D绘图库

文章插图
 
以上是总体介绍,以后有空做逐个详细介绍 。也可以自己学习 。
自己整理并分享出来,喜欢的就点赞、关注、转发和收藏 。




推荐阅读