🌐 AI搜索 & 代理 主页
Skip to content

[Bug]: DPI keeps doubling when creating a new MatPlotLib QtWidget in qt6 #26380

@hydroEng

Description

@hydroEng

Bug summary

I ran into this bizarre bug, I am not sure if it's a skill issue on my end, but I am trying to embed a plot in my qt6 application. Allowing the user to plot and replot for some reason keeps increasing the sizes of my chart elements, but only when I have display scaling on (e.g. 125%, 150% etc). To illustrate:

wat

See below for the code, basically I am creating a new plot every time there is a mouse click on that row. Embedding print(self.chart.figure.dpi) shows that my plot's dpi keeps doubling every other time I trigger a click. Console output:

100.0
100.0
200.0
200.0
400.0
400.0

I can workaround this by explicitly setting the figure DPI, e.g. figure.dpi = 100 when creating it. But I assume that means I'm giving up display scaling

Code for reproduction

import matplotlib

matplotlib.use("Qt5Agg")
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
from PyQt6.QtWidgets import (
    QWidget,
    QVBoxLayout, QFrame
)

class GraphView(QWidget):
    def __init__(self):
        super().__init__()
        self.layout = QVBoxLayout()
        self.separator = None
        self.chart = MplCanvas()

        self.figures = None

        self.init_widget()

    def init_widget(self):
        self.init_separator()
        self.layout.addWidget(self.separator)
        self.layout.addWidget(self.chart)
        self.setLayout(self.layout)
    ...
    # Note this is the code that triggers when I click a row.
    def update_graph(self, figure):

        self.chart.deleteLater()
        self.chart = MplCanvas(fig=figure)
    
        self.layout.addWidget(self.chart)
        self.layout.update()

class MplCanvas(FigureCanvasQTAgg):
    def __init__(self, fig=Figure()):
        super(MplCanvas, self).__init__(fig)

Actual outcome

Figure DPI keeps dynamically doubling

Expected outcome

DPI shouldn't double like that, it's madness ☹️

Additional information

No response

Operating system

Windows, Linux

Matplotlib Version

3.6.3

Matplotlib Backend

QtAgg

Python version

3.11.4

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions