diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index a8c81b05df73..bc5257bab60e 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2157,7 +2157,7 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None, dpi = rcParams['savefig.dpi'] if dpi == 'figure': - dpi = self.figure.dpi + dpi = getattr(self.figure, '_original_dpi', self.figure.dpi) if facecolor is None: facecolor = rcParams['savefig.facecolor'] diff --git a/lib/matplotlib/backends/backend_qt5.py b/lib/matplotlib/backends/backend_qt5.py index 0c71385eb338..4041991bd757 100644 --- a/lib/matplotlib/backends/backend_qt5.py +++ b/lib/matplotlib/backends/backend_qt5.py @@ -149,7 +149,11 @@ def _create_qApp(): qApp = app if is_pyqt5(): - qApp.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps) + try: + qApp.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps) + qApp.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) + except AttributeError: + pass class Show(ShowBase): @@ -159,6 +163,7 @@ def mainloop(self): global qApp qApp.exec_() + show = Show() @@ -261,17 +266,20 @@ def leaveEvent(self, event): FigureCanvasBase.leave_notify_event(self, guiEvent=event) def mouseEventCoords(self, pos): - """ - Calculate mouse coordinates in logical pixels. + """Calculate mouse coordinates in physical pixels + + Qt5 use logical pixels, but the figure is scaled to physical + pixels for rendering. Transform to physical pixels so that + all of the down-stream transforms work as expected. + + Also, the origin is different and needs to be corrected. - Qt5 and Matplotlib use logical pixels, but the figure is scaled to - physical pixels for rendering. Also, the origin is different and needs - to be corrected. """ + dpi_ratio = self._dpi_ratio x = pos.x() # flip y so y=0 is bottom of canvas - y = self.figure.bbox.height / self._dpi_ratio - pos.y() - return x, y + y = self.figure.bbox.height / dpi_ratio - pos.y() + return x * dpi_ratio, y * dpi_ratio def mousePressEvent(self, event): x, y = self.mouseEventCoords(event.pos()) @@ -579,7 +587,9 @@ def __init__(self, canvas, parent, coordinates=True): def _icon(self, name): if is_pyqt5(): name = name.replace('.png', '_large.png') - return QtGui.QIcon(os.path.join(self.basedir, name)) + pm = QtGui.QPixmap(os.path.join(self.basedir, name)) + pm.setDevicePixelRatio(self.canvas._dpi_ratio) + return QtGui.QIcon(pm) def _init_toolbar(self): self.basedir = os.path.join(matplotlib.rcParams['datapath'], 'images') @@ -589,7 +599,7 @@ def _init_toolbar(self): self.addSeparator() else: a = self.addAction(self._icon(image_file + '.png'), - text, getattr(self, callback)) + text, getattr(self, callback)) self._actions[callback] = a if callback in ['zoom', 'pan']: a.setCheckable(True) @@ -611,7 +621,7 @@ def _init_toolbar(self): QtCore.Qt.AlignRight | QtCore.Qt.AlignTop) self.locLabel.setSizePolicy( QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, - QtWidgets.QSizePolicy.Ignored)) + QtWidgets.QSizePolicy.Ignored)) labelAction = self.addWidget(self.locLabel) labelAction.setVisible(True) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 8f3b3afe7c03..288ff450a25e 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -323,9 +323,10 @@ def __init__(self, if frameon is None: frameon = rcParams['figure.frameon'] - self.dpi_scale_trans = Affine2D() - self.dpi = dpi self.bbox_inches = Bbox.from_bounds(0, 0, *figsize) + self.dpi_scale_trans = Affine2D().scale(dpi, dpi) + # do not use property as it will trigger + self._dpi = dpi self.bbox = TransformedBbox(self.bbox_inches, self.dpi_scale_trans) self.frameon = frameon @@ -413,6 +414,7 @@ def _get_dpi(self): def _set_dpi(self, dpi): self._dpi = dpi self.dpi_scale_trans.clear().scale(dpi, dpi) + self.set_size_inches(*self.get_size_inches()) self.callbacks.process('dpi_changed', self) dpi = property(_get_dpi, _set_dpi) @@ -710,13 +712,15 @@ def set_size_inches(self, w, h=None, forward=True): self.bbox_inches.p1 = w, h if forward: - ratio = getattr(self.canvas, '_dpi_ratio', 1) - dpival = self.dpi / ratio - canvasw = w * dpival - canvash = h * dpival - manager = getattr(self.canvas, 'manager', None) - if manager is not None: - manager.resize(int(canvasw), int(canvash)) + canvas = getattr(self, 'canvas') + if canvas is not None: + ratio = getattr(self.canvas, '_dpi_ratio', 1) + dpival = self.dpi / ratio + canvasw = w * dpival + canvash = h * dpival + manager = getattr(self.canvas, 'manager', None) + if manager is not None: + manager.resize(int(canvasw), int(canvash)) self.stale = True def get_size_inches(self): diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 677bf50da23e..f5ce06b0d942 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -214,6 +214,18 @@ def test_figaspect(): assert h / w == 1 +@cleanup(style='default') +def test_change_dpi(): + fig = plt.figure(figsize=(4, 4)) + fig.canvas.draw() + assert fig.canvas.renderer.height == 400 + assert fig.canvas.renderer.width == 400 + fig.dpi = 50 + fig.canvas.draw() + assert fig.canvas.renderer.height == 200 + assert fig.canvas.renderer.width == 200 + + if __name__ == "__main__": import nose nose.runmodule(argv=['-s', '--with-doctest'], exit=False)