user7086216
user7086216

Reputation:

Add Color To 3D Scatter Plot

I have a list of x,y,z points and a list of values assigned to each 3D point. Now the question is, how can I color each point in a 3D scatter plot according to the list of values ?

The colors should be typical engineering -> RGB -> lowest blue to highest red

Thanks a lot

Basically I am searching for an equivalent to: scatter3(X,Y,Z,S,C) See here: https://ch.mathworks.com/help/matlab/ref/scatter3.html

I tried:

col = [i/max(values)*255 for i in values]


ax.scatter(sequence_containing_x_vals, sequence_containing_y_vals, sequence_containing_z_vals,c=col, marker='o')
pyplot.show()

..but I don't get the desired result

Upvotes: 1

Views: 4049

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339052

Note the recommended way of producing scatters with colors is to supply the values directly to c:

ax.scatter(x, y, z, c=values, marker='o', cmap="Spectral")

Minimal example:

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

x = y = z = values = [1,2,3,4,5]

ax = plt.subplot(projection="3d")
sc = ax.scatter(x, y, z, c=values, marker='o', s=100, cmap="Spectral")

plt.colorbar(sc)
plt.show()

enter image description here

Upvotes: 1

Related Questions