Search code examples
pythonmatplotlibpyqt5

tabbed matplotlib plots: QGuiApplication::font() error


I use this convenient class to plot several tabbed plots within the same matplotlib window.

It works amazingly but makes Python crash after the second window creation with error QGuiApplication::font(): no QGuiApplication instance and no application font set. Minimal working example:

from plotWindow import plotWindow
import matplotlib.pyplot as plt
import numpy as np

for n in range(3):
    pw = plotWindow()
    x = np.arange(0, 10, 0.001)
    for i in range(1,3):
        f = plt.figure()
        ysin = np.sin(i*x)
        plt.plot(x, ysin, '--')
        pw.addPlot(str(i), f)
    pw.show()

I know very little about PyQt5, so I do not really know where to look for. Thank you for you help!

P.S.: For the sake of completeness: here is the full code of the class:

import matplotlib
matplotlib.use('qt5agg')

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
import numpy as np
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget,QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
import sys


class plotWindow():
    def __init__(self, parent=None):
        self.app = QApplication(sys.argv)
        self.MainWindow = QMainWindow()
        self.MainWindow.__init__()
        self.MainWindow.setWindowTitle("plot window")
        self.canvases = []
        self.figure_handles = []
        self.toolbar_handles = []
        self.tab_handles = []
        self.current_window = -1
        self.tabs = QTabWidget()
        self.MainWindow.setCentralWidget(self.tabs)
        self.MainWindow.resize(1280, 900)
        self.MainWindow.show()

    def addPlot(self, title, figure):
        new_tab = QWidget()
        layout = QVBoxLayout()
        new_tab.setLayout(layout)

        figure.subplots_adjust(left=0.05, right=0.99, bottom=0.05, top=0.91, wspace=0.2, hspace=0.2)
        new_canvas = FigureCanvas(figure)
        new_toolbar = NavigationToolbar(new_canvas, new_tab)

        layout.addWidget(new_canvas)
        layout.addWidget(new_toolbar)
        self.tabs.addTab(new_tab, title)

        self.toolbar_handles.append(new_toolbar)
        self.canvases.append(new_canvas)
        self.figure_handles.append(figure)
        self.tab_handles.append(new_tab)

    def show(self):
        self.app.exec_()

This issue has been reported (but not answered) here.


Solution

  • Fixed it by slightly editing the initiation of the class (see full snipped there, and detailed changes there):

        def __init__(self, parent=None):
            self.app = QApplication.instance()
            if not self.app:
                self.app = QApplication(sys.argv)
            self.MainWindow = QMainWindow()
            self.MainWindow.setWindowTitle("plot window")
            self.canvases = []
            self.figure_handles = []
            self.toolbar_handles = []
            self.tab_handles = []
            self.current_window = -1
            self.tabs = QTabWidget()
            self.MainWindow.setCentralWidget(self.tabs)
            self.MainWindow.resize(1280, 900)
            self.MainWindow.show()