Search code examples
pythonpoint-cloudsvispy

vispy: two data sets on same plot with colors


I have two 3d numpy arrays, and am trying to plot them in two distinct colors via a vispy 3d scatter.

I am already familiar with how you set data via scatter on vispy:

scatter.set_data(data)

where data is in the form of a numpy array. However, I need to plot two different data sets with two distinct colors, and I feel like there is something very obvious I am missing here. I'm fine with treating the two point sets as one data set, but then how do you get two distinct colors for the point groups?


Solution

  • I modified the scatter example from the vispy repo to do what you are asking.

    Result:

    enter image description here

    Code:

    # pylint: disable=no-member
    """ scatter using MarkersVisual """
    
    import numpy as np
    import sys
    
    from vispy import app, visuals, scene
    
    
    # build your visuals, that's all
    Scatter3D = scene.visuals.create_visual_node(visuals.MarkersVisual)
    
    # The real-things : plot using scene
    # build canvas
    canvas = scene.SceneCanvas(keys='interactive', show=True)
    
    # Add a ViewBox to let the user zoom/rotate
    view = canvas.central_widget.add_view()
    view.camera = 'turntable'
    view.camera.fov = 45
    view.camera.distance = 500
    
    # data
    n = 500
    # generate 2 point clouds
    cloud1 = np.random.rand(n, 3) * 100
    cloud2 = np.random.rand(n, 3) * 100
    # cloud1 -> orange
    # cloud2 -> white
    color1 = np.array([[1, 0.4, 0]] * n)
    color2 = np.ones((n, 3))
    
    # stack point clouds and colors
    pos = np.vstack((cloud1, cloud2))
    colors = np.vstack((color1, color2))
    
    
    # plot ! note the parent parameter
    p1 = Scatter3D(parent=view.scene)
    p1.set_gl_state('translucent', blend=True, depth_test=True)
    p1.set_data(pos, face_color=colors, symbol='o', size=10,
                edge_width=0.5, edge_color='blue')
    
    # run
    if __name__ == '__main__':
        if sys.flags.interactive != 1:
            app.run()