Fundamentals 8 min read

Using Mayavi for 3D Visualization in Python: Installation, Core Concepts, and Example Code

This tutorial walks through installing the Mayavi library, explains its core mlab module and API, and provides a series of Python code examples—including mesh, points3d, plot3d, imshow, surf, contour3d, and quiver3d—to demonstrate how to create various 3‑D visualizations.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Using Mayavi for 3D Visualization in Python: Installation, Core Concepts, and Example Code

This tutorial introduces the Mayavi library for 3D scientific visualization in Python, beginning with installation commands using pip install mayavi -i https://pypi.tuna.tsinghua.edu.cn/simple and a fallback Git installation pip install git+https://github.com/enthought/mayavi.git .

It then describes the basic elements of Mayavi, focusing on the mlab module for handling and displaying graphics, and shows the overall API layout via illustrative screenshots.

Quick drawing examples are presented to demonstrate common use cases.

Example 1 – Simple mesh creates a mesh from predefined x , y , and z arrays and displays it.

<code># coding=utf-8
from mayavi import mlab

x = [[-1,1,1,-1,-1],[-1,1,1,-1,-1]]
y = [[-1,-1,-1,-1,-1],[1,1,1,1,1]]
z = [[1,1,-1,-1,1],[1,1,-1,-1,1]]

s = mlab.mesh(x, y, z)
mlab.show()
</code>

A variant shows a wireframe representation by adding representation='wireframe' and line_width=1.0 to the mlab.mesh call.

<code>from mayavi import mlab

x = [[-1,1,1,-1,-1],[-1,1,1,-1,-1]]
y = [[-1,-1,-1,-1,-1],[1,1,1,1,1]]
z = [[1,1,-1,-1,1],[1,1,-1,-1,1]]

s = mlab.mesh(x, y, z, representation='wireframe', line_width=1.0)
mlab.show()
</code>

Example 2 – Parametric surface builds a 3‑D surface using NumPy trigonometric functions and visualizes it with mlab.mesh .

<code>from numpy import pi, sin, cos, mgrid
from mayavi import mlab

dphi, dtheta = pi/250.0, pi/250.0
[phi, theta] = mgrid[0:pi+dphi*1.5:dphi, 0:2*pi+dtheta*1.5:dtheta]

m0, m1, m2, m3, m4, m5, m6, m7 = 4,3,2,3,6,2,6,4
r = sin(m0*phi)**m1 + cos(m2*phi)**m3 + sin(m4*theta)**m5 + cos(m6*theta)**m7
x = r*sin(phi)*cos(theta)
y = r*cos(phi)
z = r*sin(phi)*sin(theta)

s = mlab.mesh(x, y, z)
mlab.show()
</code>

Example 3 – Points3d visualizes a parametric curve with scalar values controlling color.

<code>import numpy as np
from mayavi import mlab

def test_points3d():
    t = np.linspace(0, 4*np.pi, 20)
    x = np.sin(2*t)
    y = np.cos(t)
    z = np.cos(2*t)
    s = 2 + np.sin(t)
    return mlab.points3d(x, y, z, s, colormap="Reds", scale_factor=.25)

test_points3d()
mlab.show()
</code>

Example 4 – Plot3d (tube) draws a 3‑D tube along a parametric curve.

<code>from mayavi import mlab
import numpy as np

n_mer, n_long = 6, 11
dphi = np.pi / 1000.0
phi = np.arange(0.0, 2*np.pi + 0.5*dphi, dphi)
mu = phi * n_mer
x = np.cos(mu) + (1+np.cos(n_long*mu/n_mer)*0.5)
y = np.sin(mu) + (1+np.cos(n_long*mu/n_mer)*0.5)
z = np.sin(n_long*mu/n_mer) * 0.5

l = mlab.plot3d(x, y, z, np.sin(mu), tube_radius=0.025, colormap='Spectral')
mlab.show()
</code>

Example 5 – Imshow displays a random 2‑D array as an image.

<code>from mayavi import mlab
import numpy as np

s = np.random.random((10, 10))
img = mlab.imshow(s, colormap='gist_earth')
mlab.show()
</code>

Example 6 – Surf and Contour Surf renders a surface defined by a function f(x, y) = sin(x‑y) + cos(x+y) using mlab.surf and mlab.contour_surf .

<code>from mayavi import mlab
import numpy as np

def f(x, y):
    return np.sin(x-y) + np.cos(x+y)

x, y = np.mgrid[-7.:7.05:0.1, -5.:5.05:0.05]
mlab.surf(x, y, f)
mlab.show()
</code>
<code>from mayavi import mlab
import numpy as np

def f(x, y):
    return np.sin(x-y) + np.cos(x+y)

x, y = np.mgrid[-7.:7.05:0.1, -5.:5.05:0.05]
mlab.contour_surf(x, y, f)
mlab.show()
</code>

Example 7 – Contour3d visualizes volumetric scalar data as iso‑surfaces.

<code>from mayavi import mlab
import numpy as np

x, y, z = np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j]
scalars = x*x + y*y + z*z
obj = mlab.contour3d(scalars, contours=8, transparent=True)
mlab.show()
</code>

Example 8 – Quiver3d draws a 3‑D vector field.

<code>import numpy as np
from mayavi import mlab

x, y, z = np.mgrid[-2:3, -2:3, -2:3]
r = np.sqrt(x**2 + y**2 + z**4)
u = y * np.sin(r) / (r + 0.001)
v = -x * np.sin(r) / (r + 0.001)
w = np.zeros_like(z)
obj = mlab.quiver3d(x, y, z, u, v, w, line_width=3, scale_factor=1)
mlab.show()
</code>

Each code snippet is accompanied by screenshots (omitted here) that illustrate the resulting visualizations.

For further details and the original Chinese article, refer to the source link provided at the end of the tutorial.

pythonData Visualization3D visualizationscientific-computingMayavimlab
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.