簡體   English   中英

如何在 PyQt 中嵌入 scikitplot?

[英]How to embed scikitplot in PyQt?

有一個名為scikitplot的包,其中包含一些對我的應用程序非常有用的工具。 只需調用一個函數,它就有可能自動繪制一些特定的圖形。 問題是我需要將這些圖嵌入到 PyQt 窗口中。 我知道在使用matplotlib ,可以通過使用 PyQt 后端來做到這一點 但是,在這種情況下,我真的不知道如何進行,因為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_())

您需要在應用程序中創建軸並將其傳遞給繪圖函數。

    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 ,因此這是從頭開始寫的,沒有經過測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM