Pyqt 应用在不同的监视器上使用 matplotlib 绘图时出现奇怪行为。

huangapple go评论56阅读模式
英文:

Pyqt application using matplotlib plots strange behavior when used in a different monitor

问题

我正在创建一个Pyqt应用程序我希望在加载应用程序时绘制一个空图然后每次按下按钮时绘制一些数据

这是我的当前最小工作示例

```python
import matplotlib.pyplot as plt
from random import random
from PyQt6 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg


class Window(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.resize(1126, 568)
        self.centralwidget = QtWidgets.QWidget()
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.pushButtonConnect = QtWidgets.QPushButton("Connect")
        self.gridLayout.addWidget(self.pushButtonConnect, 0, 0, 1, 1)
        self.setCentralWidget(self.centralwidget)
        self.fig, self.ax = plt.subplots()
        self.canvas = FigureCanvasQTAgg(self.fig)
        self.gridLayout.addWidget(self.canvas, 0, 1, 1, 1)
        self.pushButtonConnect.clicked.connect(self.runGraph)
        self.ax.set_xlim([0, 1])
        self.ax.set_ylim([0, 1])

    def runGraph(self):
        canvas_new = FigureCanvasQTAgg(self.fig)
        plt.plot([random(), random()], [random(), random()])
        self.gridLayout.replaceWidget(self.canvas, canvas_new)
        self.canvas = canvas_new


if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    win = Window()
    win.show()
    app.exec()

这会创建一个应用程序,在点击按钮5次后,看起来是这样的

然而,如果我在我的笔记本屏幕上打开相同的应用程序(而不是在我的外部显示器上),每次我点击按钮时,每条线都会变得更粗,5次后看起来像这样

更甚者:如果我尝试调整主窗口的大小:

self.resize(1126, 568)

在我的显示器上在点击按钮5次后看起来正常,但在我的笔记本屏幕上看起来像这样,就好像在点击按钮时调整了网格。

我是做错了什么,还是matplotlib+Pyqt存在bug?

非常感谢您提前的帮助。


<details>
<summary>英文:</summary>

I&#39;m creating a Pyqt application where i want to plot an empty figure when the application is loaded, and then plot some data every time a button is pressed.

Here is my current minumum working example:

```python
import matplotlib.pyplot as plt
from random import random
from PyQt6 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg


class Window(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.resize(1126, 568)
        self.centralwidget = QtWidgets.QWidget()
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.pushButtonConnect = QtWidgets.QPushButton(&quot;Connect&quot;)
        self.gridLayout.addWidget(self.pushButtonConnect, 0, 0, 1, 1)
        self.setCentralWidget(self.centralwidget)
        self.fig, self.ax = plt.subplots()
        self.canvas = FigureCanvasQTAgg(self.fig)
        self.gridLayout.addWidget(self.canvas, 0, 1, 1, 1)
        self.pushButtonConnect.clicked.connect(self.runGraph)
        self.ax.set_xlim([0, 1])
        self.ax.set_ylim([0, 1])

    def runGraph(self):
        canvas_new = FigureCanvasQTAgg(self.fig)
        plt.plot([random(), random()], [random(), random()])
        self.gridLayout.replaceWidget(self.canvas, canvas_new)
        self.canvas = canvas_new


if __name__ == &quot;__main__&quot;:
    app = QtWidgets.QApplication([])
    win = Window()
    win.show()
    app.exec()

Which gives an application that, after 5 button clicks
looks like this.

However if a open the same application on my laptop screen (rather than in my external monitor), every time I click the button every line get bigger, and after 5 clicks
looks like this.

And even more: If I try to resize the main Window with:

self.resize(1126, 568)

on my monitor looks normal after 5 clicks, but on my laptop screen it looks like this, like it's resizing the grid if I click the button.

I'm doing something wrong or it's a bug of matplotlib+Pyqt?

Thank you very much in advance.

答案1

得分: 0

根据评论中relent95的建议,只需使用self.canvas.draw()就可以完成任务并解决问题。
所以现在函数如下:

def runGraph(self):
    plt.plot([random(), random()], [random(), random()])
    self.canvas.draw()

另一方面,我的笔记本电脑奇怪行为的谜团将保持未解之谜。

英文:

As relent95 suggested in the comment, simply using self.canvas.draw() does the job and fix the issue.
So now the function is:

def runGraph(self):
    plt.plot([random(), random()], [random(), random()])
    self.canvas.draw()

On the other hand, the mystery of my laptop strange behaviour will stay unsolved.

huangapple
  • 本文由 发表于 2023年7月13日 17:51:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定