Matplotlib 3D scatter plot with color gradient

后端 未结 2 988
你的背包
你的背包 2020-12-15 05:22

How can I create a 3D plot with a color gradient for the points? See the example below, which works for a 2D scatter plot.

Edit (thanks to Chris): What I\'m expectin

2条回答
  •  伪装坚强ぢ
    2020-12-15 05:43

    Following works: I can't figure out why yours doesn't. You should be able to set color as a sequence of RGBA floats, or just sequence of floats.

    # Create Map
    cm = plt.get_cmap("RdYlGn")
    
    x = np.random.rand(30)
    y = np.random.rand(30)
    z = np.random.rand(30)
    col = np.arange(30)
    
    # 2D Plot
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(x, y, s=10, c=col, marker='o')  
    
    # 3D Plot
    fig = plt.figure()
    ax3D = fig.add_subplot(111, projection='3d')
    p3d = ax3D.scatter(x, y, z, s=30, c=col, marker='o')                                                                                
    
    plt.show()
    

    However, in help of scatter, I see the following, it may be related.

        A :class:`matplotlib.colors.Colormap` instance or registered
        name. If *None*, defaults to rc ``image.cmap``. *cmap* is
        only used if *c* is an array of floats.
    

提交回复
热议问题