【问题标题】:How can I get the individual points and their attributes from a scatterplot in pyqtgraph?如何从 pyqtgraph 中的散点图中获取各个点及其属性?
【发布时间】:2020-02-24 04:25:39
【问题描述】:

通过在 Qt Designer 中将 Graphics View 小部件提升为 PlotWidget,我能够在 pyqtgraph 中轻松创建 ScatterPlotItem。我在上面绘制了一些随机数据,现在我想访问我单击的各个点。文档说可以连接 sigClicked(self, points) 信号,理论上应该返回光标下的点。但情况似乎并非如此,因为当我点击一个点时,无论我点击了哪个点,我都会得到相同的对象。我怀疑这个信号会返回整个 ScatterPlotItem 而不是任何特定点。

到目前为止,这是我的代码:

import sys, time
from timeit import default_timer as timer

from PyQt5 import QtGui
from PyQt5.QtCore import pyqtSlot, Qt, QPoint, QUrl, QEvent
from PyQt5.QtWidgets import *
from PyQt5 import QtMultimedia
from PyQt5.uic import loadUi
import pyqtgraph as pg

import numpy as np

class ScatterExample(QMainWindow):
    def __init__(self):

        # Main Loop
        super(ScatterExample, self).__init__()
        loadUi('<path/to/ui file>.ui', self)
        self.setWindowTitle('ScatterExample')

        self.scatter = pg.ScatterPlotItem(pxMode=False, pen=pg.mkPen(width=1, color='g'), symbol='t', size=1)
        self.scatter.sigClicked.connect(self.onPointsClicked)
        self.Scatter_Plot_View.addItem(self.scatter) # Scatter_Plot_View is the Graphics View I promoted to PlotWidget
        n = 5
        print('Number of points: ' + str(n))
        data = np.random.normal(size=(2, n))
        pos = [{'pos': data[:, i]} for i in range(n)]

        now = pg.ptime.time()
        self.scatter.setData(pos)
        print(self.scatter.data)


    def onPointsClicked(self, points):
        print('Ain\'t getting individual points ', points)
        points.setPen('b', width=2) # this turns EVERY point blue, not just the one clicked.

上面的打印语句打印:

Ain't getting individual points <pyqtgraph.graphicsItems.ScatterPlotItem.ScatterPlotItem object at 0x000001C36577F948>

如何获取我点击的点及其对应的属性,如x和y坐标?

【问题讨论】:

  • def onPointsClicked(self, points):更改为def onPointsClicked(self, obj, points):
  • 谢谢!有效!我还没有完全理解这些类的工作方式。

标签: pyqt python-3.6 pyqtgraph


【解决方案1】:

根据 eyllansec 的建议,我将 def onPointsClicked(self, points): 更改为 def onPointsClicked(self, obj, points):,现在 pyqtgraph 可以正常工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 2011-06-22
    • 2019-09-28
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多