Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38691d7

Browse files
committed
Fix set_size_inches on HiDPI screens
This passes physical pixels to the backend, as it will be more accurate due to the int-cast. Fixes #21090
1 parent ef74de4 commit 38691d7

File tree

5 files changed

+12
-7
lines changed

5 files changed

+12
-7
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1760,7 +1760,7 @@ def blit(self, bbox=None):
17601760
"""Blit the canvas in bbox (default entire canvas)."""
17611761

17621762
def resize(self, w, h):
1763-
"""Set the canvas size in pixels."""
1763+
"""Set the canvas size in (physical) pixels."""
17641764

17651765
def draw_event(self, renderer):
17661766
"""Pass a `DrawEvent` to all functions connected to ``draw_event``."""

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ def set_window_title(self, title):
421421

422422
def resize(self, width, height):
423423
"""Set the canvas size in pixels."""
424+
width = int(width / self.canvas.device_pixel_ratio)
425+
height = int(height / self.canvas.device_pixel_ratio)
424426
if self.toolbar:
425427
toolbar_size = self.toolbar.size_request()
426428
height += toolbar_size.height

lib/matplotlib/backends/backend_gtk4.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ def set_window_title(self, title):
372372

373373
def resize(self, width, height):
374374
"""Set the canvas size in pixels."""
375+
width = int(width / self.canvas.device_pixel_ratio)
376+
height = int(height / self.canvas.device_pixel_ratio)
375377
if self.toolbar:
376378
min_size, nat_size = self.toolbar.get_preferred_size()
377379
height += nat_size.height

lib/matplotlib/backends/backend_qt.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,12 +587,14 @@ def _get_toolbar(self, canvas, parent):
587587
return toolbar
588588

589589
def resize(self, width, height):
590-
# these are Qt methods so they return sizes in 'virtual' pixels
591-
# so we do not need to worry about dpi scaling here.
590+
# The Qt methods return sizes in 'virtual' pixels so we do need to
591+
# rescale from physical to logical pixels.
592+
width = int(width / self.canvas.device_pixel_ratio)
593+
height = int(height / self.canvas.device_pixel_ratio)
592594
extra_width = self.window.width() - self.canvas.width()
593595
extra_height = self.window.height() - self.canvas.height()
594-
self.canvas.resize(width, height)
595-
self.window.resize(width + extra_width, height + extra_height)
596+
self.canvas.resize(round(width), round(height))
597+
self.window.resize(round(width + extra_width), round(height + extra_height))
596598

597599
def show(self):
598600
self.window.show()

lib/matplotlib/figure.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,10 +2657,9 @@ def set_size_inches(self, w, h=None, forward=True):
26572657
if forward:
26582658
canvas = getattr(self, 'canvas')
26592659
if canvas is not None:
2660-
dpi_ratio = getattr(canvas, '_dpi_ratio', 1)
26612660
manager = getattr(canvas, 'manager', None)
26622661
if manager is not None:
2663-
manager.resize(*(size * self.dpi / dpi_ratio).astype(int))
2662+
manager.resize(*(size * self.dpi).astype(int))
26642663
self.stale = True
26652664

26662665
def get_size_inches(self):

0 commit comments

Comments
 (0)