Fix set_size_inches on HiDPI screens · matplotlib/matplotlib@4635702 · GitHub
Skip to content

Commit 4635702

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 19ad5a4 commit 4635702

5 files changed

Lines changed: 16 additions & 6 deletions

File tree

lib/matplotlib/backend_bases.py

Lines changed: 7 additions & 2 deletions

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
@@ -373,6 +373,8 @@ def set_window_title(self, title):
373373

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

lib/matplotlib/backends/backend_qt.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,10 @@ def _get_toolbar(self, canvas, parent):
589589
return toolbar
590590

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

lib/matplotlib/figure.py

Lines changed: 1 addition & 2 deletions

0 commit comments

Comments
 (0)