Mayavi: Mayavi does not support RGB tuples as point data

Created on 22 Nov 2013  ·  9Comments  ·  Source: enthought/mayavi

In pure vtk it is possible to have surfaces where point_data.scalars is a vtkUnsignedCharArray. The renderer will interpret this as providing a distinct RGB or RGBA value for each vertex and color the surface accordingly if the settings are set up. Mayavi objects do not let you do this at all because they don't ever set up vtkUnsignedCharArrays.

Eventually, it would be nice to have it so that if the user specifies valid RGB or RGBA tuples as scalars to mlab functions, the colors at each point would conform to the scalars.

Most helpful comment

Neither of those answers on their own really did what I wanted, but finally I figured it out by combining them :)

# Imports
import numpy as np
from mayavi.mlab import quiver3d, draw

# Primitives
N = 200 # Number of points
ones = np.ones(N)
scalars = np.arange(N) # Key point: set an integer for each point

# Define color table (including alpha), which must be uint8 and [0,255]
colors = (np.random.random((N, 4))*255).astype(np.uint8)
colors[:,-1] = 255 # No transparency

# Define coordinates and points
x, y, z = colors[:,0], colors[:,1], colors[:,2] # Assign x, y, z values to match color
pts = quiver3d(x, y, z, ones, ones, ones, scalars=scalars, mode='sphere') # Create points
pts.glyph.color_mode = 'color_by_scalar' # Color by scalar

# Set look-up table and redraw
pts.module_manager.scalar_lut_manager.lut.table = colors
draw()

snapshot

All 9 comments

The core devs of Mayavi are quite busy elsewhere. I am afraid that for
this feature request to happen, somebody else is going to have to step in
and code.

Thats quite all right. I intended it as a feature request for the future, I know it is a considerable amount of work. It is something I would try to work on, if I can find time.

Thanks a lot for being so understanding. Quite clearly the workforce on
Mayavi is not enough to meet all its potential.

@GaelVaroquaux there will be some funding on the project soon (starting Q2 next year) !

I have been banging my head against this particular wall for most of today -- specifying a list of points in terms of arrays of [x, y, z, size, color] seems like such an obvious feature! Why would anyone want the size of points to be locked to the color of points?!

Your set of requirements may that be that of others. Mayavi is built upon
VTK, which has been created as a general visualization toolkit to display
rich continuously varying data. It tends to consider things as abstract
quantities that must be mapped to visual properties of objects. Like it
or not, we are pretty much bound to it.

That said, decoupling size and color is easily possible with a bit of a
hack: Last item in the following paragraph
http://docs.enthought.com/mayavi/mayavi/mlab.html#adding-color-or-size-variations

Thanks for the quick reply. Unfortunately, it is a major limitation in terms of Mayavi's ability to plot scientific data (see e.g. this thread, featuring the quote "I also took the fetal position and started weeping after I realise the API doesn't have any clear/default way of custom colours", which summarizes my feeling exactly!). It seems the feature is possible in VTK (e.g., this), although it may not be easy to translate to Mayavi.

(Incidentally, it sounds like you'd be familiar with what I'm trying to do here -- I want to use different colors for each different population of neurons in a 3D model of the cortex.)

Hi Cliff,

There's no nice api for this, but there are some workarounds to do it.

Either by using a tvtk unsigned char array as I initially suggested here

(see
http://stackoverflow.com/questions/19431099/how-to-directly-set-rgb-rgba-colors-in-mayavi,
is complicated for certain mayavi sources)

Or by setting up a large colortable which holds every possible RGB value.

(see answer by user eqzx,
http://stackoverflow.com/questions/18537172/specify-absolute-colour-for-3d-points-in-mayavi?rq=3
)

On Fri, May 15, 2015 at 1:26 PM, Cliff Kerr [email protected]
wrote:

Thanks for the quick reply. Unfortunately, it is a major limitation in
terms of Mayavi's ability to plot scientific data (see e.g. this thread
http://stackoverflow.com/questions/18537172/specify-absolute-colour-for-3d-points-in-mayavi,
featuring the quote "I also took the fetal position and started weeping
after I realise the API doesn't have any clear/default way of custom
colours", which summarizes my feeling exactly!). It seems the feature is
possible in VTK (e.g., this
http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/ColoredPoints),
although it may not be easy to translate to Mayavi.


Reply to this email directly or view it on GitHub
https://github.com/enthought/mayavi/issues/92#issuecomment-102463748.

Neither of those answers on their own really did what I wanted, but finally I figured it out by combining them :)

# Imports
import numpy as np
from mayavi.mlab import quiver3d, draw

# Primitives
N = 200 # Number of points
ones = np.ones(N)
scalars = np.arange(N) # Key point: set an integer for each point

# Define color table (including alpha), which must be uint8 and [0,255]
colors = (np.random.random((N, 4))*255).astype(np.uint8)
colors[:,-1] = 255 # No transparency

# Define coordinates and points
x, y, z = colors[:,0], colors[:,1], colors[:,2] # Assign x, y, z values to match color
pts = quiver3d(x, y, z, ones, ones, ones, scalars=scalars, mode='sphere') # Create points
pts.glyph.color_mode = 'color_by_scalar' # Color by scalar

# Set look-up table and redraw
pts.module_manager.scalar_lut_manager.lut.table = colors
draw()

snapshot

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Make42 picture Make42  ·  7Comments

Kekushke picture Kekushke  ·  9Comments

Mallcock1 picture Mallcock1  ·  10Comments

rambalachandran picture rambalachandran  ·  9Comments

relyativist picture relyativist  ·  16Comments