You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: DPI keeps doubling when creating a new MatPlotLib QtWidget in qt6 #26380
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:
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
importmatplotlibmatplotlib.use("Qt5Agg")
frommatplotlib.backends.backend_qt5aggimportFigureCanvasQTAggfrommatplotlib.figureimportFigurefromPyQt6.QtWidgetsimport (
QWidget,
QVBoxLayout, QFrame
)
classGraphView(QWidget):
def__init__(self):
super().__init__()
self.layout=QVBoxLayout()
self.separator=Noneself.chart=MplCanvas()
self.figures=Noneself.init_widget()
definit_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.defupdate_graph(self, figure):
self.chart.deleteLater()
self.chart=MplCanvas(fig=figure)
self.layout.addWidget(self.chart)
self.layout.update()
classMplCanvas(FigureCanvasQTAgg):
def__init__(self, fig=Figure()):
super(MplCanvas, self).__init__(fig)
Are you updating one Figure or do you have more than one Figure that you show/hide? Do you do that show/hide by having more than one Canvas and then doing Qt things or do you have one canvas that you attach to each figure in turn?
(1) No, I am not setting DPI internally.
(2) More than one figure, clicking the QTableWidget row effectively just requests a new figure. Every time a request is made a new MplCanvas object (inherits FigureCanvasQTAgg, see above) is being created and added to the Qt window as a widget. So basically the canvas is being deleted + new one being created every time a figure is requested.
If you are creating new Figure and FigureCanvasQtAgg each time, where is the scaled-up DPI being persisted? The source of the scaling is that on high DPI screens we upscale the user set DPI by what ever the pixel ratio is. IF you then read that DPI and set it back you can get these "always gets bigger" loops.
@tacaswell I can reproduce this bug with the following code (using v. 3.7.2). It seems like this occurs when plotting preexisting figures on a canvas (if creating a new figure each time "plot" is clicked, there are no issues). So I guess the dpi info is persisting in the figure object?
Activity
tacaswell commentedon Jul 24, 2023
Can you please try with a newer version of Matplotlib? I believe we have fixed this bug already.
hydroEng commentedon Jul 24, 2023
Thanks @tacaswell, the issue is still present in ver 3.7.2.
If i get time I will write up some simple reproducible code.
tacaswell commentedon Jul 24, 2023
Are you setting the DPI internally to your code?
Are you updating one
Figureor do you have more than one Figure that you show/hide? Do you do that show/hide by having more than one Canvas and then doing Qt things or do you have one canvas that you attach to each figure in turn?hydroEng commentedon Jul 24, 2023
(1) No, I am not setting DPI internally.
(2) More than one figure, clicking the QTableWidget row effectively just requests a new figure. Every time a request is made a new
MplCanvasobject (inheritsFigureCanvasQTAgg, see above) is being created and added to the Qt window as a widget. So basically the canvas is being deleted + new one being created every time a figure is requested.tacaswell commentedon Jul 24, 2023
If you are creating new
FigureandFigureCanvasQtAggeach time, where is the scaled-up DPI being persisted? The source of the scaling is that on high DPI screens we upscale the user set DPI by what ever the pixel ratio is. IF you then read that DPI and set it back you can get these "always gets bigger" loops.hydroEng commentedon Jul 24, 2023
honestly, I don't know, since I don't set dpi settings anywhere in my code. I'll try write something reproducible for you.
QuLogic commentedon Jul 25, 2023
There is no such version of Matplotlib?
hydroEng commentedon Jul 25, 2023
Sorry, I meant version 3.7.2 **
hydroEng commentedon Jul 25, 2023
@tacaswell I can reproduce this bug with the following code (using v. 3.7.2). It seems like this occurs when plotting preexisting figures on a canvas (if creating a new figure each time "plot" is clicked, there are no issues). So I guess the dpi info is persisting in the figure object?
Recording:
Code:
tom-a-horrocks commentedon Nov 10, 2025
I am also experiencing this bug, Matplotlib 3.10.7 and PySide 6.10.0. It happens each time I attach a new canvas to an existing Figure.
timhoffm commentedon Nov 10, 2025
Possibly fixed through #29855 (comment).
tacaswell commentedon Nov 13, 2025
Not fixed by #29855 but there is a simple fix, PR coming.
FIX: when creating a canvas from a Figure use original dpi
FIX: when creating a canvas from a Figure use original dpi