Upriser
Upriser

Reputation: 439

Color using RGB value in Matplotlib

I have a dataframe which has X, Y, Z, R, G, B values and I want to plot the scatter plot using this information and for the color of each point in scatter plot I need to give values from R, G, B. I tried the following code it is working if I give static color like doing color = 'r'. But when I'm trying to give RGB values as shown in the code below it is generating error.

fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')

for i in range(size):
    ax.plot(pts[:,0], pts[:,1], pts[:,2], '.', color = rgba(pts[i, 3], pts[i, 4], pts[i, 5]), markersize=8, alpha=0.5)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

How can I give the value from the R, G, B columns of the dataframe?

All the values are in 8-bit format.

Upvotes: 2

Views: 19459

Answers (1)

kuzand
kuzand

Reputation: 9806

This should work:

pts = np.array([[1, 2, 3, 0.0, 0.7, 0.0],
                [2, 3, 4, 0.5, 0.0, 0.0]])  # example


fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')

#size = pts.shape[0]
#for i in range(size):
#    ax.plot([pts[i, 0]], [pts[i, 1]], [pts[i, 2]], '.',
#            color=(pts[i, 3], pts[i, 4], pts[i, 5]), markersize=8, #alpha=0.5)

for p in pts:
    ax.plot([p[0]], [p[1]], [p[2]], '.', color=(p[3], p[4], p[5]),  
            markersize=8, alpha=0.5)  

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

You can specify rgb colors by a tuple (r, g, b), where r, g, b are floats in range [0, 1].

Upvotes: 2

Related Questions