0

我想为我的一个项目绘制一些交互式图表。我无法弄清楚如何将 matplotlib 图嵌入到 PySide6 应用程序中,也找不到示例代码。

我只是想要一个嵌入在 PySide6 中的 matplotlib 图的示例代码来帮助我入门。

我找到了这段代码:

import sys
import matplotlib

matplotlib.use('Qt5Agg')

from PySide6.QtWidgets import QMainWindow, QApplication

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure


class MplCanvas(FigureCanvasQTAgg):

    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        super(MplCanvas, self).__init__(fig)


class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        # Create the maptlotlib FigureCanvas object,
        # which defines a single set of axes as self.axes.
        sc = MplCanvas(self, width=5, height=4, dpi=100)
        sc.axes.plot([0,1,2,3,4], [10,1,20,3,40])
        self.setCentralWidget(sc)

        self.show()


app = QApplication(sys.argv)
w = MainWindow()
app.exec_()

这段代码的问题是,每当我运行它时,我都会收到这个错误:

Traceback (most recent call last):
  File "", line 35, in <module>
    w = MainWindow()
  File "", line 29, in __init__
    self.setCentralWidget(sc)
TypeError: 'PySide6.QtWidgets.QMainWindow.setCentralWidget' called with wrong argument types:
  PySide6.QtWidgets.QMainWindow.setCentralWidget(MplCanvas)
Supported signatures:
  PySide6.QtWidgets.QMainWindow.setCentralWidget(PySide6.QtWidgets.QWidget)
4

0 回答 0