python - 如何使 FigureCanvas 填充嵌入在 pyqt GUI 中的 matplotlib 小部件中的整个图形?

标签 python matplotlib pyqt4

我试图制作一个带有嵌入式 matplotlib 小部件的 GUI。我只需要 FigureCanvas 完全填充图形,我已经尝试了大约 100 种不同的东西,但没有一点改变 Canvas 的大小。我在“#useless”表示的代码中留下了一些我的尝试,让您知道我已经尝试过了,但没有任何影响。请帮忙。

import sys
from PyQt4 import QtGui
from matplotlib import pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

class Terminal(QtGui.QDialog):
def __init__(self, parent=None):
    try:        
        super(Terminal, self).__init__(parent)

        self.resize(1000,800)
        self.figure = plt.figure(facecolor='black')
        self.canvas = FigureCanvas(self.figure)
        self.canvas.autoFillBackground()#useless
        self.myComboBox = QtGui.QComboBox()
        self.plotButton = QtGui.QPushButton('Plot')      
        self.xMplWidget = MatplotlibWidget(self.canvas)         
        self.plotButton.clicked.connect(self.plotCircles)

        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.canvas)
        layout.addWidget(self.myComboBox)
        layout.addWidget(self.plotButton)
        policy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)#useless
        self.canvas.setSizePolicy = policy#useless
        layout.setStretchFactor(self.canvas, 1)#useless
        self.setLayout(layout)
        self.canvas.autoFillBackground()#useless
    except Exception as err:
        print("Error in Terminal.init: Other - ", err)


def createDashboard(self,oDashboard):
    print("DoStuff")

def plotCircles(self):
    print("DoStuff")        




class MatplotlibWidget(FigureCanvas):
    def __init__(self, parent=None, title='Title', xlabel='x label', ylabel='y label', dpi=100, hold=False):
    super(MatplotlibWidget, self).__init__(Figure())       
    self.setParent(parent)
    self.figure = Figure(dpi=dpi)
    self.canvas = FigureCanvas(self.figure)

    self.theplot = self.figure.add_subplot(111)
    self.theplot.set_title(title)
    self.theplot.set_xlabel(xlabel)
    self.theplot.set_ylabel(ylabel)


def plotChart(self,oOxyName):
    print("DoStuff")     


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    main = Terminal()
    main.show()

    app.exec_()

enter image description here

最佳答案

我继续研究后想通了。我在项目的早期就把事情搞得一团糟,并且在更改初始设计时没有仔细阅读我的代码。问题是我正在创建 Canvas 小部件并将该小部件传递给 MplWidget。 MplWidget 正确地拥有自己的 Canvas ,因此不需要额外的 Canvas 传递给它。本质上,我是在 Canvas 小部件中制作 MplWidget,而不是在主窗体中制作 MplWidget。

这里是更正后的代码以及更正的注释。

import sys
from PyQt4 import QtGui
from PyQt4.QtCore import Qt
from matplotlib import pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import (
    FigureCanvasQTAgg as FigureCanvas)

class Terminal(QtGui.QDialog):
    def __init__(self, parent=None):
        try:        
            super(Terminal, self).__init__(parent)

            self.resize(1000,800)
            self.figure = plt.figure(facecolor='black')
#This was creating a canvas which I was adding my widget to (bad idea)
#            self.canvas = FigureCanvas(self.figure)

            self.myComboBox = QtGui.QComboBox()
            self.plotButton = QtGui.QPushButton('Plot')      
            self.xMplWidget = MatplotlibWidget()         
            self.plotButton.clicked.connect(self.plotCircles)

            layout = QtGui.QVBoxLayout()
            #Needed to add my MatplotlibWidget, not a canvas widget, because the MPLWidget has its own canvas
            #layout.addWidget(self.canvas)
            layout.addWidget(self.xMplWidget) #This was added

            layout.addWidget(self.myComboBox)
            layout.addWidget(self.plotButton)
            self.setLayout(layout)
        except Exception as err:
            print("Error in Terminal.init: Other - ", err)


    def createDashboard(self,oDashboard):
        print("DoStuff")

    def plotCircles(self):
        print("DoStuff")        


class MatplotlibWidget(FigureCanvas):
    def __init__(self, parent=None, title='Title', xlabel='x label', ylabel='y label', dpi=100, hold=False):
        super(MatplotlibWidget, self).__init__(Figure())       
        self.setParent(parent)
        self.figure = Figure(dpi=dpi)
        self.canvas = FigureCanvas(self.figure)

        self.theplot = self.figure.add_subplot(111)
        self.theplot.set_title(title)
        self.theplot.set_xlabel(xlabel)
        self.theplot.set_ylabel(ylabel)


    def plotChart(self,oOxyName):
        print("DoStuff")     


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    main = Terminal()
    main.show()

    app.exec_()

关于python - 如何使 FigureCanvas 填充嵌入在 pyqt GUI 中的 matplotlib 小部件中的整个图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39650940/

相关文章:

java - Jython,只使用来自 Java 的 Python 的方法?

python - 如何在 PyQT 中打开新窗口时关闭窗口?

Python函数来检查是否还活着

python - 带有 rpy2 和多处理的 Pandas

python - 是否可以在不在 URL 中使用语言代码的情况下拥有多语言 django CMS 站点?

python - 使用matplotlib设置x轴

python - 子图中的 Seaborn 热图 - 对齐 x 轴

python - 使用 matplotlib/python 设置绘图轴限制时出现问题

python - 通过 QtCore.SignalMapper 调用槽

python-2.7 - 如何使用 PyQT 提示跟随 slider 的处理程序?