栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pyqtgraph文档笔记(二)快速启用pyqtgraph

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pyqtgraph文档笔记(二)快速启用pyqtgraph

一、最简模式运行

假如源文件为main.py, 可以通过python main.py 或者python -m main运行,main.py最简代码如下:

import pyqtgraph as pg
pg.plot([1,2,3])  

但这样程序会一闪而过,可以采用三种方式:

  • 加入input()语句等待键盘输入。

  • 添加事件循环代码,如下:

import PyQt5
import sys
import pyqtgraph as pg
app = PyQt5.QtWidgets.QApplication([])    
pg.plot([1,2,3])   
sys.exit(app.exec_())
  • 加入pg.exec()
import pyqtgraph as pg
pg.plot([1,2,3])  
pg.exec()
二、快速画图 1、直接画线

下面的例子在一个图上画多条线。

import pyqtgraph as pg

x = [1,2,3]
y1 = [4,6,5]
y2 = [5,2,9]
pw = pg.plot(x,y1, pen='r')  # 画第一条线
pw.plot(x, y2, pen='b')  #画第二条线

pg.exec()

运行结果

2、使用GraphicsWindow

下面的例子在同一行画两个图

import pyqtgraph as pg

y1 = [4,6,5]
y2 = [5,2,9]

win = pg.GraphicsWindow()
p1=win.addPlot()
p1.plot(y1,pen=(255,255,0))
p2=win.addPlot()
p2.plot(y2,pen='r')

pg.exec()

运行结果:

3、 自定义分行

使用 GraphicsWindow 的 nextRow() 方法手动分行。

import pyqtgraph as pg

y1 = [4,6,5]
y2 = [5,2,9]

win = pg.GraphicsWindow()
p1=win.addPlot()
p1.plot(y1,pen=(255,255,0))

win.nextRow() #添加新行

p2=win.addPlot()
p2.plot(y2,pen='r')

pg.exec()

运行结果如下:

4、复杂布局

在 addPlot() 时设置 row , col , rowspan , colspan 参数

import pyqtgraph as pg

y = [4,6,5]

win = pg.GraphicsWindow()
p1=win.addPlot(row=0, col=0)
p1.plot(y,pen='r')

p2=win.addPlot(row=0, col=1)
p2.plot(y,pen='g')

p2=win.addPlot(row=0, col=2)
p2.plot(y,pen='b')

win.nextRow() #添加新行

p2=win.addPlot(row=1, col=0, colspan=2, rowspan=1)
p2.plot(y,pen='y')

pg.exec()

运行结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/980196.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号