Python 3d scatterplot colormap issue

前端 未结 1 1941
余生分开走
余生分开走 2021-01-19 06:00

I have four dimensional data (x, y, z displacements; and respective voltages) which I wish to plot in a 3d scatterplot in python. I\'ve gotten the 3d plot to render, but I w

1条回答
  •  鱼传尺愫
    2021-01-19 06:08

    ax.scatter can take a color parameter c which is a sequence (e.g. a list or an array) of scalars, and a cmap parameter to specify a color map. So to make the colors vary according to the magnitude of the voltages, you could define:

    c = np.abs(v)
    

    This makes positive and negative voltages have the same color. If instead you wished each color (positive or negative) to have its own color, you could just use c = v.


    For example,

    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    import numpy as np
    x, y, z, v = (np.random.random((4,100))-0.5)*15
    c = np.abs(v)
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    cmhot = plt.get_cmap("hot")
    cax = ax.scatter(x, y, z, v, s=50, c=c, cmap=cmhot)
    
    plt.show()
    

    enter image description here

    0 讨论(0)
提交回复
热议问题