Skip to content

[Bug]: Figure does not scale to window size #22822

Closed as not planned
Closed as not planned

Description

@thomas-haslwanter

Bug summary

When changing the window size of a figure, the figure does not scale immediately, so part of it is obscured. Only after a manual re-scaling (even if minute) the figure is displayed correctly.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

# Generate some data
t = np.arange(0, 10, 0.1)
x = np.cos(t)

fig, ax = plt.subplots(1,1)

# Put the figure in the top-left half of the screen
cur_win = fig.canvas.manager.window     # Current window
screen_size = cur_win.wm_maxsize()      # Get the screen size
# top-left half of the window:
(pos_x, pos_y, width, height) = (0, 0, screen_size[0]//2, screen_size[1]//2)
position = f'{width}x{height}+{pos_x}+{pos_y}'  # Window geometry string
cur_win.geometry(position)

ax.plot(t, x)
plt.show()

Actual outcome

scaling_problem_01

Expected outcome

When the size of the window is then MANUALLY changed, the figure immediately changes to the accurate scale:
scaling_problem_02

Additional information

The bug appears from Matplotlib 3.5.1 onward

Operating system

Windows 11

Matplotlib Version

3.5.1

Matplotlib Backend

TkAgg

Python version

3.9.10

Jupyter version

No response

Installation

No response

Activity

  1. oscargus commented on Apr 11, 2022

    @oscargus
    Member

    For what it is worth: this does not seem to be an issue on main using Linux (CentOS 7). So either it has been fixed in main or it is a platform dependent issue.

  2. tacaswell commented on Apr 11, 2022

    @tacaswell
    Member

    I can reproduce this (with the additional issue that it bigger than half of my screen but it may be half of the size of the union of my two screens?). I think this is related to hi-dpi screens.

    If I do:

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Generate some data
    t = np.arange(0, 10, 0.1)
    x = np.cos(t)
    
    fig, ax = plt.subplots(1,1)
    print(fig.canvas.device_pixel_ratio)
    fig.canvas.mpl_connect('resize_event', lambda *args, **kwargs: print(args, kwargs))
    # Put the figure in the top-left half of the screen
    cur_win = fig.canvas.manager.window     # Current window
    screen_size = cur_win.wm_maxsize()      # Get the screen size
    # top-left half of the window:
    (pos_x, pos_y, width, height) = (0, 0, screen_size[0]//2, screen_size[1]//2)
    position = f'{width}x{height}+{pos_x}+{pos_y}'  # Window geometry string
    cur_win.geometry(position)
    
    ax.plot(t, x)
    plt.show()
    print(fig.canvas.device_pixel_ratio)

    I see

    1
    (<matplotlib.backend_bases.ResizeEvent object at 0x7f227e56b590>,) {}
    1.68
    

    and the 1.68 is the value that comes out of my system via

    ratio = round(self._tkcanvas.tk.call('tk', 'scaling') / (96 / 72), 2)
    (which is seems to be computing by looking and xrandr computing from the reported physical sizes (in mm and pixels).

    I think what is happening here is:

    • you create (but do not show) the tk window, the device_pixel_ratio is 1
    • you set the window size in pixels
    • Matplotlib sorts out what size that is in inches (because figures are ground-truth sized in inches)
    • when the window is shown by tk
    • on activate we update the device pixel ratio (which is now 1.68 on my machine)
    • this does not trigger a resize of the window (in pixels) or the figure (in inches)
    • when we render the figure we use an effectively higher dpi -> too many pixels -> only top left is shown
    • when you tweak the window size we go through the dance again, but this time using the right device pixel ratio which makes the figure smaller in inches, but we then render the right number of pixels

    If I change the code to

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Generate some data
    t = np.arange(0, 10, 0.1)
    x = np.cos(t)
    
    fig, ax = plt.subplots(1,1)
    print(fig.canvas.device_pixel_ratio)
    fig.canvas.mpl_connect('resize_event', lambda *args, **kwargs: print(args, kwargs))
    # Put the figure in the top-left half of the screen
    cur_win = fig.canvas.manager.window     # Current window
    screen_size = cur_win.wm_maxsize()      # Get the screen size
    # top-left half of the window:
    (pos_x, pos_y, width, height) = (0, 0, screen_size[0]//2, screen_size[1]//2)
    position = f'{width}x{height}+{pos_x}+{pos_y}'  # Window geometry string
    plt.pause(.1)  # <- NOTE THIS LINE it forces the window to display before we resize it!
    cur_win.geometry(position)
    
    ax.plot(t, x)
    plt.show(); print(fig.canvas.device_pixel_ratio)

    I get a figure that shows the whole figure and is placed at the top-left of the theoretical union of my two screens (which are set up like |- ).

    attn @QuLogic This is related to the issues we were chasing down last week. attn @richardsheridan Should we be ignoring this scaling on *nix?

  3. jklymak commented on Apr 12, 2022

    @jklymak
    Member

    That all looks right, but I'm not sure we need to support changing the window size without changing the figure size. In this case doesn't the user just need to fire the window resize callback manually since they are changing the window size manually?

  4. tacaswell commented on Apr 12, 2022

    @tacaswell
    Member

    The issue is that when the window size is initially changed we have bad information about what the effective dpi should be so compute the wrong figure size to forward on.

    I think the core of the issue is that Tk handles the scaling / hi-dpi differently than the other backends.

  5. richardsheridan commented on Apr 13, 2022

    @richardsheridan
    Contributor

    That all looks right, but I'm not sure we need to support changing the window size without changing the figure size. In this case doesn't the user just need to fire the window resize callback manually since they are changing the window size manually?

    I'm leaning toward this. FigureCanvasTk.resize doesn't seem to be the right API though. It's necessary to call either resize or the geometry method after the event loop runs a bit... something between 10 and 100 ms on my machine, kind of awkward. I had some success using mpl_connect:

    fig.canvas.mpl_connect('resize_event', lambda *args, **kwargs: print(args, kwargs) or cur_win.geometry(position))
    

    which has the amusing side-effect of immediately resetting the window geometry if you try to resize it with the mouse. A real workaround would need to immediately un-register the callback.

  6. QuLogic commented on Apr 13, 2022

    @QuLogic
    Member

    I think the core of the issue is that Tk handles the scaling / hi-dpi differently than the other backends.

    Yes, on Windows, the 'standard' monitor is 96 DPI, and for monitors with HiDPI scaling, we then increase that value by whatever that scale is. The 'scaling' value we use within Tk is the DPI over a 'standard' 72 DPI.

    This does not appear to be the case on Linux, and possibly is not true on macOS either. The Tk 'scaling' value is the true DPI (or at least whatever X wants to tell it it is) over 72 DPI and so we end up setting a fake HiDPI mode, even if that's not enabled on the system. The initial mixed DPI support was only intended for Windows, and so Linux/macOS don't even rescale properly when moving across monitors. I still haven't figured out how to do the rescaling on those systems (and I don't think it's really possible on X.)

    All that means, I think we need to ignore 'scaling' on non-Windows systems.

  7. github-actions commented on Dec 3, 2025

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  8. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Jan 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    GUI: tkstatus: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.status: inactiveMarked by the “Stale” Github Action

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions