Search code examples
pythonmatplotlibpyqt5qsplitter

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. When I move the Splitter to the edge (the figure disapear then), the code crashes.

enter image description here

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. 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.


Solution

  • 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. The solution is to override the resizeEvent method:

    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)
    # ...