Mayavi: Scientific Data Visualization Using Python

Mayavi notebook

This notebook contains python code examples for visualization using mlab module of Mayavi. The code samples are derived from the SciPy workshop conducted by Prof. Prabhu Ramachandran — available online on YouTube

%gui qt
from mayavi import mlab
mlab.test_contour3d()
<mayavi.modules.iso_surface.IsoSurface at 0x2a6b2331528>
mlab.test_contour3d??
import numpy as np
from numpy import *
t = np.linspace(0, 2*pi, 50)
u = cos(t)*pi
x, y, z = sin(u), cos(u), sin(t)
mlab.points3d(x, y, z)
<mayavi.modules.glyph.Glyph at 0x1f513c35b88>

Clearing the figure window

mlab.clf()
mlab.points3d(x, y, z, t, scale_mode='none')
<mayavi.modules.glyph.Glyph at 0x2a6c944dac8>
mlab.plot3d(x, y, z, t)
<mayavi.modules.surface.Surface at 0x1f513638b28>

Parametrize a helix

mlab.clf()

t = np.linspace(0, 10*pi, 1000)
x, y = 0.2*sin(t), 0.2*cos(t)
z = 0.1*t
mlab.plot3d(x, y, z, color=(1, 0, 0))
mlab.points3d(x[-1], y[-1], z[-1], color=(0.2, 0.5, 0.7))
<mayavi.modules.glyph.Glyph at 0x2a6c71a1828>
mlab.clf()
from numpy import mgrid
arange(0, 3, 1)
array([0, 1, 2])
x, y = mgrid[0:3:1, 0:3:1]
linspace(-1, 1, 5)
array([-1. , -0.5,  0. ,  0.5,  1. ])

To get mgrid coordinates with linspace-like syntax

mgrid[-1:1:5j]
array([-1. , -0.5,  0. ,  0.5,  1. ])
x, y = mgrid[-3:3:100j, -3:3:100j]
z = sin(x*x + y*y)
mlab.surf(x, y, z)
<mayavi.modules.surface.Surface at 0x2a6be30f348>
mlab.clf()
mlab.contour_surf(x,y,z)
<mayavi.modules.surface.Surface at 0x2a6c12e4c48>
mlab.clf()
mlab.mesh(x,y,z)
<mayavi.modules.surface.Surface at 0x2a6bfa1f048>

Using the mesh command used for irregular grid points

mlab.clf()

x, y = mgrid[-3:3:100j, -3:3:100j]
z = sin(x*x + y*y)*0.5
mlab.mesh(sin(x), cos(y*y), z)
<mayavi.modules.surface.Surface at 0x2a6be980828>
mlab.clf()

u, v = mgrid[0:2*pi:50j, 0:pi:50j]
x = 0.5*sin(u)*sin(v)
y = 0.5*cos(u)*sin(v)
z = 0.5*cos(v)

mlab.mesh(x, y, z, representation = 'wireframe')
<mayavi.modules.surface.Surface at 0x2a6834f39a8>
mlab.clf()

Triangular mesh

points = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0]])
x, y, z = points.T
t = [[0, 1, 2]]
mlab.triangular_mesh(x, y, z, t)
<mayavi.modules.surface.Surface at 0x2a68ff87d68>
mlab.clf()
points = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0.5, 0.5, 1]])
x, y, z = points.T
t = [[0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4]]
mlab.triangular_mesh(x, y, z, t)
<mayavi.modules.surface.Surface at 0x2a684784ee8>

Image Processing

import numpy as np
s = np.random.random((2**13,2**13))
s.shape
(8192, 8192)
mlab.clf()
mlab.imshow(s)
<mayavi.modules.image_actor.ImageActor at 0x2a684767f48>
x, y, z = mgrid[-5:5:64j, -5:5:64j, -5:5:64j]
mlab.clf()
mlab.contour3d(x*x*0.5 + y*y + z*z*2)
<mayavi.modules.iso_surface.IsoSurface at 0x2a692c27228>
mlab.clf()
mlab.volume_slice(x, y, z, x*x*0.5 + y*y + z*z*2)
<mayavi.modules.image_plane_widget.ImagePlaneWidget at 0x2a6901100a8>

Vector Data

mlab.clf()
mlab.test_quiver3d()
<mayavi.modules.vectors.Vectors at 0x2a69264f3a8>
mlab.clf()
o = mlab.quiver3d(1, 1, 1, 0, 5, 5)
x, y, z = mgrid[-2:3:100j, -2:3:100j, -2:3:100j]
r = sqrt(x**2 + y**2 + z**4)
u = y*sin(r)/(r + 0.001)
v = -x*sin(r)/(r + 0.001)
w = ones_like(z)*0.1
mlab.clf()
obj = mlab.flow(x, y, z, u, v, w, seedtype='plane')

Animate a scene in the GUI

import time
mlab.clf()
x, y = np.mgrid[0:3:1, 0:3:1]
s = mlab.surf(x, y, x*0.1)
for i in range(100):
    s.mlab_source.scalars = x*0.05*(i + 1)
    mlab.process_ui_events() # Used to smoothen the animation on the UI
    time.sleep(1)
mlab.clf()
x, y = np.mgrid[0:3:1, 0:3:1]
s = mlab.surf(x, y, x*0.1, representation='wireframe')
fig = mlab.gcf()
for i in range(5):
    s1 = slice(0, 3, 1.0/(i+2))
    x, y = np.mgrid[s1, s1]
    sc = x*x*0.05*(i+1)
    s.mlab_source.reset(x = x, y = y, scalars=sc)
    fig.scene.reset_zoom()
mlab.savefig('mayavi_output2.png')

Animate decorator

mlab.clf()
@mlab.animate
def anim():
    x, y = np.mgrid[0:3:1, 0:3:1]
    s = mlab.surf(x, y, x*0.1)
    for i in range(25):
        s.mlab_source.set(scalars = x*0.1*(i+1))
        yield
a = anim()
mlab.clf()
mlab.clf()
import time
f = mlab.figure()
f.scene.movie_maker.record = True
@mlab.animate
def anim():
    x, y = np.mgrid[-3:3:100j, -3:3:100j]
    z = np.sin(x*x + y*y)
    s = mlab.surf(x, y, z)
    for i in range(25):
        s.mlab_source.scalars = np.sin((x*x + y*y) + 8*np.pi*i/25)
        mlab.process_ui_events()
        filename = 'Mayavi_figures/mayavi_anim_%d.png' % i
        mlab.savefig(filename)
        time.sleep(0.05)
    yield
a = anim()
mlab.show_pipeline()
<traitsui.ui.UI at 0x2a69265c108>
mlab.clf()
x, y, z = np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j]
o = mlab.contour3d(x*x*0.5 + y*y + z*z*2)
o.visible=False
o.visible=True
p = o.parent
print(p)
<mayavi.core.module_manager.ModuleManager object at 0x000002A69246FF48>
print(p.parent)
p.children
<mayavi.sources.array_source.ArraySource object at 0x000002A684771948>





[<mayavi.modules.iso_surface.IsoSurface at 0x2a684771768>]
mlab.pipeline.outline(o)
<mayavi.modules.outline.Outline at 0x2a6900f8f48>
mlab.pipeline.grid_plane(p)
<mayavi.modules.grid_plane.GridPlane at 0x2a690110e88>
mlab.orientation_axes()
<mayavi.modules.orientation_axes.OrientationAxes at 0x2a69265c1c8>
o.edit_traits()
<traitsui.ui.UI at 0x2a6923027c8>

Make VTK datasets

from scipy import special
# The scalar values
x, y = np.mgrid[-10:10:20j, -10:10:20j]
r = np.sqrt(x**2 + y**2)
z = 5.0*special.j0(r) # Bessel function from SciPy special module

Structured Points: 2D

from tvtk.api import tvtk
spoints = tvtk.StructuredPoints(origin=(-10,10,0), spacing=(1, 1, 1)
                               , dimensions=(20, 20, 1))
# Transpose the array due to VTK's implicit ordering of x followed
# by y followed by z
# ravel to make the number of components 1
spoints.point_data.scalars = z.T.ravel()
spoints.point_data.scalars.name = 'scalar'
# Visualize using Mayavi
mlab.clf()
src = mlab.pipeline.add_dataset(spoints)

warp = mlab.pipeline.warp_scalar(src)
surf = mlab.pipeline.surface(warp)

Published by Abhinav Roy

PhD Candidate (Ryan Fellow) at the Department of Materials Science and Engineering, Northwestern University. Currently working in the domain of Theoretical & Computational Materials Science.

Leave a comment