Mouse Interaction with 3D Scatter Plots

86 views
Skip to first unread message

Alexander Groeger

unread,
Jul 8, 2022, 2:11:45 PM7/8/22
to pyqtgraph
Hi all,

I've been trying to build a point cloud visualization where the user may hover, click, or draw a box over points in a 3D scatter plot to highlight them. I don't know if there exists any built-in functionality for this. I found another similar post from 2016 in this group using an older version of pyqtgraph, but I had difficulties getting that solution to run, so I figured I'd bring up the issue again.

Here's my code. Right now it only aims to allow the user to click on a point and change its color to green, but the problem is I am using GLViewWidget's itemsAt method to return the GLScatterPlotItem, but it does not distinguish the particular point my mouse clicked, therefore all points turn green. Is there a short and sweet solution already out there?


import numpy as np
import pyqtgraph as pg
import pyqtgraph.opengl as gl

app = pg.mkQApp("GLScatterPlotItem Example")

class MyView(gl.GLViewWidget):

    def __init__(self):
        super().__init__()

    def mousePressEvent(self, e):
        super().mousePressEvent(e)

        items = w.itemsAt((e.pos().x()-5, e.pos().y()-5, 10, 10))
        if len(items) == 0:
            return
        print(items)
        for item in items:
            if type(item) == gl.GLScatterPlotItem:
                item.setData(color = (0,1,0,0.5))
        e.accept()

w = MyView()
w.show()
w.setCameraPosition(distance=10)

n = 100
pos = np.random.normal(0,1,(n,3))
size = 0.25*np.ones((n,))
color = np.ones((n,4))
color[:,-1] = 0.5

sp = gl.GLScatterPlotItem(pos=pos, size=size, color=color, pxMode=False)
w.addItem(sp)

if __name__ == '__main__':
    pg.exec()
Reply all
Reply to author
Forward
0 new messages