How to use mask for Mayavi's grid plots?

capybaralet

I'm using python and mayavi2 for 3D plotting. I'm plotting a sphere using the mesh command, and now I want to color some of the panels of the sphere a different color. It seems like this is what the mask option is for, but I can't get it to work (I just get the whole sphere recolored).

http://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html#mayavi.mlab.mesh "mask: boolean mask array to suppress some data points"

I use:

mesh(X,Y,Z, color = (1,1,1), opacity=0.5)

to color the whole sphere white, and then

mesh(X,Y,Z, color = (1,0,1), mask = active_region, opacity=0.5) 

to color some parts purple, where

active_region = [[False False False False False False  True]
                 [False False False False False False  True]
                 [False False False False False  True  True]
                 [False False False  True  True  True  True]
                 [False False  True  True  True  True  True]
                 [False False False  True  True  True  True]
                 [False False False False False False  True]]

but this results in a completely purple sphere. X, Y, and Z are all arrays with shape (7,7), just like active_region. What am I doing wrong?

Prabhu Ramachandran

Masking works based on scalars and not when you have set a solid color. Try this instead:

import numpy as np
m = mesh(X,Y,Z, mask=active_region, opacity=0.5)
m.mlab_source = ones_like(X)

The masking works by setting the scalar to Nan where masked. The geometry drawn is the same but the masked regions are not displayed if the scalars are Nan's.

In any case, here is a complete working example for your reference:

import numpy as np
phi, theta = np.mgrid[0:np.pi:100j, 0:2 * np.pi:100j]
x = np.sin(phi) * np.cos(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(phi)
mask = np.zeros_like(x).astype(bool)
mask[::5] = True
mask[:,::5] = True

from mayavi import mlab
mlab.mesh(x, y, z, scalars=z, mask=mask)

Note that you don't need to provide the scalars, its just that if you provide an explicit color there will be no masking. Also the scalars here are simply set to z you can use any compatible array, even a constant value via ones_like(X).

Turns out that if you have a constant scalar VTK 5.6 doesn't seem to mask your points correctly, if you have VTK 5.10 you can do the following:

m = mlab.mesh(x, y, z, scalars=scalars, mask=mask)
m.module_manager.scalar_lut_manager.lut.nan_color = 0, 0, 0, 0

Otherwise, use scalar=z and setup a constant color lookup table. You can create your own by clicking on "colors and legends" on the pipeline editor, then click on "launch lut editor". To use this custom color map, do the following:

scalars = np.ones_like(x)
m = mlab.mesh(x, y, z, scalars=scalars, mask=mask)
m.module_manager.scalar_lut_manager.lut_mode = 'file'
m.module_manager.scalar_lut_manager.file_name = '/path/to/your.lut'

If you do not want to mess with the LUT editor and all that malarky, here is a pure numpy version that you can do right from Python.

m = mlab.mesh(x, y, z, scalars=z, mask=mask)
# Lets make some colors:
#    this is an array of (R, G, B, A) values (each in range 0-255), there should
#    be at least 2 colors in the array.
colors = np.zeros((2, 4), dtype='uint8')
# Set the green value to constant.
colors[:,1] = 255
# the alpha value to fully opaque.
colors[:,3] = 255
# Now use this colormap.
m.module_manager.scalar_lut_manager.lut.table = colors

Note that the colors array should have at least 2 colors and can have as many as you like. For a constant color 2 should suffice.

HTH.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use QLineEdit mask with Russian letters?

From Dev

How to mask phone number input in Kendo UI Grid Column

From Dev

How to subclass CALayer for use as another CALayer mask?

From Dev

How to use angular ui-mask to mask a phone number with optional extension?

From Dev

How to use colormaps to color plots of Pandas DataFrames

From Dev

How to use a logical image as a mask for the original image?

From Dev

how to create a grid of plots in Bokeh w/ some empty cell(s)

From Dev

How to use make multiple plots with ggplot using the same function?

From Dev

How to use mouse to rotate matplotlib 3D plots in wxPython?

From Dev

How and Where to use Bit Mask in java

From Dev

How to use an image as an svg mask

From Dev

matplotlib: How to use marker size / color as an extra dimension in plots?

From Dev

How can I use bootstrap's grid columns to set the width of X-Editable inputs?

From Dev

how to use mask to remove the background in python

From Dev

How to use mask in angular-xeditable?

From Dev

How to use .ico images in plots programmatically with/without conversion

From Dev

Basic 3D voxel grid in Mayavi

From Dev

ggplot: how to add common x and y labels to a grid of plots

From Dev

How to print a's in between this grid

From Dev

Use checkboxGroupInput in Shiny to make selectable plots from one tibble with plot_grid

From Dev

How to use the grid() python

From Dev

make a mask for each well in a grid

From Dev

How to use mask for Mayavi's grid plots?

From Dev

How to use multiple symbols in plots based on different variables in R?

From Dev

How to use UGUI mask and custom font correctly

From Dev

Dynamic grid plots in shiny R

From Dev

How to use the axis command with semilogy bar plots

From Dev

How to animate a BuiltinSurface in Mayavi mlab?

From Dev

ggplot: how to add common x and y labels to a grid of plots

Related Related

  1. 1

    How to use QLineEdit mask with Russian letters?

  2. 2

    How to mask phone number input in Kendo UI Grid Column

  3. 3

    How to subclass CALayer for use as another CALayer mask?

  4. 4

    How to use angular ui-mask to mask a phone number with optional extension?

  5. 5

    How to use colormaps to color plots of Pandas DataFrames

  6. 6

    How to use a logical image as a mask for the original image?

  7. 7

    how to create a grid of plots in Bokeh w/ some empty cell(s)

  8. 8

    How to use make multiple plots with ggplot using the same function?

  9. 9

    How to use mouse to rotate matplotlib 3D plots in wxPython?

  10. 10

    How and Where to use Bit Mask in java

  11. 11

    How to use an image as an svg mask

  12. 12

    matplotlib: How to use marker size / color as an extra dimension in plots?

  13. 13

    How can I use bootstrap's grid columns to set the width of X-Editable inputs?

  14. 14

    how to use mask to remove the background in python

  15. 15

    How to use mask in angular-xeditable?

  16. 16

    How to use .ico images in plots programmatically with/without conversion

  17. 17

    Basic 3D voxel grid in Mayavi

  18. 18

    ggplot: how to add common x and y labels to a grid of plots

  19. 19

    How to print a's in between this grid

  20. 20

    Use checkboxGroupInput in Shiny to make selectable plots from one tibble with plot_grid

  21. 21

    How to use the grid() python

  22. 22

    make a mask for each well in a grid

  23. 23

    How to use mask for Mayavi's grid plots?

  24. 24

    How to use multiple symbols in plots based on different variables in R?

  25. 25

    How to use UGUI mask and custom font correctly

  26. 26

    Dynamic grid plots in shiny R

  27. 27

    How to use the axis command with semilogy bar plots

  28. 28

    How to animate a BuiltinSurface in Mayavi mlab?

  29. 29

    ggplot: how to add common x and y labels to a grid of plots

HotTag

Archive