pythonmatplotlib

A right way to represent 4 dimension points using colors in a matplotlib scatter


I'm currently trying to represent a set of 4 dimensional points in a 3D space using a matplotlib scatter. For do that, I represent the 4th dimension as the color of the 3D point.

According to that, I want to print colored points. Therefore, the color of this points depends on the 4th component of the point.

I want to use the spectral color map. I've already succeeded but using a greyscale, and this repesentation is not enought for me.

I really need to use the spectral color map. So this following code was my last try before ask here:

inicioVertices=5
finalVertices=10
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
datos={}
for vertice in range(inicioVertices,finalVertices):
    print(vertice)
    for arista in range(vertice, vertice*(vertice-1)/2):
       for k in range(vertice//4,vertice+1):
           media=0.0
           for n in range(10):
               g=nx.dense_gnm_random_graph(vertice,arista)
               inicio=time.time()
               recubrimientoVertices(g,k)
               diferencia=time.time()-inicio
           media+=diferencia
           aux=media
           media=aux/10

           datos[(vertice,arista,k)]=media
           mMin=0.00054
           mMax=0.067

           normalizada=(media-mMin)/(mMax-mMin)
           cmap = cm.ScalarMappable( cmap = plt.get_cmap('spectral'))

           print(media)
           ax.scatter(vertice, arista, k, c= cmap.to_rgba(normalizada), marker='o',s=40)
print("max"+str(max(datos.values())))
print("min"+str(min(datos.values())))
ax.set_xlabel('Vertices')
ax.set_ylabel('Aristas')
ax.set_zlabel('K')
plt.show()

media is the 4th component value and normalizada is the normalized value for this component so normalizada always be a number in this interval [0,1]. This is the representation returned by the previous code:

enter image description here

As you can see, all dots are printed in black. I hope someone can help me with this, thank you.


Solution

  • Just an example to plot a 3D scatter plot and using an user defined colour map.

    import matplotlib.cm as cmx
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    import matplotlib
    import numpy as np
    
    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,label='Test')
        plt.show()
    
    x = np.random.uniform(0,1,50)
    y = np.random.uniform(0,1,50)
    z = np.random.uniform(0,1,50)
    

    When I call scatter3d(x,y,z,x+y) for example, I get the following with x+y being my colormap:

    enter image description here