Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Resize canvas when changing figure size #15045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 26, 2019

Conversation

dstansby
Copy link
Member

When the figure size is resized, as well as the window being manually resized the canvas needs to be resized too. Usually the canvas is resized automatically to the window at draw time, but this handles the case where the figure size is changed more than once before drawing (ie. #15040).

This might not be the right thing to do though, maybe others know better?

Fixes #15040

@dstansby dstansby added this to the v3.2.0 milestone Aug 12, 2019
@tacaswell
Copy link
Member

What is the mechanism by which this breaks?

It is also possible that this "just works" on master, @anntzer recently put in some PRs to tweak how we handle the window sizing.

@dstansby
Copy link
Member Author

See the example in #15040, which shows how it breaks, even on master.

@tacaswell
Copy link
Member

I mean, why does setting the size twice confuse it?

@dstansby
Copy link
Member Author

Currently the resize code in Qt5 is:

    def resize(self, width, height):
        # these are Qt methods so they return sizes in 'virtual' pixels
        # so we do not need to worry about dpi scaling here.
        extra_width = self.window.width() - self.canvas.width()
        extra_height = self.window.height() - self.canvas.height()
        self.window.resize(width + extra_width, height + extra_height)

After one resize the window gets resized, but the canvas doesn't. This means when it comes to the second resize, self.canvas.width()/height() don't reflect the previous resize, so extra_width/height are not computed correctly.

My guess as to why it works fine in the usual case with one resize and a draw is because at draw time the canvas is automatically resized to fit the window.

@dstansby
Copy link
Member Author

I've just pushed a test that fails without the change in this PR.

@dstansby dstansby changed the title Resize canvas when changing figures size in Qt5Agg Resize canvas when changing figure size in Qt5Agg Aug 12, 2019
@anntzer
Copy link
Contributor

anntzer commented Aug 19, 2019

I'm not totally convinced this is the correct fix? For example, perhaps set_size_inches should be the one doing the canvas resize, e.g. with

        if forward:
            canvas = getattr(self, 'canvas')
            if canvas is not None:
                dpi_ratio = getattr(canvas, '_dpi_ratio', 1)

                canvas.resize(...)  # <--- HERE

                manager = getattr(canvas, 'manager', None)
                if manager is not None:
                    manager.resize(*(size * self.dpi / dpi_ratio).astype(int))

otherwise, I would guess the other backends also need updating:

    # GTK3
    def resize(self, width, height):
        'set the canvas size in pixels'
        #_, _, cw, ch = self.canvas.allocation
        #_, _, ww, wh = self.window.allocation
        #self.window.resize (width-cw+ww, height-ch+wh)
        self.window.resize(width, height)

    # Tk
    def resize(self, width, height):
        self.canvas._tkcanvas.master.geometry("%dx%d" % (width, height))

        if self.toolbar is not None:
            self.toolbar.configure(width=width)

    # wx
    def resize(self, width, height):
        'Set the canvas size in pixels'
        self.canvas.SetInitialSize(wx.Size(width, height))
        self.window.GetSizer().Fit(self.window)

which appear to all suffer from the same issue? (FigureManagerMac doesn't even seem to define resize()?)

@dstansby
Copy link
Member Author

Yes, I think you're right that set_size_inches should handle it, and what you proposed is correct, ie.

  1. Change the figure (really canvas) size (currently missing)
  2. If there's a window manager, change the window size to match (currently present)

@dstansby dstansby changed the title Resize canvas when changing figure size in Qt5Agg Resize canvas when changing figure size Aug 21, 2019
@dstansby
Copy link
Member Author

Is there a way to parameterize the test with different backends?

@timhoffm
Copy link
Member

Is there a way to parameterize the test with different backends?

Not sure. You can always generate the functions dynamically in a for loop. I think something like this should work:

for backend in ['Qt5Agg', 'TkAgg']:
    @pytest.mark.backend(backend)
    def _test_x():
        ...
    
    globals()[f'test_x_{backend}'] = _test_x

@anntzer
Copy link
Contributor

anntzer commented Aug 22, 2019

They basically need to run in different subprocesses (because a given process can only ever use a single interactive backend) -- see tests_backends_interactive.py. (Probably stashing all the tests in a single process invocation, as is done right now, is better for performance.)

@tacaswell
Copy link
Member

With #14703 merged do we still need this?

@dstansby
Copy link
Member Author

Can take a look soon-ish, but I think it's worth putting the test in even if it does pass on current master.

@dstansby
Copy link
Member Author

Yep, this fix is still needed even with #14703

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, this code breaks the resizing.

window = fig.canvas.manager.window

fig.set_size_inches(2, 2)
old_width = window.width()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my test of this code, the example window is always 640x480 no matter what I pass to set_size_inches. Maybe explicitly test for 200 here (or rcParams['figure.dpi'] * 2 if you want to be very explicit).

@dstansby
Copy link
Member Author

I've changed my mind and think that it's reasonable for the backend resize methods to also be responsible for resizing the canvas. I'm going to leave this as is as a Qt5 fix, and open new bugs for each other backend that has set_size_inches() issues.

I've also added an explict check for figure width as @timhoffm suggests.

@timhoffm timhoffm merged commit 8df7fe1 into matplotlib:master Aug 26, 2019
@dstansby dstansby deleted the qt5-fig-size branch August 27, 2019 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong figure window size after calling fig.set_size_inches() repeatedly
4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.