我是 pyqt5 初学者,用 Designer 工具创建 UI 文件然后转换 py ,然后添加 figure 放到 canvas ,再放到 QGraphicsView 的 setScene 输出 pyplot 绘图,结果报错了
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'
...
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 的报错?
TypeError: addWidget(self, QWidget, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()): argument 1 has unexpected type 'Figure_Canvas'
......
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 报错,搞了半天没解决
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.