如何在PyQt中嵌入scikitplot?

2024-03-28 21:49:37 发布

您现在位置:Python中文网/ 问答频道 /正文

有一个名为scikitplot的包,其中包含一些对我的应用程序非常有用的工具。它可以通过调用单个函数自动绘制一些特定的图形。问题是我需要在PyQt窗口中嵌入这些绘图。我知道在使用PyQt后端处理matplotlibit is possible to do this时。但是,在这种情况下,我不知道如何继续,因为scikitplot函数每个函数都返回一个plot,我也不知道如何将现有plot添加到figure小部件中。你知道吗

代码应该是这样的(显然不起作用,但我希望它能帮助解释我的问题):

import sys
from PyQt5 import QtWidgets
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
import scikitplot as skplt
from sklearn.naive_bayes import GaussianNB

class ExampleWindow(QtWidgets.QMainWindow):        
    def __init__(self, parent=None):
        super().__init__(parent)
        self._main = QtWidgets.QWidget()
        self.setCentralWidget(self._main)

        ## Lines to make minimal example
        X, y = load_breast_cancer(return_X_y=True)
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
        nb = GaussianNB()
        nb.fit(X_train, y_train)
        predicted_probas = nb.predict_proba(X_test)

        ## The plots I want to show in the window
        ## It doesn't work because they aren't widgets, but I hope you get the idea
        plot1 = skplt.metrics.plot_cumulative_gain(y_test, predicted_probas)
        plot2 = skplt.metrics.plot_roc(y_test, predicted_probas)

        layout = QtWidgets.QHBoxLayout()
        layout.addWidget(plot1)
        layout.addWidget(plot2)
        self.setLayout(layout)
        self.showMaximized()

if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    ex = ExampleWindow()
    ex.show()
    sys.exit(app.exec_())

Tags: to函数fromtestimportselfplotmain
1条回答
网友
1楼 · 发布于 2024-03-28 21:49:37

您需要在应用程序中创建轴并将其传递给plotting函数。你知道吗

    self.figure1 = matplotlib.figure.Figure()
    self.canvas1 = FigureCanvas(self.figure1)
    self.toolbar1 = NavigationToolbar(self.canvas1, self)
    self.ax1 = self.figure1.add_subplot(111)
    layout.addWidget(self.canvas1)
    layout.addWidget(self.toolbar)

    plot1 = skplt.metrics.plot_cumulative_gain(y_test, predicted_probas, ax=self.ax1)

第二个情节也一样。你知道吗

请注意,这是从我的头顶写的,没有测试,因为我没有可用的scikitplot。你知道吗

相关问题 更多 >