将数据从散点图传输到 lineEdit - Matplotlib 和 Pyqt5

Posted

技术标签:

【中文标题】将数据从散点图传输到 lineEdit - Matplotlib 和 Pyqt5【英文标题】:Transfer data from a scatterplot to a lineEdit - Matplotlib and Pyqt5 【发布时间】:2017-10-07 15:10:03 【问题描述】:

我使用 PyQt5 开发了 GUI,在 widget 和 2 lineEdit 中有一个交互式 scatterplot。通过单击一个点,我可以获得 x 和 y 值,但我无法用这两个值填充我的两个 lineEdit

前端代码:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(539, 400)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.lineEdit.sizePolicy().hasHeightForWidth())
        self.lineEdit.setSizePolicy(sizePolicy)
        self.lineEdit.setObjectName("lineEdit")
        self.gridLayout.addWidget(self.lineEdit, 0, 0, 1, 1)
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setObjectName("widget")
        self.gridLayout.addWidget(self.widget, 0, 1, 2, 1)
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.lineEdit_2.sizePolicy().hasHeightForWidth())
        self.lineEdit_2.setSizePolicy(sizePolicy)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.gridLayout.addWidget(self.lineEdit_2, 1, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

后端代码:

import sys
import matplotlib.pyplot as plt
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QSizePolicy
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from front_test import Ui_MainWindow


class Graph_init(FigureCanvas):

    def __init__(self, parent=None):
        fig = Figure()
        fig.patch.set_facecolor("None")
        self.axes = fig.add_subplot(111)
        self.compute_initial_figure()
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)
        FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)
        FigureCanvas.mpl_connect(self, 'pick_event', self.onclick)

    def onclick(self, event):
        ind = event.ind[0]
        data = event.artist.get_offsets()
        xdata, ydata = data[ind,:]
        print ((xdata, ydata))

        index = [10,20,30].index(xdata)

        x = [10,20,30]
        y = [100,100,150]
        size = [1000,2000,3000]

        x_high = x[index]
        y_high = y[index]
        size_high = size[index]

        del x[index]
        del y[index]
        del size[index]

        self.axes.clear()
        self.axes.scatter(x, y, s=size, color='blue', picker=1, alpha=0.3)
        self.axes.scatter(x_high, y_high, s=size_high, color='red', picker=1, alpha=0.3)
        self.axes.figure.canvas.draw()

        #self.lineEdit.setText(xdata)
        #self.lineEdit_2.setText(ydata)


class Graph_populate(Graph_init):

    def compute_initial_figure(self):
        x = [10,20,30]
        y = [100,100,150]
        size = [1000,2000,3000]
        self.axes.scatter(x,y,s=size,color='blue', picker=1, alpha=0.3)


class GUI(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(GUI, self).__init__(parent)
        self.setupUi(self)
        self.sc = Graph_populate(self.widget)
        self.gridLayout.addWidget(self.sc, 0, 1, 2, 1)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    prog = GUI()
    prog.showMaximized()
    sys.exit(app.exec_())

谢谢

【问题讨论】:

【参考方案1】:

您不能更新 Graph_init 中的 QLineEdits,因为它们在该上下文中不存在,在这种情况下,建议通过信号发送上下文存在的信息,必须创建此信号:

posClicked = QtCore.pyqtSignal(tuple)

而当你想发送emit()时使用

self.posClicked.emit((xdata, ydata))

然后它必须连接到一个槽,用于填充 QLineEdits,如下所示:

class Graph_init(FigureCanvas):
    posClicked = QtCore.pyqtSignal(tuple)
    def __init__(self, parent=None):
        [...]

    def onclick(self, event):
        ind = event.ind[0]
        data = event.artist.get_offsets()
        xdata, ydata = data[ind,:]
        print ((xdata, ydata))

        [...]
        self.axes.figure.canvas.draw()

        self.posClicked.emit((xdata, ydata))


class GUI(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(GUI, self).__init__(parent)
        self.setupUi(self)
        self.sc = Graph_populate(self.widget)
        self.gridLayout.addWidget(self.sc, 0, 1, 2, 1)
        self.sc.posClicked.connect(self.onPosClicked)

    def onPosClicked(self, values):
        xdata, ydata = values
        self.lineEdit.setText(str(xdata))
        self.lineEdit_2.setText(str(ydata))

【讨论】:

以上是关于将数据从散点图传输到 lineEdit - Matplotlib 和 Pyqt5的主要内容,如果未能解决你的问题,请参考以下文章

怎样在excel中求相关系数

如何从这些数据创建点图(不是散点图)?

Python使用matplotlib模块绘制多条折线图散点图

怎样用Eviews画散点图

如何将标签注释到 3D matplotlib 散点图?

eviews怎么做散点图