Matplotlib 3D scatter plot with color gradient

后端 未结 2 996
你的背包
你的背包 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:52

    Here is an example for 3d scatter with gradient colors:

    import matplotlib.cm as cmx
    from mpl_toolkits.mplot3d import Axes3D
    def scatter3d(x,y,z, cs, colorsMap='jet'):
        cm = plt.get_cmap(colorsMap)
        cNorm = matplotlib.colors.Normalize(vmin=min(cs), vmax=max(cs))
        scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm)
        fig = plt.figure()
        ax = Axes3D(fig)
        ax.scatter(x, y, z, c=scalarMap.to_rgba(cs))
        scalarMap.set_array(cs)
        fig.colorbar(scalarMap)
        plt.show()
    

    Of course, you can choose the scale to range between different values, like 0 and 1.

提交回复
热议问题