错误,将绘制曲线的Pyplot添加到QgraphicsView

发布于 2025-01-19 20:02:59 字数 2674 浏览 0 评论 0原文

我对此很陌生,想知道在使用 Pyuic 转换的 py 文件时尝试添加绘图时出了什么问题。 我试图遵循该网页的答案,但我似乎犯了一个错误。 将 Matplotlib 绘图作为 QGraphicsItem/放入 QGraphicsView< /a>

错误

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'

代码:

import h5py
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
import scipy.io as scio
from PyQt5.QtCore import QSize
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsPixmapItem
from matplotlib.backends.backend_template import FigureCanvas
from PyQt5.QtGui import QPainter, QPixmap, QWheelEvent
from matplotlib.figure import Figure
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")
        .....

图片除外

Excpect1

Excpect2

I am new to this and wonder what went wrong when trying to add plot when using Pyuic converted py files.
I tried to follow the answer of this web page, but I seem to have made a mistake.
Put a Matplotlib plot as a QGraphicsItem/into a QGraphicsView

ERROR

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'

Code:

import h5py
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
import scipy.io as scio
from PyQt5.QtCore import QSize
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsPixmapItem
from matplotlib.backends.backend_template import FigureCanvas
from PyQt5.QtGui import QPainter, QPixmap, QWheelEvent
from matplotlib.figure import Figure
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")
        .....

Excpect Picture

Excpect1

Excpect2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身泪倾城 2025-01-26 20:02:59

问题解决了。

我导入 from matplotlib.backends.backend_template import FigureCanvas,现在导入 from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

self.Plotting.setScene(proxy_widget)self.Plotting.setScene(self.scene)

The problem is solved.

I import from matplotlib.backends.backend_template import FigureCanvas, now import from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas.

self.Plotting.setScene(proxy_widget) to self.Plotting.setScene(self.scene)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文