【发布时间】: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