How can I make the FigureCanvas fill the entire Figure in a matplotlib widget embedded in a pyqt GUI?
<p>I have attempted to make a GUI with embedded matplotlib widget in it. I just need the figure to be completely filled by the FigureCanvas, I have tried about 100 different things and nothing has changed the size of the canvas one bit. I left a few of my attempts in the code denoted by "#useless" to let you know that I tried it and it had no impact. Please help.</p>

<pre><code>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_()
</code></pre>

<p><a href="http://i.stack.imgur.com/MAE37.png" rel="nofollow"><img src="http://i.stack.imgur.com/MAE37.png" alt="enter image description here"></a></p>

<p>I figured it out after continued studying. I had made a mess early on in the project and had failed to carefully reread my code when changing the initial design. The issue was that I was creating a canvas widget and passing that widget to the MplWidget. The MplWidget correctly had its own canvas, and therefor did not need an additional one passed to it. Essentially I was making the MplWidget within the canvas widget, rather than the MplWidget within the main form. </p>

<p>Here is the corrected code with notes on the corrections.</p>

<pre><code>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_()
</code></pre>

python matplotlib pyqt4