0

I am trying to place a FigureCanvasQTAgg inside a window using PyQt, however I am not being able to make the canvas of the Window size nor scalable. That means I want the corners of the Figure to match the corner of the window.

visual demonstration of the behaviour

I have tried some of the solutions described here but without any success.

I am using the following code for testing:

import sys
import matplotlib; matplotlib.use("Qt5Agg")

from PyQt5 import QtWidgets, QtCore # <- additional import
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT
from matplotlib.figure import Figure
from netgraph import EditableGraph


class MplCanvas(FigureCanvasQTAgg):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        super(MplCanvas, self).__init__(Figure(figsize=(width, height), dpi=dpi))
        self.setParent(parent)
        self.ax = self.figure.add_subplot(111)
        self.graph = EditableGraph([(0, 1)], ax=self.ax)


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
        self.canvas.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.canvas.setFocus()

        self.setCentralWidget(self.canvas)

def main():
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    app.exec_()


if __name__ == "__main__":
    main()
Lavínia Beghini
  • 165
  • 1
  • 11
  • try add `self.updateGeometry()` after `self.graph = EditableGraph([(0, 1)], ax=self.ax)` – eyllanesc May 22 '22 at 20:17
  • Ii still faced the same behavior of a smaller canvas box inside the window... I wonder if it has something to do with the `figsize` or `dpi` paremeters I pass to `Figure`? I tried to change them but without any changes either. – Lavínia Beghini May 22 '22 at 22:28

0 Answers0