Skip to content

MNT: Debug fig_size change after window close #29129

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

timhoffm
Copy link
Member

@timhoffm timhoffm commented Nov 12, 2024

This is a minimal reproducer for a failure in the pipeline #29125 (comment).

The test_interactive_backend[toolmanager-MPLBACKEND=wxagg-BACKEND_DEPS=wx] fails in the pipeline at the subsequent assert result.getvalue() == result_after.getvalue() because the PNG data in result and result_after have different sizes (640x480) vs. (640x478); i.e. result_after is two pixels smaller. This stems from a changed fig size, as made explicit by the assert statement added here (precisely, fig.get_size_inches() changes from np.array([6.4, 4.8]) to np.array([6.4, 4.78])).

I have not yet a clue why closing the figure window could change the figure size. Actually, showing the window changes the size, see below.


Note: reading the exception is a bit complicated because it happens in a called subprocess, but you'll find this somewhere in the middle of the traceback:

[...] assert tuple(fig.get_size_inches()) == tuple(size_inches), \\\nAssertionError: [6.4  4.78], [6.4 4.8]\n'

Update: This is the call stack that leads to the changed figure size:

  File "/home/tim/git/matplotlib/lib/matplotlib/tests/test_backends_interactive.py", line 223, in _test_interactive_impl
    plt.show()
  File "/home/tim/git/matplotlib/lib/matplotlib/pyplot.py", line 612, in show
    return _get_backend_mod().show(*args, **kwargs)
  File "/home/tim/git/matplotlib/lib/matplotlib/backend_bases.py", line 3534, in show
    manager.show()  # Emits a warning for non-interactive backend.
  File "/home/tim/git/matplotlib/lib/matplotlib/backends/backend_wx.py", line 1006, in show
    self.frame.Show()
  File "/home/tim/git/matplotlib/lib/matplotlib/backends/backend_wx.py", line 683, in _on_size
    self.figure.set_size_inches(winch, hinch, forward=False)

Apparently show() creates a wx.EVT_SIZE, which is bound to _FigureCanvasWxBase._on_size(), and in there

size = self.GetClientSize()

returns the reduced height of 478.

@QuLogic
Copy link
Member

QuLogic commented Nov 13, 2024

This is a result of #28838, which enabled the test everywhere, but at the time did not seem to be causing trouble.

However, I am seeing the problem with wx and Qt when building 3.10.0rc1 for Fedora. I though it was because I was running Xvfb at 680x480 and the figure didn't fit, but even at 800x600 it failed.

@timhoffm
Copy link
Member Author

timhoffm commented Nov 13, 2024

I've tried to move the show() before the savefig() to workaround show() altering figure size. While this fixes the previously failing tests, it seems to introduce issues in other tests. 😢

Anyway, the proper fix is to understand why show() changes the figure size - it should not do that.


I've also tried to produce a minimal example:

import matplotlib
import matplotlib.pyplot as plt

matplotlib.use('wxagg')
plt.rcParams['toolbar'] == 'toolmanager'

fig = plt.figure()
print(fig.get_size_inches())
plt.show()
# close manually
print(fig.get_size_inches())

But (in the same environment as for the tests) this does not change figsize, so the effect seems somehow related to the addmittedly complex test setup.

timhoffm added a commit to timhoffm/matplotlib that referenced this pull request Nov 13, 2024
Works around the failure matplotlib#29129. A proper analysis
and resolution is still needed.
@timhoffm timhoffm marked this pull request as draft December 18, 2024 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants