Ui中绘图

固定为3列(子图)

主要是想实现在弹框里绘制图形,而且固定为三列。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 导入模块
import sys
import numpy as np
import pyqtgraph as pg
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PyQt5 import QtCore

class MainWindow(QMainWindow):
def __init__(self):
super().__init__()

self.setWindowTitle("Curve Plot")
self.resize(800, 600)

# 创建主布局
main_layout = QVBoxLayout()

# 创建ViewBox作为绘图容器
self.plot_widget = pg.GraphicsLayoutWidget()

# 将ViewBox添加到主布局
main_layout.addWidget(self.plot_widget)

# 设置central widget
central_widget = QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)

def plot_curves(self, data):
# 计算行数
num_plots = len(data)
rows = (num_plots + 2) // 3

# 清空绘图容器
self.plot_widget.clear()

# 创建子图,并将其添加到绘图容器
for i, curve_data in enumerate(data):
row = i // 3
col = i % 3

plot = self.plot_widget.addPlot(row=row, col=col)
plot.plot(curve_data)

app = QApplication(sys.argv)
window = MainWindow()
window.show()

# 模拟数据
t = np.linspace(0, 10, 100)
data = [np.sin(t), np.cos(t), np.tan(t), np.exp(-t), 1 / (t + 1)]

window.plot_curves(data)

sys.exit(app.exec_())