Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将雷达图形集成到小部件中_Python_Python 3.x_Matplotlib_Pyqt_Pyqt5 - Fatal编程技术网

Python 将雷达图形集成到小部件中

Python 将雷达图形集成到小部件中,python,python-3.x,matplotlib,pyqt,pyqt5,Python,Python 3.x,Matplotlib,Pyqt,Pyqt5,我试图将这个雷达图形()的代码集成到一个包含小部件的GUI中。 我不想在小部件中绘制x和y,而是想绘制雷达图形,但我无法解决这个问题。 我们的想法是替换这段代码: x = [2000,2001,2002,2003,2004] y = [10,20,30,40,50] self.axes.plot(x, y 使用上面链接中的代码 下面是我的GUI后端的代码: import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWi

我试图将这个雷达图形()的代码集成到一个包含小部件的GUI中。 我不想在小部件中绘制x和y,而是想绘制雷达图形,但我无法解决这个问题。 我们的想法是替换这段代码:

x = [2000,2001,2002,2003,2004]
y = [10,20,30,40,50]
self.axes.plot(x, y
使用上面链接中的代码

下面是我的GUI后端的代码:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QSizePolicy
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from front_end import Ui_MainWindow

class Graph_init(FigureCanvas):

    def __init__(self, parent=None):

        fig = Figure()
        self.axes = fig.add_subplot(111)
        self.compute_initial_figure()

        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)


class Graph_populate(Graph_init):

    def compute_initial_figure(self):

        x = [2000,2001,2002,2003,2004]
        y = [10,20,30,40,50]
        self.axes.plot(x, y)


class GUI(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(GUI, self).__init__(parent)
        self.setupUi(self)

        self.sc = Graph_populate(self.widget)
        self.gridLayout.addWidget(self.sc, 0, 1, 1, 1)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    prog = GUI()
    prog.showMaximized()
    sys.exit(app.exec_())
前端:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(595, 393)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setMaximumSize(QtCore.QSize(100, 16777215))
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setMinimumSize(QtCore.QSize(0, 200))
        self.widget.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.widget.setObjectName("widget")
        self.gridLayout.addWidget(self.widget, 0, 1, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 595, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

谢谢

雷达类型图形是带有极轴投影的图形,因此在创建轴时必须激活该属性

self.axes = fig.add_subplot(111, projection="polar")
使用plot函数时,第一个参数是以弧度为单位的角度,第二个参数是半径的长度

class Graph_init(FigureCanvas):
    def __init__(self, parent=None):
        fig = Figure()
        self.axes = fig.add_subplot(111, projection="polar")
        self.compute_initial_figure()

        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)


class Graph_populate(Graph_init):
    def compute_initial_figure(self):
        import math
        r =[10, 20, 30, 40, 50]
        theta = [0, 50, 90, 120, 180]
        angles = [angle*math.pi/180 for angle in theta]
        self.axes.plot(angles, r)

问题是我无法将链接中的代码集成到GUI中