GenericJam
GenericJam

Reputation: 3015

Set color per point 3d plot numpy/scipy

I have a set of data points from a kinect in the form of [x,y,z,r,g,b] and I want to plot [x,y,z] setting the point to [r,g,b]. The only thing I've been able to accomplish so far is to change the color per row as plot requires a distribution as far as I can tell.

This is my code so far:

i = 0
for frame in data[0]:
    fig = figure()
    ax2 = fig.gca(projection='3d')
    for col in frame:
        color_value=(median(col[:,3])/255, median(col[:,4])/255, median(col[:,5])/255)
        ax2.scatter(col[:,0],col[:,2]*(-1),col[:,1],color=color_value, alpha=0.25 )
    print(str(i))
    i += 1

    savefig(working_dir + 'images/scatter_point' + str(i) + '.png', bbox_inches='tight')

EDIT: Thanks to HYRY for the suggestion of using scatter. It works, however one would need more RAM than I have for it to work practically in a 640x480 image. For future reference for anyone looking for this type of thing, the operative line is:

    ax2.scatter(frame[i][j][0], frame[i][j][2]*(-1), frame[i][j][1], color = (frame[i][j][3]/255, frame[i][j][4]/255, frame[i][j][5]/255), marker = 's', s=0.25)

marker= 's' means to use a square instead of a dot, s=0.25 sets the dot smaller.

If you want to do a lower resolution image, restrict the for loop to only plot every other or every fourth point.

Upvotes: 1

Views: 853

Answers (1)

HYRY
HYRY

Reputation: 97261

the c argument of scatter can receive a array of shape (N, 3) with values between 0 to 1 which represent color in RGB:

from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca(projection='3d')

x = np.random.sample(20)
y = np.random.sample(20)
z = np.random.sample(20)
c = np.random.rand(20, 3)
s = ax.scatter(x, y, z, c=c)

plt.show()

here is the output:

enter image description here

Upvotes: 1

Related Questions