Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

I can't import FigureCanvas



  • Hi everyone
    I created a simple GUI using PyQt5 and matplotlib and it works without problem. But i see this error in line which i import FigureCanvas

    from matplotlib.backends.backend_qt5agg import FigureCanvas
    
    Cannot find reference 'FigureCanvas' in 'backend_qt5agg.py'
    

    Here is my code:
    main.py

    # ------------------------------------------------------
    # ---------------------- main.py -----------------------
    # ------------------------------------------------------
    from PyQt5.QtWidgets import *
    from PyQt5.uic import loadUi
    
    from matplotlib.backends.backend_qt5agg import (NavigationToolbar2QT as NavigationToolbar)
    
    import numpy as np
    import random
    
    
    class MatplotlibWidget(QMainWindow):
    
        def __init__(self):
            QMainWindow.__init__(self)
    
            loadUi("qt_designer.ui", self)
    
            self.setWindowTitle("PyQt5 & Matplotlib Example GUI")
    
            self.pushButton.clicked.connect(self.update_graph)
    
            self.addToolBar(NavigationToolbar(self.MplWidget.canvas, self))
    
        def update_graph(self):
            fs = 500
            f = random.randint(1, 100)
            ts = 1 / fs
            length_of_signal = 100
            t = np.linspace(0, 1, length_of_signal)
    
            cosinus_signal = np.cos(2 * np.pi * f * t)
            sinus_signal = np.sin(2 * np.pi * f * t)
    
            self.MplWidget.canvas.axes.clear()
            self.MplWidget.canvas.axes.plot(t, cosinus_signal)
            self.MplWidget.canvas.axes.plot(t, sinus_signal)
            self.MplWidget.canvas.axes.legend(('cosinus', 'sinus'), loc='upper right')
            self.MplWidget.canvas.axes.set_title('Cosinus - Sinus Signal')
            self.MplWidget.canvas.draw()
    
    
    app = QApplication([])
    window = MatplotlibWidget()
    window.show()
    app.exec_()
    

    mplwidget.py

    # ------------------------------------------------------
    # -------------------- mplwidget.py --------------------
    # ------------------------------------------------------
    from PyQt5.QtWidgets import *
    
    from matplotlib.backends.backend_qt5agg import FigureCanvas
    
    from matplotlib.figure import Figure
    
    
    class MplWidget(QWidget):
    
        def __init__(self, parent=None):
            QWidget.__init__(self, parent)
    
            self.canvas = FigureCanvas(Figure())
    
            vertical_layout = QVBoxLayout()
            vertical_layout.addWidget(self.canvas)
    
            self.canvas.axes = self.canvas.figure.add_subplot(111)
            self.setLayout(vertical_layout)
    

  • Qt Champions 2019

    @hernancrespo89 Did you check whether FigureCanvas in fact is in backend_qt5agg.py?




Log in to reply