gpt4 book ai didi

python - 如何在 PyQt 中嵌入 scikitplot?

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:56 28 4
gpt4 key购买 nike

有一个名为 scikitplot 的包,其中有一些对我的应用程序非常有用的工具。只需调用一个函数,它就可以自动绘制一些特定的图表。问题是我需要将这些图嵌入到 PyQt 窗口中。我知道在使用 matplotlib it is possible to do this 时通过使用 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

关于python - 如何在 PyQt 中嵌入 scikitplot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54369390/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com