Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mayavi does not support RGB tuples as point data #92

Closed
aestrivex opened this issue Nov 22, 2013 · 9 comments
Closed

Mayavi does not support RGB tuples as point data #92

aestrivex opened this issue Nov 22, 2013 · 9 comments

Comments

@aestrivex
Copy link
Contributor

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.

@GaelVaroquaux
Copy link
Contributor

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.

@aestrivex
Copy link
Contributor Author

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.

@GaelVaroquaux
Copy link
Contributor

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

@dpinte
Copy link
Member

dpinte commented Nov 26, 2013

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

@cliffckerr
Copy link

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?!

@GaelVaroquaux
Copy link
Contributor

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

@cliffckerr
Copy link

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.)

@aestrivex
Copy link
Contributor Author

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 notifications@github.com
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
#92 (comment).

@cliffckerr
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants