Backport PR #21212: Fix set_size_inches on HiDPI and also GTK4 · meeseeksmachine/matplotlib@fbf5477 · GitHub
Skip to content

Commit fbf5477

Browse files
tacaswellmeeseeksmachine
authored andcommitted
Backport PR matplotlib#21212: Fix set_size_inches on HiDPI and also GTK4
1 parent bc1d37c commit fbf5477

5 files changed

Lines changed: 19 additions & 16 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
@@ -413,6 +413,8 @@ def set_window_title(self, title):
413413

414414
def resize(self, width, height):
415415
"""Set the canvas size in pixels."""
416+
width = int(width / self.canvas.device_pixel_ratio)
417+
height = int(height / self.canvas.device_pixel_ratio)
416418
if self.toolbar:
417419
toolbar_size = self.toolbar.size_request()
418420
height += toolbar_size.height

lib/matplotlib/backends/backend_gtk4.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -374,18 +374,13 @@ def set_window_title(self, title):
374374

375375
def resize(self, width, height):
376376
"""Set the canvas size in pixels."""
377+
width = int(width / self.canvas.device_pixel_ratio)
378+
height = int(height / self.canvas.device_pixel_ratio)
377379
if self.toolbar:
378-
toolbar_size = self.toolbar.size_request()
379-
height += toolbar_size.height
380+
min_size, nat_size = self.toolbar.get_preferred_size()
381+
height += nat_size.height
380382
canvas_size = self.canvas.get_allocation()
381-
if canvas_size.width == canvas_size.height == 1:
382-
# A canvas size of (1, 1) cannot exist in most cases, because
383-
# window decorations would prevent such a small window. This call
384-
# must be before the window has been mapped and widgets have been
385-
# sized, so just change the window's starting size.
386-
self.window.set_default_size(width, height)
387-
else:
388-
self.window.resize(width, height)
383+
self.window.set_default_size(width, height)
389384

390385

391386
class NavigationToolbar2GTK4(_NavigationToolbar2GTK, Gtk.Box):

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)