Exploring 3D Data Visualization with Mayavi in Python

Mayavi is an interactive 3D plotting library that can be used to visualize complex spatial datasets. Although matplotlib can also be used for simple 3D plotting, Mayavi uses a more complex engine (VTK), making Mayavi more suitable for displaying large and complex data. The Mayavi library is like a magic tool that helps us do lots of cool things in a computer program, like creating 3d plots and graphs. We can use it by adding it to our program in Python.

In this article, we will understand and explore the Mayavi library for 3D visualization.

Table of Content

  • Exploring Mayavi Module
  • Installing Mayavi for 3D Data Visualization
  • Utilizing the Application for 3D Data Visualization
    • 1. 3D Lines Visualization with Mayavi
    • 2. 3D Arbitrary Regular Mesh
    • 3. 3D Elevation Surface Using Mayavi
    • 4. Volumetric Data with Mayavi

Exploring Mayavi Module

In the Mayavi module, we can easily see and understand scientific data with the help of different tools. But MayaVi is special because it has other cool features that make it stand out from other similar modules. Let’s talk about these features now:

  • The Mayavi module is a helpful tool for making graphs and pictures of scientific information. It can also be used with the Mayavi extension for even more features.
  • The Mayavi module can help you open and read different types of files, like PLOT3D and VTK, which is really useful.
  • The Mayavi module can easily be used in any Operating System (OS).
  • Mayavi is like a special tool that lets you look at and change things in a pipeline of objects.
  • With the Mayavi module, we can bring in basic 3D pictures and VRML scenes.

Installing Mayavi for 3D Data Visualization

We can use pip to install the Mayavi library in our notebook. If you are installing the library in your local system, you can follow these steps:

Install PyQt5:

pip install PyQt5

Then, you can install Mayavi by running the following commands in your terminal or command prompt:

pip install mayavi

Alternatively, you can install Mayavi from source by downloading the source code from the Mayavi website and then running:

python setup.py install

This will install Mayavi along with its dependencies.

Running the Application

To check if the installation is complete and to run the application call this command in the terminal (MacOS):

mayavi2

For some other platforms like win34 double click mayavi2.exe or mayavi2.py file in the scripts folder which will be inside PythonXY\mayavi folder.

If the installation is completed without any error, this window will open:

Utilizing the Application for 3D Data Visualization

Now let’s see how to use the application with few examples. Let’s first create a random 3d points using numpy and mlab.points3d() function from mayavi library.

  • In this program, first we will import necessary libraries which are numpy and mlab from mayavi.
  • Then we will assign random numbers to variables named x, y, z, and value using np.random.random() function.
  • Finally, we will use points3d() function and pass the above variables are parameter to generate the 3d plot.
Python
from mayavi import mlab
import numpy as np

x, y, z, value = np.random.random((4, 40))
mlab.points3d(x, y, z, value)

Save this code with .py extension in your system. Now in the mayavi application click on file/Run Python Script, then select the saved python file.

Once you open the python script you will see the output.

Output:

1. 3D Lines Visualization with Mayavi

Now let’s plot a 3d line visualization using plot3d() function. We can provide the thickness of the line and the colour we want as parameter to the function. Here’s how to do it:

  • First we will import the necessary libraries which are numpy and mlab from mayavi.
  • Then we will create a variable t in which we will use numpy’s linspace function to generate evely spaced numbers that are spread out in a certain range.
  • Finally we will use plot3d() function, in which the x coordinate will be np.sin(t), y coordinate will be np.cos(t), and z coordinate will be 0.01*t. Then the last parameter will be ‘t‘ which is used to specify the scalar values associated with each point.
Python
from mayavi import mlab
import numpy as np

t = np.linspace(0, 20, 200)
mlab.plot3d(np.sin(t), np.cos(t), 0.1*t, t)

Output:

Save the above code in a new filename.py file and load into the mayavi2 application in the same way as before.

2. 3D Arbitrary Regular Mesh

Now let’s create a arbitrary regular mesh defined by x, y, z positions of node points. This surface is defined by points connected to form triangles or polygons. We can use mlab.mesh() function to create this plot.

As usual, first we will import the numpy and mayavi libraries.

  • Now we will use mgrid() function to create a mesh grid of spherical coordinates.
  • Then we will do spherical-to-Cartesian coordinate transformations.
  • Finally we will use mesh() function and pass the x, y, z parameters into this function to create the arbitrary regular mesh.
Python
from mayavi import mlab
import numpy as np

phi, theta = np.mgrid[0:np.pi:11j, 0:2*np.pi:11j]
x = np.sin(phi) * np.cos(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(phi)
mlab.mesh(x, y, z)
mlab.mesh(x, y, z, representation='wireframe', color=(0, 0, 0))

Output:

3. 3D Elevation Surface Using Mayavi

An elevation surface is like a bumpy map that shows how high or low things are in a certain area. It helps us see the shape of the land and how steep or flat it is. We use elevation surfaces to study things like mountains, rivers, and other natural features. It helps us see patterns and understand how things are arranged in space.

  • After importing the necessary libraries, we will use mgrid() function to generate a grid of x and y values. This is similar to the plt.clf() function of matplotlib.
  • Then we will calculate distance from the origin to each point and assign it to the variable r.
  • Then we will calculate z values for each point using the distance calculated in the precious step.
  • Finally we will use surf() function and pass the parameter z to create the elevation surface plot.
Python
from mayavi import mlab
import numpy as np

x, y = np.mgrid[-10:10:100j, -10:10:100j]
r = np.sqrt(x**2 + y**2)
z = np.sin(r)/r
mlab.surf(z, warp_scale='auto')

Output:

4. Volumetric Data with Mayavi

Volumetric data refers to a type of data that represents information distributed throughout a three-dimensional space, often within a grid or voxel structure. This data typically represents properties such as density, temperature, pressure, or other scalar or vector quantities within a volumetric region and we can see what’s inside by dividing it into lots of little squares and checking what’s in each square.

  • Just like above examples, we will first import the necessary libraries then we will use mgrid() function to generate spatial coordinates of the points in the grid.
  • Then we will compute the scalar values of each point in grid and assign it to the variable values.
  • Finally we will use contoured() function to create the volumetric plot.
Python
from mayavi import mlab
import numpy as np

x, y, z = np.mgrid[-5:5:64j, -5:5:64j, -5:5:64j]
values = x*x*0.5 + y*y + z*z*2.0
mlab.contour3d(values)

Output:

Conclusion

Mayavi is a really helpful tool that lets people look at and understand complicated data in a 3D way. It’s easy to use and has lots of cool features, making it great for scientists and developers. You can do all sorts of things with it, like making basic 3D graphs or more advanced visualizations. It works with Python and can be used on different types of computers, so lots of people can use it to help them study and analyze data in a more visual way.



Contact Us