Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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 使用PyQt和Matplotlib画布进行复杂的类实例化_Python_Oop_Pyqt - Fatal编程技术网

Python 使用PyQt和Matplotlib画布进行复杂的类实例化

Python 使用PyQt和Matplotlib画布进行复杂的类实例化,python,oop,pyqt,Python,Oop,Pyqt,我有一个带有多个窗口的应用程序,每个窗口都有自己的Matplotlib画布。我已经为绘图设置了一个PlotCanvas,以及一个WidgetPlot包装器,该包装器决定GUI布局并允许连接到后端,例如快捷方式 无论如何,我希望每个窗口都用 self.m = WidgetPlot(ax_layout = "triple") # <- How do I make sure this argument reaches PlotCanvas? self.canvas = self.m.canvas

我有一个带有多个窗口的应用程序,每个窗口都有自己的Matplotlib画布。我已经为绘图设置了一个
PlotCanvas
,以及一个
WidgetPlot
包装器,该包装器决定GUI布局并允许连接到后端,例如快捷方式

无论如何,我希望每个窗口都用

self.m = WidgetPlot(ax_layout = "triple") # <- How do I make sure this argument reaches PlotCanvas?
self.canvas = self.m.canvas
self.mpl_LayoutBox.addWidget(self.m)

其他参数的右边必须有一个附加参数,最好有一个默认值:

from PyQt5.Qt import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

class WidgetPlot(QWidget):
    def __init__(self, parent=None, ax_layout=""):
        QWidget.__init__(self, parent)
        self.setLayout(QVBoxLayout())
        self.canvas = PlotCanvas(self, ax_layout=ax_layout)
        self.layout().addWidget(self.canvas)

class PlotCanvas(FigureCanvas):
    def __init__(self, parent = None, width = 1, height = 1, dpi = 100, ax_layout=""):
        self.fig = Figure(figsize = (width, height), dpi = dpi, tight_layout = True)
        self.ax_layout = ax_layout

        if self.ax_layout == "triple":
            self.ax1 = self.fig.add_subplot(131)
            self.ax2 = self.fig.add_subplot(132)
            self.ax3 = self.fig.add_subplot(133)

            self.ax1.plot(range(100), color = "green")
            self.ax2.plot(range(100), color = "red")
            self.ax3.plot(range(100), color = "blue")

        elif self.ax_layout == "single":
            self.ax = self.fig.add_subplot(111)
            self.ax.plot(range(100), color = "black")
        else:
            raise ValueError

        FigureCanvas.__init__(self, self.fig)

class Ui_TraceWindow(object):
    def setupUi(self, TraceWindow):
        TraceWindow.setObjectName("TraceWindow")
        TraceWindow.resize(1086, 500)
        TraceWindow.setMinimumSize(QSize(900, 500))
        TraceWindow.setMaximumSize(QSize(5000, 2000))
        self.centralWidget = QWidget(TraceWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.gridLayout_2 = QGridLayout(self.centralWidget)
        self.gridLayout_2.setContentsMargins(11, 11, 11, 11)
        self.gridLayout_2.setSpacing(6)
        self.gridLayout = QGridLayout()
        self.gridLayout.setSpacing(6)
        self.mpl_LayoutBox = QVBoxLayout()
        self.mpl_LayoutBox.setSpacing(6)
        self.gridLayout.addLayout(self.mpl_LayoutBox, 3, 1, 1, 1)
        self.gridLayout_2.addLayout(self.gridLayout, 1, 0, 1, 1)
        TraceWindow.setCentralWidget(self.centralWidget)


        # Instantiate canvas here
        self.m = WidgetPlot(ax_layout = "triple") # <- How do I make sure this argument reaches PlotCanvas?
        self.canvas = self.m.canvas
        self.mpl_LayoutBox.addWidget(self.m)


if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    TraceWindow = QMainWindow()
    ui = Ui_TraceWindow()
    ui.setupUi(TraceWindow)
    TraceWindow.show()
    sys.exit(app.exec_())
来自PyQt5.Qt导入*
从matplotlib.backends.backend_qt5agg导入FigureCanvas qtagg as FigureCanvas
从matplotlib.figure导入图形
类WidgetPlot(QWidget):
def uuu init uuuu(self,parent=None,ax_layout=“”):
QWidget.\uuuuu init\uuuuuuu(自我,父)
self.setLayout(QVBoxLayout())
self.canvas=PlotCanvas(self,ax\u布局=ax\u布局)
self.layout().addWidget(self.canvas)
类PlotCanvas(FigureCanvas):
def uuu init uuuuu(self,parent=None,width=1,height=1,dpi=100,ax\u layout=“”):
self.fig=图形(figsize=(宽度、高度),dpi=dpi,紧密布局=True)
self.ax\u布局=ax\u布局
如果self.ax_布局==“三重”:
self.ax1=self.fig.add_子批次(131)
self.ax2=self.fig.add_子批次(132)
self.ax3=self.fig.add_子批次(133)
self.ax1.plot(范围(100),color=“绿色”)
self.ax2.plot(范围(100),color=“红色”)
self.ax3.plot(范围(100),color=“蓝色”)
elif self.ax_布局==“单个”:
self.ax=self.fig.add_子批次(111)
自轴绘图(范围(100),颜色=“黑色”)
其他:
升值误差
FigureCanvas.\uuuu init\uuuuu(self,self.fig)
类Ui\u跟踪窗口(对象):
def设置UI(自身、跟踪窗口):
setObjectName(“TraceWindow”)
TraceWindow.resize(1086500)
跟踪宽度设置最小尺寸(QSize(900500))
TraceWindow.setMaximumSize(QSize(50002000))
self.centralWidget=QWidget(TraceWindow)
self.centralWidget.setObjectName(“centralWidget”)
self.gridLayout_2=QGridLayout(self.centralWidget)
self.gridLayout_2.setContentsMargins(11,11,11,11)
self.gridLayout_2.setspace(6)
self.gridLayout=QGridLayout()
self.gridLayout.setspace(6)
self.mpl_LayoutBox=QVBoxLayout()
self.mpl_LayoutBox.setspace(6)
self.gridLayout.addLayout(self.mpl\u LayoutBox,3,1,1,1)
self.gridLayout_2.addLayout(self.gridLayout,1,0,1,1)
TraceWindow.setCentralWidget(self.centralWidget)
#在这里实例化画布
self.m=WidgetPlot(ax_layout=“triple”)#
from PyQt5.Qt import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

class WidgetPlot(QWidget):
    def __init__(self, parent=None, ax_layout=""):
        QWidget.__init__(self, parent)
        self.setLayout(QVBoxLayout())
        self.canvas = PlotCanvas(self, ax_layout=ax_layout)
        self.layout().addWidget(self.canvas)

class PlotCanvas(FigureCanvas):
    def __init__(self, parent = None, width = 1, height = 1, dpi = 100, ax_layout=""):
        self.fig = Figure(figsize = (width, height), dpi = dpi, tight_layout = True)
        self.ax_layout = ax_layout

        if self.ax_layout == "triple":
            self.ax1 = self.fig.add_subplot(131)
            self.ax2 = self.fig.add_subplot(132)
            self.ax3 = self.fig.add_subplot(133)

            self.ax1.plot(range(100), color = "green")
            self.ax2.plot(range(100), color = "red")
            self.ax3.plot(range(100), color = "blue")

        elif self.ax_layout == "single":
            self.ax = self.fig.add_subplot(111)
            self.ax.plot(range(100), color = "black")
        else:
            raise ValueError

        FigureCanvas.__init__(self, self.fig)

class Ui_TraceWindow(object):
    def setupUi(self, TraceWindow):
        TraceWindow.setObjectName("TraceWindow")
        TraceWindow.resize(1086, 500)
        TraceWindow.setMinimumSize(QSize(900, 500))
        TraceWindow.setMaximumSize(QSize(5000, 2000))
        self.centralWidget = QWidget(TraceWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.gridLayout_2 = QGridLayout(self.centralWidget)
        self.gridLayout_2.setContentsMargins(11, 11, 11, 11)
        self.gridLayout_2.setSpacing(6)
        self.gridLayout = QGridLayout()
        self.gridLayout.setSpacing(6)
        self.mpl_LayoutBox = QVBoxLayout()
        self.mpl_LayoutBox.setSpacing(6)
        self.gridLayout.addLayout(self.mpl_LayoutBox, 3, 1, 1, 1)
        self.gridLayout_2.addLayout(self.gridLayout, 1, 0, 1, 1)
        TraceWindow.setCentralWidget(self.centralWidget)


        # Instantiate canvas here
        self.m = WidgetPlot(ax_layout = "triple") # <- How do I make sure this argument reaches PlotCanvas?
        self.canvas = self.m.canvas
        self.mpl_LayoutBox.addWidget(self.m)


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