import sys from PySide6 import QtWidgets import pyqtgraph as pg import numpy as np class MultiCurvePlotWidget(QtWidgets.QWidget): def __init__(self, parent=None): super().__init__(parent) # 创建布局并添加 PlotWidget layout = QtWidgets.QVBoxLayout() self.plot_widget = pg.PlotWidget() layout.addWidget(self.plot_widget) self.setLayout(layout) # 生成数据 x = np.linspace(0, 10, 100) y1 = np.sin(x) # 第一条曲线:正弦函数 y2 = np.cos(x) # 第二条曲线:余弦函数 y3 = np.tan(x) / 10 # 第三条曲线:正切函数(缩小范围以显示) # 绘制多条曲线 self.plot_widget.plot(x, y1, pen='r', name="sin(x)") self.plot_widget.plot(x, y2, pen='g', name="cos(x)") self.plot_widget.plot(x, y3, pen='b', name="tan(x) / 10") # 添加图例 self.plot_widget.addLegend() class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Multi-Curve Plot in QWidget") # 将 MultiCurvePlotWidget 添加到主窗口 self.plot_widget = MultiCurvePlotWidget() self.setCentralWidget(self.plot_widget) if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) main = MainWindow() main.show() sys.exit(app.exec_())