@@ -148,7 +148,11 @@ def _create_qApp():
148148 qApp = app
149149
150150 if is_pyqt5 ():
151- qApp .setAttribute (QtCore .Qt .AA_UseHighDpiPixmaps )
151+ try :
152+ qApp .setAttribute (QtCore .Qt .AA_UseHighDpiPixmaps )
153+ qApp .setAttribute (QtCore .Qt .AA_EnableHighDpiScaling )
154+ except AttributeError :
155+ pass
152156
153157
154158class Show (ShowBase ):
@@ -158,6 +162,7 @@ def mainloop(self):
158162 global qApp
159163 qApp .exec_ ()
160164
165+
161166show = Show ()
162167
163168
@@ -267,17 +272,20 @@ def leaveEvent(self, event):
267272 FigureCanvasBase .leave_notify_event (self , guiEvent = event )
268273
269274 def mouseEventCoords (self , pos ):
270- """
271- Calculate mouse coordinates in logical pixels.
275+ """Calculate mouse coordinates in physical pixels
276+
277+ Qt5 use logical pixels, but the figure is scaled to physical
278+ pixels for rendering. Transform to physical pixels so that
279+ all of the down-stream transforms work as expected.
280+
281+ Also, the origin is different and needs to be corrected.
272282
273- Qt5 and Matplotlib use logical pixels, but the figure is scaled to
274- physical pixels for rendering. Also, the origin is different and needs
275- to be corrected.
276283 """
284+ dpi_ratio = self ._dpi_ratio
277285 x = pos .x ()
278286 # flip y so y=0 is bottom of canvas
279- y = self .figure .bbox .height / self . _dpi_ratio - pos .y ()
280- return x , y
287+ y = self .figure .bbox .height / dpi_ratio - pos .y ()
288+ return x * dpi_ratio , y * dpi_ratio
281289
282290 def mousePressEvent (self , event ):
283291 x , y = self .mouseEventCoords (event .pos ())
@@ -602,7 +610,10 @@ def __init__(self, canvas, parent, coordinates=True):
602610 def _icon (self , name ):
603611 if is_pyqt5 ():
604612 name = name .replace ('.png' , '_large.png' )
605- return QtGui .QIcon (os .path .join (self .basedir , name ))
613+ pm = QtGui .QPixmap (os .path .join (self .basedir , name ))
614+ if hasattr (pm , 'setDevicePixelRatio' ):
615+ pm .setDevicePixelRatio (self .canvas ._dpi_ratio )
616+ return QtGui .QIcon (pm )
606617
607618 def _init_toolbar (self ):
608619 self .basedir = os .path .join (matplotlib .rcParams ['datapath' ], 'images' )
@@ -612,7 +623,7 @@ def _init_toolbar(self):
612623 self .addSeparator ()
613624 else :
614625 a = self .addAction (self ._icon (image_file + '.png' ),
615- text , getattr (self , callback ))
626+ text , getattr (self , callback ))
616627 self ._actions [callback ] = a
617628 if callback in ['zoom' , 'pan' ]:
618629 a .setCheckable (True )
@@ -634,7 +645,7 @@ def _init_toolbar(self):
634645 QtCore .Qt .AlignRight | QtCore .Qt .AlignTop )
635646 self .locLabel .setSizePolicy (
636647 QtWidgets .QSizePolicy (QtWidgets .QSizePolicy .Expanding ,
637- QtWidgets .QSizePolicy .Ignored ))
648+ QtWidgets .QSizePolicy .Ignored ))
638649 labelAction = self .addWidget (self .locLabel )
639650 labelAction .setVisible (True )
640651
0 commit comments