Black Lives Matter. Please consider donating to Black Girls Code today.

Specifying a color for each point in a 3d scatter plot

Hi - I’m attempting to make a 3d scatter plot with one data set in which I specify the color for each point. To be clear, I don’t want a color function, rather I’d like to explicitly specify a color (via RGB value or similar) for each data point.

I’m a beginner with Plotly, but I’ve got the basics down of using Scatted3D from the tutorial examples. What I can’t figure out is how to do the explicit colors.

Thanks

Hi @west999,
A scatter3d trace with a given color for each point is defined as follows:

trace=dict(type='scatter3d',
           x=np.random.rand(25),
           y=np.random.rand(25),
           z=np.random.rand(25),
           mode='markers',
           marker=dict(color=[f'rgb({np.random.randint(0,256)}, {np.random.randint(0,256)}, {np.random.randint(0,256)})' for _ in range(25)],
                       size=10)
          )

i.e. the color value is a list of color codes of len equal to the number of points. Here I defined 25 random rgb color codes, via f-strings in Python 3.6+.