将pyqtgraph作为窗体嵌入到PyQt程序中

总体使用原则:可以用其他的widget一样的使用方式使用pyqtgraph

基础使用方法之PlotWidget类

基础类之一 pyqtgraph.PlotWidget

这个类是用来绘图的基础控件

# 类定义
class pyqtgraph.PlotWidget(parent=None, background='default', **kargs)
# 初始化的时候,parent和background参数会被传递给 GraphsWidget.__init__()
# 其他参数会被传递给 PlotItem.__init__()# 方法--获取控件中包含的 PlotItem 对象
getPlotItem()sigTransformChanged
# GraphicsView widget with a single PlotItem inside.
"""
The following methods are wrapped directly from PlotItem: addItem, removeItem, clear, setXRange, setYRange, setRange, autoRange, setXLink, setYLink, viewRect, setMouseEnabled, enableAutoRange, disableAutoRange, setAspectLocked, setLimits, register, unregister
For all other methods, use getPlotItem.
"""

实例测试:在QMainWindow中添加PlotWidget控件

基础测试

源码:

from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QMainWindow, QWidget
import pyqtgraph as pg
import numpy as np
import sysclass TestWindow(QMainWindow):def __init__(self, parent=None):QMainWindow.__init__(self)# 设置窗体尺寸self.setGeometry(300, 300, 500, 350)# 添加一个按钮和一个PlotWidgetself.draw_fig_btn = QPushButton("绘图")self.draw_fig_btn.setFixedWidth(100)self.plt = pg.PlotWidget()# 将两个Widget垂直排列布局# MainWindow的主体内容要通过一个widget包裹在一起,通过self.setCentralWidget设置centralWidget = QWidget()main_layout = QVBoxLayout()main_layout.addWidget(self.draw_fig_btn)main_layout.addWidget(self.plt)centralWidget.setLayout(main_layout)# 应用上述布局self.setCentralWidget(centralWidget)# 为[绘图]按钮注册一个回调函数self.draw_fig_btn.clicked.connect(self.draw_fig_btn_cb)def draw_fig_btn_cb(self):# [绘图]按钮回调函数x = np.linspace(-5 * np.pi, 5 * np.pi, 500)y = 0.5 * np.sin(x)self.plt.plot(x, y, pen="r")if __name__ == "__main__":app = QApplication(sys.argv)qb = TestWindow()qb.show()sys.exit(app.exec_())

初始窗体:

单击绘图按键以后:

改变绘图背景颜色

全局设置

在程序最开头 import的后面加入以下语句
设置背景为白色,前景为黑色

pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
局部设置

默认的绘图背景是黑色的,若实际应用中不想使用黑色作为背景,可以自定义背景颜色,上文提到 pyqtgraph.PlotWidget 在初始化时,可以通过传入background参数,该参数即绘图的背景颜色,其合法值为pyqtgraph.mkColor函数的合法输入,包括以下形式:

  1. 以下字符中的一个: r, g, b, c, m, y, k, w
  2. R, G, B, [A] integers 0-255
  3. (R, G, B, [A]) tuple of integers 0-255
  4. float greyscale, 0.0-1.0
  5. int see :func:intColor() <pyqtgraph.intColor>
  6. (int, hues) see :func:intColor() <pyqtgraph.intColor>
  7. “RGB” hexadecimal strings; may begin with ‘#’
  8. “RGBA”
  9. “RRGGBB”
  10. “RRGGBBAA”
  11. QColor QColor instance; makes a copy.

笔者测试时使用白色背景,将

 self.plt = pg.PlotWidget()

改为

self.plt = pg.PlotWidget(background="w")

效果变为

改变坐标刻度样式,添加坐标轴标题

默认的坐标轴刻度颜色是灰色,在白色背景下,不太好看清,并且没有添加坐标轴,这就需要注意到 pyqtgraph.PlotItem类了。

涉及到的几个封装类有:

  • pyqtgraph.PlotWidget
  • pyqtgraph.PlotItem
  • pyqtgraph.AxisItem

在初始化控件的时候,实例后的self.plt对象就是pyqtgraph.PlotWidget,通过以下方式可以获得其中的另外两个对象

pltItem = self.plt.getPlotItem()
left_axis = pltItem.getAxis("left")

其中 left_axis的轴是左边的坐标轴,传入 ‘bottom’, ‘top’, 或者 ‘right’参数可以获得其他的坐标轴

坐标轴标签很好更改,

labelStyle = {'color': '#000', 'font-size': '22pt'} # 字体颜色和大小
left_axis.setLabel('y轴', units='y轴单位', **labelStyle)

但在更改坐标刻度的字体大小时遇到了困难,官方文档给出的接口是AxisItem.setStyle(tickFont=QFont),但经过测试,该接口存在BUG,并不起作用,通过stack上搜索,以下方法可以有效更改

font = QFont()
font.setPixelSize(14)
left_axis.tickFont = font

但当刻度的字体过大时,容易造成刻度和坐标轴标题重合(如下图所示),需要注意

设置合适的字体大小后(颜色通过前文全局设置修改),效果如下图所示

至此,基本绘图所需功能实现完毕

整体代码如下:

from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QMainWindow, QWidget, QFontDialog
from PyQt5.QtGui import QFont
import pyqtgraph as pg
import numpy as np
import syspg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')class TestWindow(QMainWindow):def __init__(self, parent=None):QMainWindow.__init__(self)# 设置窗体尺寸self.setGeometry(300, 300, 500, 350)# 添加一个按钮和一个PlotWidgetself.draw_fig_btn = QPushButton("绘图")self.draw_fig_btn.setFixedWidth(100)self.plt = pg.PlotWidget(background="w")# 将两个Widget垂直排列布局# MainWindow的主体内容要通过一个widget包裹在一起,通过self.setCentralWidget设置centralWidget = QWidget()main_layout = QVBoxLayout()main_layout.addWidget(self.draw_fig_btn)main_layout.addWidget(self.plt)centralWidget.setLayout(main_layout)# 应用上述布局self.setCentralWidget(centralWidget)# 为[绘图]按钮注册一个回调函数self.draw_fig_btn.clicked.connect(self.draw_fig_btn_cb)def draw_fig_btn_cb(self):# [绘图]按钮回调函数x = np.linspace(-5 * np.pi, 5 * np.pi, 500)y = 0.5 * np.sin(x)self.plt.plot(x, y, pen="r")pltItem = self.plt.getPlotItem()left_axis = pltItem.getAxis("left")left_axis.enableAutoSIPrefix(False)font = QFont()font.setPixelSize(16)left_axis.tickFont = fontbottom_axis = pltItem.getAxis("bottom")bottom_axis.tickFont = fontlabelStyle = {'color': '#000', 'font-size': '16pt'}left_axis.setLabel('y轴', units='单位', **labelStyle)bottom_axis.setLabel('x轴', units='单位', **labelStyle)if __name__ == "__main__":app = QApplication(sys.argv)qb = TestWindow()qb.show()sys.exit(app.exec_())

使用pyqtgraph模块进行PyQt绘图(2)相关推荐

  1. 【Python_PyQtGraph 学习笔记(五)】基于PyQtGraph和GraphicsLayoutWidget动态绘图并实现窗口模式,且保留全部绘图信息

    基于PyQtGraph和GraphicsLayoutWidget动态绘图并实现窗口模式,且保留全部绘图信息 前言 基于PySide2.PyQtGraph和GraphicsLayoutWidget动态绘 ...

  2. 【Python2】使用python中的turtle模块学习海龟绘图(有趣的python初体验)(最全最详细的turtle介绍使用)

    目录 海龟绘图 Python中tkinter的mainloop函数实质 turtle模块里的方法 Python绘图Turtle库详解 turtle绘图的基础知识: 海龟绘图 海龟绘图是Python中非 ...

  3. Python中pyqtgraph模块结构及用法(1)

    pyqtgraph官方文档 官方网站 pyqtgraph是一个纯python的图形和GUI库,基于PyQt4\PySide和Numpy 一. pyqtgraph绘图方式 方法 官方文档 描述 pyqt ...

  4. pyqtgraph文档笔记(三)pyqtgraph嵌入到PyQt

    pyqtgraph可以单独运行,也可以嵌入到PyQt中交互.要嵌入到PyQt, 需要生成 pyqtgraph.PlotWidget,然后就可以像正常的Qt Widget 一样添加使用,如添加到layo ...

  5. Python 第三方模块 绘图 Matplotlib模块 简介与配置

    颜色信息查看:https://matplotlib.org/gallery/color/color_demo.html 一些常用图像的绘制代码:25个常用Matplotlib图的Python代码,收藏 ...

  6. python模块matplotlib.pyplot用法_03_Python 使用Matplotlib绘图

    2019.5.13 不知不觉,已经进入第12周了,Python数据分析的学习现今也已经进入了中后期,在继上周进行了Numpy的康威生命游戏的编写之后:紧接着进行的学习就是利用Python的Matplo ...

  7. python gui 三维 pyqt5_【PyQt5-Qt Designer】在GUI中使用pyqtgraph绘图库

    pyqtgraph绘图库 1.1 简介: pyqtgraph是Python平台上一种功能强大的2D/3D绘图库,相对于matplotlib库,由于内部实现方式上,使用了高速计算的numpy信号处理库以 ...

  8. 二、PyQtGragh模块安装以及上手体验

    PyQtGraph是一个强大的绘图库,用于创建实时交互式2D和3D图形.它是基于PyQt库开发的,因此它可以与PyQt应用程序紧密集成,并且拥有许多强大的特性,如: 快速:PyQtGraph使用高效的 ...

  9. ESP32( IDF平台)+MAX30102 配合Pyqt上位机实现PPG波形显示与心率计算

    0 引言 年前买了一个MAX30102模块,在家无聊做了这个demo对一些相关的知识进行学习. 主要学习的内容: 光体积变化描记图(Photoplethysmogram, PPG)测量原理学习. ES ...

最新文章

  1. java ftp 判断文件是否存在_FTP判断文件是否存在
  2. 10.25 es问题
  3. 杭州电子科技大学计算机组成原理期末试卷,杭州电子科技大学计算机组成原理期末样卷(A)...
  4. Fedora 与 Ubuntu 深度比较
  5. 快速启动程序的工具软件都比不了Win+R-转
  6. error: '__gnu_cxx::_Lock_policy' has not been declared
  7. PyCharm之python书写规范--消去提示波浪线
  8. html5 下拉刷新(pc+移动网页源码)
  9. 60+PPT 下载丨Oracle Open World 2019
  10. jenkins 启动_通过http请求启动jenkins任务
  11. JDK1.8网盘链接
  12. 根据指定字段排序编号(SQL Server 2005,Update,Order By)
  13. Spring 框架蕴含的设计思想
  14. 利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。
  15. html em vw,rem em 与vh vw的用法简单介绍
  16. 西电微原课设——矩阵式键盘数字密码锁设计
  17. 三角函数逼近锯齿函数和阶梯函数
  18. Fluent NHibernate入门
  19. windows性能计数器
  20. 新!《一天吸引大量精准流量》主动加你微信的方法,无需软件,告别大量推广,让对你产品感兴趣的客源主动加你!!!

热门文章

  1. UnityShader_天空盒子中的反射、折射、聂菲尔效应
  2. Altium Designer21 的安装过程
  3. 《缠中说禅108课》22:将 8 亿的大米装到 5 个庄家的肚里
  4. 忆享聚焦|全球云计算市场份额、数字虚拟人、“元宇宙”实体店……近期行业热点速览
  5. 实践出真知:大乱斗游戏
  6. 史上最全 | 基于深度学习的3D分割综述(RGB-D/点云/体素/多目)
  7. HDU-4069(Squiggly Sudoku)(Dancing Links + dfs)
  8. JS获取当前使用的浏览器名字以及版本号
  9. Inventor冲压加强筋_Inventor教程:创建加强筋
  10. Inventor 2020 安装教程