V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ALLROBOT
V2EX  ›  程序员

pyqt5 输出绘图出错 Qt.WindowFlags()): argument 1 has unexpected type 'Figure_Canvas'

  •  
  •   ALLROBOT · 2022-04-08 23:03:43 +08:00 · 1278 次点击
    这是一个创建于 960 天前的主题,其中的信息可能已经有所发展或是发生改变。

    说明

    我是 pyqt5 初学者,用 Designer 工具创建 UI 文件然后转换 py ,然后添加 figure 放到 canvas ,再放到 QGraphicsView 的 setScene 输出 pyplot 绘图,结果报错了

    报错 1

    Traceback (most recent call last):
      File "C:\Users\Administrator\PycharmProjects\Request_Project\Non_real_time_recognition.py", line 155, in <module>
        ui.setupUi(form)
      File "C:\Users\Administrator\PycharmProjects\Request_Project\Non_real_time_recognition.py", line 87, in setupUi
        proxy_widget=self.scene.addWidget(canvas)
    TypeError: addWidget(self, QWidget, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()): argument 1 has unexpected type 'FigureCanvasTemplate'
    

    代码 1

    ...
    class UiForm(object):
        def __init__(self):
            E1 = scio.loadmat('S1_E1_A1.mat')
            # display emg data,shape:[2292526,16]
            self.E1_emg = E1['emg']
            self.E1_label = E1['restimulus']
        def setupUi(self,Form):
            Form.setObjectName("Form")
            Form.resize(550, 419)
            Form.setFixedSize(Form.width(), Form.height())
            # plt.plot(self.E1_emg[0:200000] * 20000)
            # plt.plot(self.E1_label[0:200000]*100)
    
            self.Plotting = QtWidgets.QGraphicsView(Form)
            self.Plotting.setGeometry(QtCore.QRect(10, 10, 361, 171))
            self.Plotting.setObjectName("Plotting")
            self.scene=QtWidgets.QGraphicsScene()
    
            figure=Figure()
            axes=figure.gca()
            axes.set_title("sEMG")
            x=np.linspace(0,200000,num=200000)
            y=self.E1_emg[0:200000] * 20000
            axes.plot(x,y,"-k",label="sEMG")
            axes.legend()
            axes.grid(True)
    
            canvas=FigureCanvas(figure)
    
            proxy_widget=self.scene.addWidget(canvas)
            self.Plotting.setScene(proxy_widget)
    
            self.Image = QtWidgets.QGraphicsView(Form)
            self.Image.setGeometry(QtCore.QRect(390, 10, 151, 171))
            self.Image.setObjectName("Image")
            .....
    

    换别的方法,似乎都是 Figure canvas 的报错?

    报错 2

    TypeError: addWidget(self, QWidget, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()): argument 1 has unexpected type 'Figure_Canvas'
    

    代码 2

    ......
    class Figure_Canvas(FigureCanvas):
        def __init__(self, parent=None, width=360, height=170, dpi=100):
            E1 = scio.loadmat('S1_E1_A1.mat')
            print(E1.keys())
            self.E1_emg = E1['emg']
            self.E1_label = E1['restimulus']
            fig = Figure(figsize=(width, height), dpi=100)
            FigureCanvas.__init__(self, fig)
            self.axes = fig.add_subplot(111)
    	# 测试输出
        def updatePlot(self):
            x = [1,2,3,4,5,6,7,8,9]
            y = [23,21,32,13,3,132,13,3,1]
            self.axes.plot(x, y)
    
    class UiForm(object):
        def __init__(self):
    
            f = h5py.File('DB2_S1_Image_[200_16_1].h5', 'r')
            image = np.array(f['imageData'])
            print(image.shape)
            label = np.array(f['imageLabel'])
            print(label.shape)
            f.close()
            # self.figure=MyFigureCanvas()
    
        def setupUi(self,Form):
            Form.setObjectName("Form")
            Form.resize(550, 419)
            Form.setFixedSize(Form.width(), Form.height())
            # plt.plot(self.E1_emg[0:200000] * 20000)
            # plt.plot(self.E1_label[0:200000]*100)
            #
            # self.Plotting = QtWidgets.QGraphicsView(Form)
            # self.Plotting.setGeometry(QtCore.QRect(10, 10, 361, 171))
            # self.Plotting.setObjectName("Plotting")
            # self.scene=QtWidgets.QGraphicsScene()
            #
            # figure=Figure()
            # axes=figure.gca()
            # axes.set_title("sEMG")
            # x=np.linspace(0,200000,num=200000)
            # y=self.E1_emg[0:200000] * 20000
            # axes.plot(x,y,"-k",label="sEMG")
            # axes.legend()
            # axes.grid(True)
            #
            # canvas=FigureCanvas(figure)
            #
            # proxy_widget=self.scene.addWidget(canvas)
            # self.Plotting.setScene(proxy_widget)
    
            self.gridLayoutWidget = QtWidgets.QWidget(Form)
            self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 10, 361, 171))
            self.gridLayoutWidget.setObjectName("gridLayoutWidget")
    
    
            self.gridLayout_2 = QtWidgets.QGridLayout(self.gridLayoutWidget)
            self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
            self.gridLayout_2.setObjectName("gridLayout_2")
    
            # ===通过 graphicview 来显示图形
    
            self.graphicview = QtWidgets.QGraphicsView(self.gridLayoutWidget)
            self.graphicview.setObjectName("graphicview")
            self.gridLayout_2.addWidget(self.graphicview, 0, 0)
    
            self.dr = Figure_Canvas()
            self.dr.updatePlot()  # 画图
    
            self.graphicscene = QtWidgets.QGraphicsScene(Form)
            self.graphicscene.addWidget(self.dr)
            self.graphicview.setScene(self.graphicscene)
            self.graphicview.show()
            ......
    

    怎么解决啊😂,Matplotlib 嵌入 pyqt 报错,搞了半天没解决

    ec0
        1
    ec0  
       2022-04-09 01:28:10 +08:00   ❤️ 1
    试着跑了下代码

    1. 你的 FigureCanvas 和 Figure 是怎么导入(import)的?我是这样
    from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
    from matplotlib.figure import Figure

    2. 代码 1 的 setupUi 函数里的
    self.Plotting.setScene(proxy_widget)
    我改成了 self.Plotting.setScene(self.scene)

    3. 代码 2 的 class Figure_Canvas 的 init 函数里,参数的 width 和 height 设置成了 360,170 然后传给了 Figure ,Figure 的 figsize 的单位是 inches 而不是 px ,360 inches 太大了,会 Out of memory
    所以我把 Figure(figsize=(width, height), dpi=100) 改成了 Figure(dpi=100) ,使用默认的 figsize( [6.4, 4.8])
    ALLROBOT
        2
    ALLROBOT  
    OP
       2022-04-09 09:18:16 +08:00
    @ec0 #1
    1. 导入 from matplotlib.backends.backend_template import FigureCanvas ,改成你的 from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

    2. 和 3.随着导入改变,就成功跑通了

    感谢!!!😂我还得想办法去除绘图周边的空白,使图片完整的显示~DPI 调小了字看不清
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1086 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 23:27 · PVG 07:27 · LAX 15:27 · JFK 18:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.