You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Figure does not scale to window size #22822
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
importnumpyasnpimportmatplotlib.pyplotasplt# Generate some datat=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 screencur_win=fig.canvas.manager.window# Current windowscreen_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 stringcur_win.geometry(position)
ax.plot(t, x)
plt.show()
Actual outcome
Expected outcome
When the size of the window is then MANUALLY changed, the figure immediately changes to the accurate scale:
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.
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:
importnumpyasnpimportmatplotlib.pyplotasplt# Generate some datat=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 screencur_win=fig.canvas.manager.window# Current windowscreen_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 stringcur_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
(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
importnumpyasnpimportmatplotlib.pyplotasplt# Generate some datat=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 screencur_win=fig.canvas.manager.window# Current windowscreen_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 stringplt.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?
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?
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.
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.
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.
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!
Activity
oscargus commentedon Apr 11, 2022
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.
tacaswell commentedon Apr 11, 2022
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:
I see
and the 1.68 is the value that comes out of my system via
matplotlib/lib/matplotlib/backends/_backend_tk.py
Line 218 in 25c85f0
I think what is happening here is:
If I change the code to
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?
jklymak commentedon Apr 12, 2022
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?
tacaswell commentedon Apr 12, 2022
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.
richardsheridan commentedon Apr 13, 2022
I'm leaning toward this.
FigureCanvasTk.resizedoesn't seem to be the right API though. It's necessary to call eitherresizeor thegeometrymethod after the event loop runs a bit... something between 10 and 100 ms on my machine, kind of awkward. I had some success usingmpl_connect: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.
QuLogic commentedon Apr 13, 2022
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.
github-actions commentedon Dec 3, 2025
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!