@@ -149,7 +149,11 @@ def _create_qApp():
149149 qApp = app
150150
151151 if is_pyqt5 ():
152- qApp .setAttribute (QtCore .Qt .AA_UseHighDpiPixmaps )
152+ try :
153+ qApp .setAttribute (QtCore .Qt .AA_UseHighDpiPixmaps )
154+ qApp .setAttribute (QtCore .Qt .AA_EnableHighDpiScaling )
155+ except AttributeError :
156+ pass
153157
154158
155159class Show (ShowBase ):
@@ -159,6 +163,7 @@ def mainloop(self):
159163 global qApp
160164 qApp .exec_ ()
161165
166+
162167show = Show ()
163168
164169
@@ -261,17 +266,20 @@ def leaveEvent(self, event):
261266 FigureCanvasBase .leave_notify_event (self , guiEvent = event )
262267
263268 def mouseEventCoords (self , pos ):
264- """
265- Calculate mouse coordinates in logical pixels.
269+ """Calculate mouse coordinates in physical pixels
270+
271+ Qt5 use logical pixels, but the figure is scaled to physical
272+ pixels for rendering. Transform to physical pixels so that
273+ all of the down-stream transforms work as expected.
274+
275+ Also, the origin is different and needs to be corrected.
266276
267- Qt5 and Matplotlib use logical pixels, but the figure is scaled to
268- physical pixels for rendering. Also, the origin is different and needs
269- to be corrected.
270277 """
278+ dpi_ratio = self ._dpi_ratio
271279 x = pos .x ()
272280 # flip y so y=0 is bottom of canvas
273- y = self .figure .bbox .height / self . _dpi_ratio - pos .y ()
274- return x , y
281+ y = self .figure .bbox .height / dpi_ratio - pos .y ()
282+ return x * dpi_ratio , y * dpi_ratio
275283
276284 def mousePressEvent (self , event ):
277285 x , y = self .mouseEventCoords (event .pos ())
@@ -579,7 +587,9 @@ def __init__(self, canvas, parent, coordinates=True):
579587 def _icon (self , name ):
580588 if is_pyqt5 ():
581589 name = name .replace ('.png' , '_large.png' )
582- return QtGui .QIcon (os .path .join (self .basedir , name ))
590+ pm = QtGui .QPixmap (os .path .join (self .basedir , name ))
591+ pm .setDevicePixelRatio (self .canvas ._dpi_ratio )
592+ return QtGui .QIcon (pm )
583593
584594 def _init_toolbar (self ):
585595 self .basedir = os .path .join (matplotlib .rcParams ['datapath' ], 'images' )
@@ -589,7 +599,7 @@ def _init_toolbar(self):
589599 self .addSeparator ()
590600 else :
591601 a = self .addAction (self ._icon (image_file + '.png' ),
592- text , getattr (self , callback ))
602+ text , getattr (self , callback ))
593603 self ._actions [callback ] = a
594604 if callback in ['zoom' , 'pan' ]:
595605 a .setCheckable (True )
@@ -611,7 +621,7 @@ def _init_toolbar(self):
611621 QtCore .Qt .AlignRight | QtCore .Qt .AlignTop )
612622 self .locLabel .setSizePolicy (
613623 QtWidgets .QSizePolicy (QtWidgets .QSizePolicy .Expanding ,
614- QtWidgets .QSizePolicy .Ignored ))
624+ QtWidgets .QSizePolicy .Ignored ))
615625 labelAction = self .addWidget (self .locLabel )
616626 labelAction .setVisible (True )
617627
0 commit comments