在 MatplotlibWidget pyqt5 中使用 FigureCanvasQTAgg 时出错 [英] Error using FigureCanvasQTAgg in MatplotlibWidget pyqt5

查看:533
本文介绍了在 MatplotlibWidget pyqt5 中使用 FigureCanvasQTAgg 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 pyqt5 使用 matplotlib 在我的 GUI 上绘图.我创建了一个名为 MatplotlibWidget 的类,它创建了我的绘图的图形和画布.但是我在使用 FigureCanvasQTAgg 函数(这是一个 matplotlib 函数)生成我的画布时遇到了问题.

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)

我遇到此错误:

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 !

我在使用python3.5 64bit的MacOS El Capitan上.如果有帮助,我用 pip 安装了 matplotlib.

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

谢谢大家:)

推荐答案

如果使用pyqt5,请执行以下操作:

If using pyqt5, do this:

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg

这篇关于在 MatplotlibWidget pyqt5 中使用 FigureCanvasQTAgg 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆