【问题标题】:How to embed scikitplot in PyQt?如何在 PyQt 中嵌入 scikitplot?
【发布时间】:2019-01-26 01:04:16
【问题描述】:

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

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

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

【问题讨论】:

    标签: python matplotlib pyqt


    【解决方案1】:

    您需要在应用程序中创建坐标轴并将其传递给绘图函数。

        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,因此这是在未经测试的情况下从我的脑海中写出来的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2012-09-09
      相关资源
      最近更新 更多