2

I've been trying to set a fixed size padding (in pixels) on my matplotlib figure. I've never been able to find a solution that suits my needs on the internet so I had to make a little workaround for this, which is done with the following code :

def resizeEvent(self,e):
    windowWidth=self.geometry().width()
    windowHeight=self.geometry().height()
    #print(windowWidth,windowHeight)
    figure.subplots_adjust(left=100.0/windowWidth,right=1-100.0/windowWidth, top=1-100.0/windowHeight,bottom=100.0/windowHeight)

It works fine when manually resizing the window (we have a padding of 100px on every side).

Unfortunatly, when clicking Maximize, the padding (in 0 to 1) seems to be equal to it's previous value, even if the print returns the correct window size (1920px). A second click to Restore Down will then set the padding to the value we should have when we maximized it.

Explanation with a picture :

I don't really get what's happening here, I must be missing something... Tell me if you need more information such as more code.

Thank you for your kind help :)

1 Answer 1

1

I've had similar issues in the past (the figure not resizing at start up) and moved the recalculation of the figure subplots to the draw method. Something like this (I use tight_layout but you get the idea):

class PlotWidget(FigureCanvas):

    def __init__(self):
        self.__resizing_needed = True

    def draw(self):
        if self.__resizing_needed:
            self.__resizing_needed = False
            try:
                self.figure.tight_layout()
            except ValueError:
                # Set the minimumSize of this widget to prevent this
                print('Plot too small.')

        super(PlotWidget, self).draw()

    def resizeEvent(self, event):
        self.__resizing_needed = True
        super(PlotWidget, self).resizeEvent(event)
Sign up to request clarification or add additional context in comments.

1 Comment

yeah Indeed redrawing is required in this case. But as I has many figures of my plot the draw() was a bit to slow so I made the application redraw on maximize and restore only :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.