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

Skip to content

Commit 8a8dd90

Browse files
authored
Merge pull request #21212 from QuLogic/fix-set_size_inches
FIX: set_size_inches on HiDPI and also GTK4
2 parents da5249a + 9869826 commit 8a8dd90

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,7 +1762,12 @@ def blit(self, bbox=None):
17621762
"""Blit the canvas in bbox (default entire canvas)."""
17631763

17641764
def resize(self, w, h):
1765-
"""Set the canvas size in pixels."""
1765+
"""
1766+
UNUSED: Set the canvas size in pixels.
1767+
1768+
Certain backends may implement a similar method internally, but this is
1769+
not a requirement of, nor is it used by, Matplotlib itself.
1770+
"""
17661771

17671772
def draw_event(self, renderer):
17681773
"""Pass a `DrawEvent` to all functions connected to ``draw_event``."""
@@ -2815,7 +2820,7 @@ def full_screen_toggle(self):
28152820
pass
28162821

28172822
def resize(self, w, h):
2818-
"""For GUI backends, resize the window (in pixels)."""
2823+
"""For GUI backends, resize the window (in physical pixels)."""
28192824

28202825
@_api.deprecated(
28212826
"3.4", alternative="self.canvas.callbacks.process(event.name, 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: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -373,18 +373,13 @@ 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:
377-
toolbar_size = self.toolbar.size_request()
378-
height += toolbar_size.height
379+
min_size, nat_size = self.toolbar.get_preferred_size()
380+
height += nat_size.height
379381
canvas_size = self.canvas.get_allocation()
380-
if canvas_size.width == canvas_size.height == 1:
381-
# A canvas size of (1, 1) cannot exist in most cases, because
382-
# window decorations would prevent such a small window. This call
383-
# must be before the window has been mapped and widgets have been
384-
# sized, so just change the window's starting size.
385-
self.window.set_default_size(width, height)
386-
else:
387-
self.window.resize(width, height)
382+
self.window.set_default_size(width, height)
388383

389384

390385
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
Original file line numberDiff line numberDiff line change
@@ -2658,10 +2658,9 @@ def set_size_inches(self, w, h=None, forward=True):
26582658
if forward:
26592659
canvas = getattr(self, 'canvas')
26602660
if canvas is not None:
2661-
dpi_ratio = getattr(canvas, '_dpi_ratio', 1)
26622661
manager = getattr(canvas, 'manager', None)
26632662
if manager is not None:
2664-
manager.resize(*(size * self.dpi / dpi_ratio).astype(int))
2663+
manager.resize(*(size * self.dpi).astype(int))
26652664
self.stale = True
26662665

26672666
def get_size_inches(self):

0 commit comments

Comments
 (0)