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

Skip to content

Commit cfff335

Browse files
committed
FIX: Qt5 account for dpiratio as early as possible
Previously, the canvas size would get set to too small because - in the FigureCanvasQt.__init__ `get_width_height` is used to set the initial size of the canvas - `get_width_height` accounts for the dpiratio, but it has not been multiplied up yet so it reports half the size it should - this get propagated back down into the Figure object in the resize method - the dpi is then scaled up This fix - moves adding the `_original_dpi` attribute to the Figure as soon as possible in the base __init__ - calls `_update_dpi` before asking the figure how big it is Closes #8717
1 parent f8df71b commit cfff335

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,13 @@ class FigureCanvasQT(QtWidgets.QWidget, FigureCanvasBase):
182182
# QtCore.Qt.XButton2: None,
183183
}
184184

185+
def _update_figure_dpi(self):
186+
dpi = self._dpi_ratio * self.figure._original_dpi
187+
self.figure._set_dpi(dpi, forward=False)
188+
185189
def __init__(self, figure):
186190
_create_qApp()
191+
figure._original_dpi = figure.dpi
187192

188193
# NB: Using super for this call to avoid a TypeError:
189194
# __init__() takes exactly 2 arguments (1 given) on QWidget
@@ -192,6 +197,8 @@ def __init__(self, figure):
192197
# http://pyqt.sourceforge.net/Docs/PyQt5/pyqt4_differences.html#cooperative-multi-inheritance
193198
super(FigureCanvasQT, self).__init__(figure=figure)
194199
self.figure = figure
200+
self._update_figure_dpi()
201+
195202
self.setMouseTracking(True)
196203
w, h = self.get_width_height()
197204
self.resize(w, h)

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,6 @@ class FigureCanvasQTAgg(FigureCanvasQTAggBase, FigureCanvasQT):
190190
191191
"""
192192

193-
def __init__(self, figure):
194-
super(FigureCanvasQTAgg, self).__init__(figure=figure)
195-
# We don't want to scale up the figure DPI more than once.
196-
# Note, we don't handle a signal for changing DPI yet.
197-
self.figure._original_dpi = self.figure.dpi
198-
self._update_figure_dpi()
199-
200-
def _update_figure_dpi(self):
201-
dpi = self._dpi_ratio * self.figure._original_dpi
202-
self.figure._set_dpi(dpi, forward=False)
203-
204193

205194
@_BackendQT5.export
206195
class _BackendQT5Agg(_BackendQT5):

0 commit comments

Comments
 (0)