PyQt5开发笔记:2. 2D与3D散点图、水平布局和边框修饰

一、装pyqtgraph和PyOpenGL库

bash 复制代码
pip install pyqtgraph
pip install PyOpenGL

注意:一定不要pip install OpenGL,否则会找不到

二、3D散点图效果

python 复制代码
import pyqtgraph as pg
import pyqtgraph.opengl as gl
import numpy as np

# 创建应用程序
app = pg.mkQApp("3D Scatter Plot")

# 创建图形窗口
w = gl.GLViewWidget()
w.show()
w.setWindowTitle('pyqtgraph 3D Scatter Plot')
w.setCameraPosition(distance=40)

# 创建3D散点图数据
g = gl.GLGridItem()
w.addItem(g)

pos = np.random.normal(size=(1000, 3))
scatter = gl.GLScatterPlotItem(pos=pos, color=(1, 1, 1, 1), size=5)
w.addItem(scatter)

# 运行应用程序
pg.exec()

Pyqtgraph,一个神奇的python库 (qq.com)

三、水平布局

使用 QSplitter 来管理两个窗口的布局,这样可以确保它们并排显示并且可以调整大小;使用传统的QHBoxLayout容易出问题

python 复制代码
import pyqtgraph as pg
import pyqtgraph.opengl as gl
from pyqtgraph.Qt import QtCore, QtWidgets
import numpy as np

# 创建应用程序
app = QtWidgets.QApplication([])

# 创建主窗口
main_window = QtWidgets.QMainWindow()
main_window.setWindowTitle('Combined 3D and 2D Scatter Plot')
main_window.resize(1200, 600)

# 创建一个中心部件
central_widget = QtWidgets.QWidget()
main_window.setCentralWidget(central_widget)

# 创建水平布局
hbox = QtWidgets.QHBoxLayout(central_widget)

# 创建QSplitter来并排显示两个窗口
splitter = QtWidgets.QSplitter(QtCore.Qt.Horizontal)

# 创建3D散点图窗口
w = gl.GLViewWidget()
w.setWindowTitle('pyqtgraph 3D Scatter Plot')
w.setCameraPosition(distance=40)

# 创建3D散点图数据
g = gl.GLGridItem()
w.addItem(g)

pos = np.random.normal(size=(1000, 3))
scatter = gl.GLScatterPlotItem(pos=pos, color=(1, 1, 1, 1), size=5)
w.addItem(scatter)

# 创建2D散点图窗口
win = pg.GraphicsLayoutWidget()
win.setWindowTitle("Interactive Scatter Plot")

# 创建一个绘图窗口
plot = win.addPlot(title="Interactive Scatter Plot")
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
plot.plot(x, y, pen=None, symbol='o')

# 将两个窗口添加到QSplitter中
splitter.addWidget(w)
splitter.addWidget(win)

# 将QSplitter添加到水平布局中
hbox.addWidget(splitter)

# 显示主窗口
main_window.show()

# 运行应用程序
app.exec_()

四、模块化代码并修饰边框

将两块区域分别放在不同的py文件里,然后定义一个main.py,并且稍微修饰一下边框,代码如下

python 复制代码
# main.py
from PyQt5 import QtCore, QtWidgets
from Scatter3D_plot import create_3d_scatter_plot
from Scatter2D_plot import create_2d_scatter_plot

# 创建应用程序
app = QtWidgets.QApplication([])

# 创建主窗口
main_window = QtWidgets.QMainWindow()
main_window.setWindowTitle('Combined 3D and 2D Scatter Plot')
main_window.resize(1200, 600)

# 设置样式表来修改边框样式
main_window.setStyleSheet("""
    QMainWindow {
        background-color: white;  /* 设置背景颜色 */
        border: 2px solid #3498db;  /* 设置边框样式 */
        border-radius: 10px;        /* 设置边框圆角 */
        padding: 10px;              /* 设置内边距 */
    }
""")

# 创建一个中心部件
central_widget = QtWidgets.QWidget()
main_window.setCentralWidget(central_widget)

# 创建水平布局
hbox = QtWidgets.QHBoxLayout(central_widget)

# 创建QSplitter来并排显示两个窗口
splitter = QtWidgets.QSplitter(QtCore.Qt.Horizontal)

# 创建3D散点图窗口
w = create_3d_scatter_plot()

# 创建2D散点图窗口
win = create_2d_scatter_plot()

# 将两个窗口添加到QSplitter中
splitter.addWidget(w)
splitter.addWidget(win)

# 将QSplitter添加到水平布局中
hbox.addWidget(splitter)

# 显示主窗口
main_window.show()

# 运行应用程序
app.exec_()
相关推荐
递归不收敛21 小时前
Conda 常用命令汇总(新手入门笔记)
笔记·conda
前端橙一陈1 天前
Salesforce Developer Edition(开发者版) 搭建测试环境
经验分享·笔记·其他
电子小子洋酱1 天前
BearPi小熊派 鸿蒙入门开发笔记(4)
笔记·华为·harmonyos
摇滚侠1 天前
Spring Boot 3零基础教程,WEB 开发 通过配置类代码方式修改静态资源配置 笔记32
java·spring boot·笔记
聪明的笨猪猪1 天前
Java JVM “内存(1)”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
_dindong1 天前
Linux网络编程:Socket编程TCP
linux·服务器·网络·笔记·学习·tcp/ip
摇滚侠1 天前
Spring Boot 3零基础教程,WEB 开发 Thymeleaf 属性优先级 行内写法 变量选择 笔记42
java·spring boot·笔记
摇滚侠1 天前
Spring Boot 3零基础教程,WEB 开发 Thymeleaf 总结 热部署 常用配置 笔记44
java·spring boot·笔记
rechol1 天前
汇编与底层编程笔记
汇编·arm开发·笔记
lzj_pxxw1 天前
嵌入式开发技巧:舍弃标志位,用宏定义函数实现程序单次运行
笔记·stm32·单片机·嵌入式硬件·学习