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

2022-04-08 23:03:43 +08:00
 ALLROBOT

说明

我是 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 报错,搞了半天没解决

1246 次点击
所在节点    程序员
2 条回复
ec0
2022-04-09 01:28:10 +08:00
试着跑了下代码

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
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 调小了字看不清

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/845816

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX