简体   繁体   English

当我用 matplotlib 图形将 Qslitter 移动到边缘时,如何避免 PyQt5 崩溃?

[英]How avoid PyQt5 to crash when I move Qslitter to the edge with a matplotlib figure in it?

I have two matplotlib figures inside a QSplitter.我在 QSplitter 中有两个 matplotlib 人物。 When I move the Splitter to the edge (the figure disapear then), the code crashes.当我将拆分器移动到边缘时(然后数字消失),代码崩溃。

在此处输入图像描述

the error I get is我得到的错误是

Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\matplotlib\backends\backend_qt5.py", line 397, in resizeEvent
    self.figure.set_size_inches(winch, hinch, forward=False)
  File "C:\Python36\lib\site-packages\matplotlib\figure.py", line 902, in set_size_inches
    raise ValueError(f'figure size must be positive finite not {size}')
ValueError: figure size must be positive finite not [0.   6.12]

This wasn't an issue before, when only one of the two side of the Qsplitter was on the screen, my code did not crash.以前这不是问题,当 Qsplitter 的两侧只有一个在屏幕上时,我的代码没有崩溃。 When I pull the splitter to the center again, the figure reappear properly.当我再次将分离器拉到中心时,该图形再次正确出现。

here a minimal example:这是一个最小的例子:

from PyQt5.QtGui import *
from PyQt5.QtWidgets import  *
from PyQt5.QtCore import * 
import sys
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

class window(QMainWindow):
    def __init__(self, parent=None):
        super(window, self).__init__()
        self.parent = parent

        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)

        self.mainHBOX_param_scene = QHBoxLayout()

        self.mainsplitter = QSplitter(Qt.Horizontal)
        V1 = Viewer()
        V2 = Viewer()
        self.mainsplitter.addWidget(V1)
        self.mainsplitter.addWidget(V2)


        self.mainHBOX_param_scene.addWidget(self.mainsplitter)
        self.centralWidget.setLayout(self.mainHBOX_param_scene)


class Viewer(QGraphicsView):
    def __init__(self, parent=None):
        super( Viewer, self).__init__(parent)
        self.parent = parent
        self.setStyleSheet("border: 0px")
        self.scene = QGraphicsScene(self)
        self.setScene(self.scene)
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)

        self.axes_Delay = self.figure.add_subplot(1, 1,1)
        self.axes_Delay.set_title("Title")

        # self.canvas.setGeometry(0, 0, 1600, 500 )
        layout = QVBoxLayout()
        layout.addWidget(self.canvas)
        self.setLayout(layout)
        self.canvas.show()


    def closeEvent(self, event):
        plt.close(self.figure)


def main():
    app = QApplication(sys.argv)
    ex = window(app)
    ex.show()
    sys.exit(app.exec_( ))


if __name__ == '__main__':
    main()

How can I avoid this error?我怎样才能避免这个错误? I use matplotlib version 3.2.1 by the way.顺便说一句,我使用的是 matplotlib 版本 3.2.1。

Clearly it is a matplotlib bug that has not considered that the size of the resizeEvent can be <=0, and that information is used to paint through the associated Figure causing this type of error.显然这是一个 matplotlib 错误,没有考虑到 resizeEvent 的大小可以 <= 0,并且该信息用于通过关联的图形进行绘制导致此类错误。 The solution is to override the resizeEvent method:解决方案是重写 resizeEvent 方法:

class FixFigureCanvas(FigureCanvas):
    def resizeEvent(self, event):
        if event.size().width() <= 0 or event.size().height() <= 0:
            return
        super(FixFigureCanvas, self).resizeEvent(event)
# ...
self.figure = plt.figure()
self.canvas = FixFigureCanvas(self.figure)

self.axes_Delay = self.figure.add_subplot(1, 1, 1)
# ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Matplotlib 和 PyQt5 绘图图 - Matplotlib and PyQt5 Plotting Figure 在 PyQt5 中调整父窗口大小时,如何更改 matplotlib 图形/轴的拉伸方式? - How can you change how a matplotlib figure/axes is stretched when resizing the parent window in PyQt5? PyQt5中的Matplotlib:如何删除边缘上的小空间 - Matplotlib in PyQt5: How to remove the small space along the edge 如何通过简单地在 PyQt5 中拖动图形来移动图形(使用paintEvent 创建) - How to move a figure(created using paintEvent) by simply draging it in PyQt5 当在 PyQt5 中的不同 class 中按下按钮时,如何更新图形? - How can I update a figure when button is pressed in a different class in PyQt5? 如何在 PyQt5 Qwidget 上显示 matplotlib - How can I show matplotlib on my PyQt5 Qwidget 在 PyQt5 中,如何从 QStackedWidget 上的 label 单击按钮时移动到另一个 label? - In PyQt5, how do I move to another label when clicking a button from a label on a QStackedWidget? 如何通过鼠标在 gui 或图形中绘制交互式固定网格? /python / matplotlib / pyqt5 - how to draw by mouse an interactive fixed grid within a gui or figure? /python / matplotlib / pyqt5 Matplotlib 图与 PyQt5/PySide2 QSplitter Widget 之间的问题 - Problem between Matplotlib figure & the PyQt5/PySide2 QSplitter Widget PyQT5 和 matplotlib 图,事件循环已经在运行 - PyQT5 with matplotlib figure, the event loop is already running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM