如何使用 matplotlib 的对象选取功能在 PyQt 文本字段中显示值?

Posted

技术标签:

【中文标题】如何使用 matplotlib 的对象选取功能在 PyQt 文本字段中显示值?【英文标题】:How to display a value in a PyQt text field using matplotlib's object picking function? 【发布时间】:2016-04-06 12:25:06 【问题描述】:

我将 PyQt 4 用于基本 GUI,将 matplotlib 用于绘图,我想从中读取绘制数据点的坐标。基于these examples(简单选择示例),我有一个简单的问题,即我无法在 QtGui.QLabel() 等文本字段中显示数据点的坐标。我不明白为什么我不能在方法onpick() 中调用实例Window.msg。可能是因为它没有给方法的实例。我对面向对象编程只有一个基本的了解(但我正在努力),所以问题是我缺乏知识。

我的问题:如何在基于 PyQT 的 GUI 中的 matplotlib 图中显示所选数据的坐标(通过单击它)(在这种情况下,在我的标签 lbl 中)?

此外,最好在图中突出显示所选数据点。

这是我的代码(工作):

import numpy as np
import matplotlib.pyplot as plt
from PyQt4 import QtGui
import sys
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
import matplotlib.pyplot as plt


class Window(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.initUI()

    def initUI(self):  
       self.msg = '0'

       # a figure instance to plot on
       self.figure = plt.figure()
       self.canvas = FigureCanvas(self.figure)
       self.toolbar = NavigationToolbar(self.canvas, self)

       # a label
       self.lbl = QtGui.QLabel(self.msg)

       # set the layout
       layout = QtGui.QVBoxLayout()
       layout.addWidget(self.toolbar)
       layout.addWidget(self.canvas)
       layout.addWidget(self.lbl)
       self.setLayout(layout)

       self.plot()

    def plot(self):
        # random data
        data = [np.random.random() for i in range(10)]
        # create an axis
        ax = self.figure.add_subplot(111)
        # discards the old graph
        ax.hold(False)
        # plot data
        line, = ax.plot(data, 'o', picker=5)  # 5 points tolerance
        self.canvas.draw()
        self.canvas.mpl_connect('pick_event',  Window.onpick)

    def onpick(self):
        thisline = self.artist
        xdata = thisline.get_xdata()
        ydata = thisline.get_ydata()
        ind = self.ind

        # show data
        self.msg = (xdata[ind], ydata[ind])
        print(self.msg)

        # This does not work:
        #Window.lbl.setText(self.msg)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    main = Window()
    main.show()
    sys.exit(app.exec_())

【问题讨论】:

【参考方案1】:

self 被选择器重叠(不知道为什么)。无论如何,这应该有效:

import numpy as np
import matplotlib.pyplot as plt
from PyQt4 import QtGui
import sys
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
import matplotlib.pyplot as plt

class Window(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.initUI()

    def initUI(self):  
       self.msg = '0'

       # a figure instance to plot on
       self.figure = plt.figure()
       self.canvas = FigureCanvas(self.figure)
       self.toolbar = NavigationToolbar(self.canvas, self)

       # a label
       self.lbl = QtGui.QLabel(self.msg)

       # set the layout
       layout = QtGui.QVBoxLayout()
       layout.addWidget(self.toolbar)
       layout.addWidget(self.canvas)
       layout.addWidget(self.lbl)
       self.setLayout(layout)

       self.plot()

    def changelabel(arg):
        main.lbl.setText(str(arg[0])+' '+str(arg[1]))

    def plot(self):
        # random data
        data = [np.random.random() for i in range(10)]
        # create an axis
        ax = self.figure.add_subplot(111)
        # discards the old graph
        ax.hold(False)
        # plot data
        line, = ax.plot(data, 'o', picker=5)  # 5 points tolerance
        self.canvas.draw()
        self.canvas.mpl_connect('pick_event',  Window.onpick)

    def onpick(self):
        thisline = self.artist
        xdata = thisline.get_xdata()
        ydata = thisline.get_ydata()
        ind = self.ind

        # show data
        self.msg = (xdata[ind], ydata[ind])
        print(self.msg)
#        Window.changelabel(self.msg)
        main.lbl.setText(str(self.msg[0])+' '+str(self.msg[1]))


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    main = Window()
    main.show()
    sys.exit(app.exec_())

,更改在setText 函数中,因为我直接从变量中调用它(没有selfWindow)。

main.lbl.setText(str(self.msg[0])+' '+str(self.msg[1]))

【讨论】:

以上是关于如何使用 matplotlib 的对象选取功能在 PyQt 文本字段中显示值?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 matplotlib 中添加悬停注释

如何使用`matplotlib`可视化5-D功能集和回归结果?

Matplotlib 使用GridSpec和其他功能自定义图形布局

绘图: matplotlib核心剖析

jquery中如何选择选取DOM元素?

如何将 Matplotlib 图形转换为 PIL Image 对象(不保存图像)