Skip to content

Issue with DPI corrections with Qt5 backend #8052

@astrofrog

Description

@astrofrog

I am running into issues with Matplotlib 2.0 with the Qt5 backend. The following script:

import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])

print(ax.transData.transform((0.0, 0.0)))
print(ax.transData.transform((0.5, 0.5)))
print(ax.transData.transform((1.0, 1.0)))
print(fig.canvas.width(), fig.canvas.height())

returns

[ 0.  0.]
[ 640.  480.]
[ 1280.   960.]
320 240

with Qt5, and:

[ 0. 0.]
[ 320. 240.]
[ 640. 480.]
(640, 480)

with Qt4. In Qt5 there is a factor of 4x difference between the display coordinates and the size of the widget, where I think there should only be a factor of 2x. A more visual way to see this issue is to run:

from PyQt5 import QtGui, QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas


class MplCanvas(FigureCanvas):

    def __init__(self, fig):
        FigureCanvas.__init__(self, fig)
        self.renderer = None
        self.x = self.y = 0

    def paintEvent(self, event):

        if self.renderer is None:
            self.renderer = self.get_renderer()

        super(MplCanvas, self).paintEvent(event)

        p = QtGui.QPainter(self)
        pen = QtGui.QPen(QtGui.QColor('black'))
        pen.setWidth(20)
        p.setPen(pen)
        p.drawPoint(self.x, self.y)

    def mouse_press(self, event):
        x, y = ax.transData.transform((event.xdata, event.ydata))
        self.x, self.y = x, self.height() - y
        self.update()


app = QtWidgets.QApplication([''])

fig = Figure()
canvas = MplCanvas(fig)
ax = fig.add_axes([0, 0, 1, 1])

print(ax.transData.transform((0.0, 0.0)))
print(ax.transData.transform((0.5, 0.5)))
print(ax.transData.transform((1.0, 1.0)))
print(canvas.width(), canvas.height())

canvas.mpl_connect('button_press_event', canvas.mouse_press)
canvas.mpl_connect('motion_notify_event', canvas.mouse_press)

canvas.show()
app.exec_()

This shows a window where if you move around the cursor, a black square should follow it. With Qt5, the square is offset by a factor of 2x:

untitled

cc @tacaswell

Metadata

Metadata

Assignees

No one assigned

    Labels

    GUI: QtRelease criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions