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

Skip to content

Commit 419da79

Browse files
committed
Qt5: Fix event positions on HiDPI screens.
The event location is in logical pixels, which is the way it should stay for the rest of the stack.
1 parent 067f9b6 commit 419da79

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,16 @@ def leaveEvent(self, event):
263263
FigureCanvasBase.leave_notify_event(self, guiEvent=event)
264264

265265
def mouseEventCoords(self, pos):
266-
x = pos.x() * self._dpi_ratio
266+
"""
267+
Calculate mouse coordinates in logical pixels.
268+
269+
Qt5 and Matplotlib use logical pixels, but the figure is scaled to
270+
physical pixels for rendering. Also, the origin is different and needs
271+
to be corrected.
272+
"""
273+
x = pos.x()
267274
# flip y so y=0 is bottom of canvas
268-
y = self.figure.bbox.height - pos.y() * self._dpi_ratio
275+
y = self.figure.bbox.height / self._dpi_ratio - pos.y()
269276
return x, y
270277

271278
def mousePressEvent(self, event):

0 commit comments

Comments
 (0)