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

一、装pyqtgraph和PyOpenGL库

pip install pyqtgraph
pip install PyOpenGL

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

二、3D散点图效果

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容易出问题

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,并且稍微修饰一下边框,代码如下

# 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_()

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 20:56:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 20:56:01       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 20:56:01       58 阅读
  4. Python语言-面向对象

    2024-07-11 20:56:01       69 阅读

热门阅读

  1. 关系代数中的八种基本运算

    2024-07-11 20:56:01       21 阅读
  2. Oracle 确认被锁对象

    2024-07-11 20:56:01       19 阅读
  3. 【思考Spring Security框架的相关问题】

    2024-07-11 20:56:01       24 阅读
  4. 在SpringBoot使用AOP防止接口重复提交

    2024-07-11 20:56:01       22 阅读
  5. Spring AOP的几种实现方式

    2024-07-11 20:56:01       18 阅读
  6. pytorch 模型保存到本地之后,如何继续训练

    2024-07-11 20:56:01       22 阅读
  7. 【Spring】springSecurity使用

    2024-07-11 20:56:01       16 阅读
  8. 力扣682.棒球比赛

    2024-07-11 20:56:01       17 阅读
  9. STM32学习历程(day4)

    2024-07-11 20:56:01       20 阅读
  10. C# 装饰器模式(Decorator Pattern)

    2024-07-11 20:56:01       20 阅读
  11. 代码随想录-DAY⑦-字符串——leetcode 344 | 541 | 151

    2024-07-11 20:56:01       21 阅读
  12. FastAPI+SQLAlchemy数据库连接

    2024-07-11 20:56:01       19 阅读
  13. 关于vue监听数组

    2024-07-11 20:56:01       18 阅读
  14. SQL 自定义函数

    2024-07-11 20:56:01       22 阅读