How do I auto fit a Matplotlib figure inside a PySide QFrame?
matplotlib, pyside, python-3.x
Solution
Here's a working code sample that shows you how to get it working. I first thought it was because of the stretch you added to the layout, which will use up the additional space around the other widgets. But when I removed it, it still wouldn't resize. The 'easy' solution is to add a resizeEvent, which lets you define the size of your GraphView widget. In this case I just set its geometry to be that of the QFrame, though you might want to add some padding and make sure you set a sensible minimum size for the QFrame.
from PySide import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import sys
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.fft_frame = FftFrame(self)
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.fft_frame)
self.setLayout(self.layout)
self.setCentralWidget(self.fft_frame)
class FftFrame(QtGui.QFrame):
def __init__(self, parent=None):
super(FftFrame, self).__init__(parent)
self.setFrameShape(QtGui.QFrame.StyledPanel)
self.parent = parent
self.graph_view = GraphView('fftFrame', 'FFT Transform:', 'FFT Transform of Signal', self)
def resizeEvent(self, event):
self.graph_view.setGeometry(self.rect())
class GraphView(QtGui.QWidget):
def __init__(self, name, title, graph_title, parent = None):
super(GraphView, self).__init__(parent)
self.name = name
self.graph_title = graph_title
self.dpi = 100
self.fig = Figure((5.0, 3.0), dpi = self.dpi, facecolor = (1,1,1), edgecolor = (0,0,0))
self.axes = self.fig.add_subplot(111)
self.canvas = FigureCanvas(self.fig)
self.canvas.setParent(self)
self.Title = QtGui.QLabel(self)
self.Title.setText(title)
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.Title)
self.layout.addWidget(self.canvas)
self.layout.setStretchFactor(self.canvas, 1)
self.setLayout(self.layout)
self.canvas.show()
def update_graph(self, data, title = None):
self.axes.clear()
self.axes.plot(data)
if title != None:
self.axes.set_title(title)
self.canvas.draw()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
Problem
I'm creating a simple PySide application that also uses MatPlotLib. However, when I add the figure into a `QFrame`, the figure doesn't automatically fit to the frame: My graph is created using the following code: ``` class GraphView(gui.QWidget): def __init__(self, name, title, graphTitle, parent = None): super(GraphView, self).__init__(parent) self.name = name self.graphTitle = graphTitle self.dpi = 100 self.fig = Figure((5.0, 3.0), dpi = self.dpi, facecolor = (1,1,1), edgecolor = (0,0,0)) self.axes = self.fig.add_subplot(111) self.canvas = FigureCanvas(self.fig) self.Title = gui.QLabel(self) self.Title.setText(title) self.layout = gui.QVBoxLayout() self.layout.addStretch(1) self.layout.addWidget(self.Title) self.layout.addWidget(self.canvas) self.setLayout(self.layout) def UpdateGraph(self, data, title = None): self.axes.clear() self.axes.plot(data) if title != None: self.axes.set_title(title) self.canvas.draw() ``` And it's added to the main Widget like so: ``` # Create individual Widget/Frame (fftFrame) fftFrame = gui.QFrame(self) fftFrame.setFrameShape(gui.QFrame.StyledPanel) self.FFTGraph = GraphView('fftFrame', 'FFT Transform:', 'FFT Transform of Signal', fftFrame) ```