I am using the MayaVi Python library to plot 3d points, using the points3d
class. The documentation specifies that the colour of each point is specified through a fourth argument, s
:
In addition, you can pass a fourth array s of the same shape as x, y, and z giving an associated scalar value for each point, or a function f(x, y, z) returning the scalar value. This scalar value can be used to modulate the color and the size of the points.
This specifies a scalar value for each point, which maps the point to a colourmap, such as copper
, jet
or hsv
. E.g. from their documentation:
import numpy
from mayavi.mlab import *
def test_points3d():
t = numpy.linspace(0, 4*numpy.pi, 20)
cos = numpy.cos
sin = numpy.sin
x = sin(2*t)
y = cos(t)
z = cos(2*t)
s = 2+sin(t)
return points3d(x, y, z, s, colormap="copper", scale_factor=.25)
Gives:
Instead, I would like to specify the actual value for each point as an (r, g, b) tuple. Is this possible in MayaVi? I have tried replacing the s
with an array of tuples, but an error is thrown.
You can use a rgb look up table and map your rgb values to it using whatever logic you want. Here's a simple example:
I have no idea what color scheme you want, but you can evaluate the positions of x,y,z and return whatever scalar corresponds to the rgb value you are seeking.
I've found a better way to set the colors directly.
You can create your own direct LUT pretty easily. Let's say we want 256**3 granularity:
This can now simply be done with the
color
argumentAfter struggling with this for most of today, I found a relatively simple way to do exactly what the question asks -- specify an RGB tuple for each point. The trick is just to define a color map with exactly the same number of entries as there are points to plot, and then set the argument to be a list of indices: