Dayalan Saravanan


Install Mayavi on Ubuntu 20.04

Wednesday, May 18, 2022

The installation instructions for the Mayavi on Python 3.8.10 [GCC 9.4.0] on Ubuntu 20.04 LTS.

Mayavi is an interactive 3D plotting package. Matplotlib can also do simple 3D plotting, but Mayavi relies on a more powerful engine ( VTK ) and is more suited to displaying large or complex data.

Installing required packages:

On Ubuntu 20.04, we need to install (as sudo) the following system packages:

$ sudo apt-get install gcc g++ vtk7 python3-pyqt5
Installing with pip:

pip is PyPA recommended tool for installing Python packages from PyPI. The latest version of Mayavi available on PyPI can be found here. Required python packages can be automatically fetched and installed with pip.

As of the release version 4.6.0 and above, if you are using Python 3.x and are on a 64 bit machine, installation via pip is the easiest and is as follows:

(venv)$ pip install vtk 
(venv)$ pip install PyQt5
(venv)$ pip install mayavi

If you are interested in the jupyter notebook support as well, do the following (after ensuring that you have jupyter installed of course):

(venv)$ jupyter nbextension install --py mayavi --user 
(venv)$ jupyter nbextension enable --py mayavi --user

You will also need to have ipywidgets and ipyevents installed via pip.

Installing the bleeding edge:

If you want to install the latest version of Mayavi from github, you can simply do the following:

(venv)$ pip install PyQt5 
(venv)$ git clone https://github.com/enthought/mayavi.git
(venv)$ cd mayavi
(venv)$ pip install -r requirements.txt
(venv)$ python setup.py install

Add the jupyter nbextensions using the instructions in the section above and you should be good to go.

Testing installation:

The mayavi.mlab module provides simple plotting functions to apply to numpy arrays, similar to matplotlib plotting interface.


    #!/usr/bin/env python

    """ Script to plot a surface mesh given by x, y, z positions of its node points """

    import numpy as np
    from mayavi import mlab

    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.figure(bgcolor=(1, 1, 1), fgcolor=None)
    mlab.mesh(x, y, z)
    mlab.mesh(x, y, z, representation="wireframe", color=(0, 0, 0))
    mlab.show()
    
Arbitrary regular mesh
Reference: