Tho Re
Tho Re

Reputation: 195

How to adjust the size of a plot in PyQt?

I have the following code which I found here. I reduced the code a bit to make it more suitable to my question.

import sys
import matplotlib
matplotlib.use("Qt5Agg")
from PyQt5 import QtCore
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject
from PyQt5.QtWidgets import *
from numpy import arange, sin, pi
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

class MyMplCanvas(FigureCanvas):
    """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
    def __init__(self, parent=None, width = 5, height = 3, dpi=100):

        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)

        self.compute_initial_figure()
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.updateGeometry(self)

class MyStaticMplCanvas(MyMplCanvas):
    """Simple canvas with a sine plot."""
    def compute_initial_figure(self):
        t = arange(0.0, 3.0, 0.01)
        s = sin(2*pi*t)
        self.axes.plot(t, s)
        self.axes.set_ylabel('label2')
        self.axes.set_xlabel('label1')
        self.axes.grid(True)

class ApplicationWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumWidth(800)
        self.setMinimumHeight(300)
        self.setMaximumWidth(800)
        self.setMaximumHeight(300)

        self.main_widget = QWidget(self)

        self.sc = MyStaticMplCanvas(self.main_widget, width=5, height=4, dpi=100)

        l = QVBoxLayout(self.main_widget)
        l.addWidget(self.sc)
        self.setCentralWidget(self.main_widget)

if __name__ == '__main__':
    app = QApplication(sys.argv)

    aw = ApplicationWindow()
    aw.setWindowTitle("PyQt5 Matplotlib Example")
    aw.show()
    app.exec_()

The problem is that the plot is shown in principle correctly but the x-label is missing (fell out of the frame which is displayed). So, how can I adjust the size of the axes object to make PyQt also display the x-label?

I know that the figure size is adjustable through the 'figsize' argument. But so far I could not find a similar command for a diagram inside of the figure.

Also, I heard of the gridspec package of matplotlib but I think it is not suitable here since I only have one plot to display.

Upvotes: 4

Views: 13687

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339700

You may want to read the thight layout guide.

So one option is to call

self.fig.tight_layout()

You can also adjust the subplot parameters

self.fig.subplots_adjust(0.2, 0.2, 0.8, 0.8) # left,bottom,right,top 

You can also set the position of the axes

self.axes.set_position([0.2, 0.2, 0.6, 0.6]) # left,bottom,width,height 

Upvotes: 4

Tho Re
Tho Re

Reputation: 195

I solved the problem and it turned out that one can simply adjust the axes through the position argument.

self.ax = self.fig.add_subplot(111, position=[0.15, 0.15, 0.75, 0.75])

Upvotes: 1

Related Questions