1

I would like to plot on my GUI with pyqt5 using matplotlib. I have created a class called MatplotlibWidget which create the figure and canvas of my plot. But I have a problem to generate my canvas with the FigureCanvasQTAgg function (which is a matplotlib function).

Here the part of my code which is bugging:

import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
from matplotlib.figure import Figure

#Some more code...not relevant

class MatplotlibWidget(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.fig = Figure()
        self.canvas = FigureCanvasQTAgg(self.fig) #line 86
        self.axis = self.fig.add_subplot(111)

        self.layoutVerticalTest = QVBoxLayout(self)
        self.layoutVerticalTest.addWidget(self.canvas)

I have this error :

File "/Users/AlexisTuil/Desktop/projet inno/sc_analysis/visualisation.py", line 86, in 
__init__self.canvas = FigureCanvasQTAgg(self.fig)
File "/usr/local/lib/python3.5/site packages/matplotlib/backends/backend_qt4agg.py", line 76, in     
__init__FigureCanvasQT.__init__(self, figure)
File "/usr/local/lib/python3.5/site-packages/matplotlib/backends/backend_qt4.py", line 71, in
__init__QtWidgets.QWidget.__init__(self)
TypeError: __init__() missing 1 required positional argument: 'figure'
Abort trap: 6

I've searched on many forums but I couldn't find out a solution to my problem. I don't get it why there is a missing "positional argument". Please help me !

I am on MacOS El Capitan with python3.5 64bit. I installed matplotlib with pip if it can help.

1
  • I'm not very familiar with Qt but from the trace it looks like QWidget.__init__() is what's missing a positional argument. Commented Mar 30, 2016 at 14:55

1 Answer 1

4

If using pyqt5, do this:

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.