diff --git a/lib/matplotlib/backends/backend_qt5.py b/lib/matplotlib/backends/backend_qt5.py index fff76369ef84..fa2b06bc0ec1 100644 --- a/lib/matplotlib/backends/backend_qt5.py +++ b/lib/matplotlib/backends/backend_qt5.py @@ -263,9 +263,16 @@ def leaveEvent(self, event): FigureCanvasBase.leave_notify_event(self, guiEvent=event) def mouseEventCoords(self, pos): - x = pos.x() * self._dpi_ratio + """ + Calculate mouse coordinates in logical pixels. + + 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. + """ + x = pos.x() # flip y so y=0 is bottom of canvas - y = self.figure.bbox.height - pos.y() * self._dpi_ratio + y = self.figure.bbox.height / self._dpi_ratio - pos.y() return x, y def mousePressEvent(self, event):