Lavínia Beghini
Lavínia Beghini

Reputation: 275

Scalable MatPlotLib Figure with PyQt window

Since my previous question here at StackOverflow I was able to make my plot touch the edges of my Figure:

plot with graph touching Figure edges

However as you can see it does not keep the PyQt Window scaling, I do not know how to make the Figure size coincide with my Window size.

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

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


class MplCanvas(FigureCanvasQTAgg):
    def __init__(self, parent=None):
        figure = plt.figure()
        figure.set_figheight(6)
        figure.set_figwidth(6)
        plt.tight_layout()
        figure.patch.set_visible(False)
        super(MplCanvas, self).__init__(figure)
        self.setParent(parent)
        self.ax = plt.axes([0,0,1,1], frameon=False)
        self.ax.axis('off')
        self.ax.get_xaxis().set_visible(False)
        self.ax.get_yaxis().set_visible(False)
        self.graph = EditableGraph([(0, 1)], ax=self.ax)
        plt.autoscale(axis='both', tight=True)
        self.updateGeometry()


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

        self.canvas = MplCanvas(self)

        self.vbl = QtWidgets.QVBoxLayout()
        self.vbl.addWidget(self.canvas)
        self.setLayout(self.vbl)


def main():

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


if __name__ == "__main__":
    main()

And I am also facing a strange behaviour. If I change the figure height and width to different values (8 and 6 por exemple) like so:

figure.set_figheight(8)
figure.set_figwidth(6)

It stops touching the border: Image with graph not touching the border

I can not figure why it would not work for rectangle plots.

Does anyone have a clue of what I can do to perfect this behavior?

Upvotes: 0

Views: 531

Answers (1)

OualidSai
OualidSai

Reputation: 117

You can use this code:

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

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


class MplCanvas(FigureCanvasQTAgg):
    def __init__(self, parent=None):
        figure = plt.figure()
        figure.set_figheight(6)
        figure.set_figwidth(6)
        plt.tight_layout()
        figure.patch.set_visible(False)
        super(MplCanvas, self).__init__(figure)
        self.setParent(parent)
        self.ax = plt.axes([0,0,1,1], frameon=False)
        self.ax.axis('off')
        self.ax.get_xaxis().set_visible(False)
        self.ax.get_yaxis().set_visible(False)
        self.graph = EditableGraph([(0, 1)], ax=self.ax)
        plt.autoscale(axis='both', tight=True)
        self.updateGeometry()


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

        self.canvas = MplCanvas(self)
        self.lbl = QtWidgets.QLabel(self)
        self.setCentralWidget(self.canvas)
        self.setFixedHeight(700)
        self.setFixedWidth(700)


def main():

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


if __name__ == "__main__":
    main()

enter image description here

Upvotes: 1

Related Questions