Convert Figma logo to code with AI

enthought logomayavi

3D visualization of scientific data in Python

1,297
284
1,297
461

Top Related Projects

Image processing in Python

15,979

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!

12,383

Statistical data visualization in Python

matplotlib: plotting with Python

3,289

Main repository for Vispy

2,593

3D plotting and mesh analysis through a streamlined interface for the Visualization Toolkit (VTK)

Quick Overview

Mayavi is a powerful 3D scientific data visualization and plotting library for Python. It provides a comprehensive set of tools for creating interactive 3D visualizations of scientific data, including scalar, vector, and tensor fields. Mayavi is built on top of VTK (Visualization Toolkit) and integrates well with NumPy and other scientific Python libraries.

Pros

  • Rich set of 3D visualization tools and techniques
  • Highly customizable and interactive plots
  • Seamless integration with NumPy and scientific Python ecosystem
  • Supports both scripting and GUI-based interfaces

Cons

  • Steep learning curve for beginners
  • Installation can be complex due to dependencies
  • Documentation could be more comprehensive and up-to-date
  • Performance may be slower compared to some specialized visualization tools

Code Examples

  1. Creating a simple 3D surface plot:
from mayavi import mlab
import numpy as np

x, y = np.ogrid[-2:2:100j, -2:2:100j]
z = x * np.exp(-x**2 - y**2)

mlab.surf(x, y, z)
mlab.show()
  1. Visualizing a vector field:
from mayavi import mlab
import numpy as np

x, y, z = np.mgrid[-2:2:20j, -2:2:20j, -2:2:20j]
u = -y
v = x
w = np.zeros_like(z)

mlab.quiver3d(x, y, z, u, v, w)
mlab.show()
  1. Creating an isosurface plot:
from mayavi import mlab
import numpy as np

x, y, z = np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j]
scalar_field = x*x*0.5 + y*y + z*z*2.0

mlab.contour3d(scalar_field, contours=[1.0], transparent=True)
mlab.show()

Getting Started

To get started with Mayavi, follow these steps:

  1. Install Mayavi and its dependencies:

    pip install mayavi
    
  2. Import the necessary modules:

    from mayavi import mlab
    import numpy as np
    
  3. Create a simple 3D plot:

    x, y = np.ogrid[-2:2:100j, -2:2:100j]
    z = x * np.exp(-x**2 - y**2)
    mlab.surf(x, y, z)
    mlab.show()
    

This will create a basic 3D surface plot. Explore the documentation and examples to learn more about Mayavi's capabilities and advanced features.

Competitor Comparisons

Image processing in Python

Pros of scikit-image

  • Broader focus on general image processing tasks
  • More extensive documentation and tutorials
  • Larger and more active community

Cons of scikit-image

  • Limited 3D visualization capabilities
  • Less specialized for scientific visualization

Code Comparison

Mayavi example:

from mayavi import mlab
import numpy as np

x, y, z = np.ogrid[-10:10:20j, -10:10:20j, -10:10:20j]
s = np.sin(x*y*z)/(x*y*z)
mlab.contour3d(s)
mlab.show()

scikit-image example:

from skimage import data, filters
import matplotlib.pyplot as plt

image = data.camera()
edges = filters.sobel(image)
plt.imshow(edges, cmap='gray')
plt.show()

Mayavi excels in 3D scientific visualization, while scikit-image offers a wide range of image processing tools. Mayavi provides powerful 3D plotting capabilities, whereas scikit-image focuses on 2D image analysis and manipulation. The code examples demonstrate Mayavi's 3D visualization strength and scikit-image's image processing functionality.

15,979

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!

Pros of Plotly

  • Web-based, interactive visualizations that can be easily shared and embedded
  • Supports a wide range of chart types and customization options
  • Extensive documentation and community support

Cons of Plotly

  • Can be slower for large datasets compared to Mayavi
  • Requires internet connection for some features (CDN-hosted libraries)

Code Comparison

Mayavi:

from mayavi import mlab
mlab.figure()
x, y, z = np.ogrid[-10:10:100j, -10:10:100j, -10:10:100j]
s = np.sin(x*y*z)/(x*y*z)
mlab.contour3d(s)
mlab.show()

Plotly:

import plotly.graph_objects as go
import numpy as np

x, y, z = np.mgrid[-10:10:100j, -10:10:100j, -10:10:100j]
values = np.sin(x*y*z)/(x*y*z)
fig = go.Figure(data=go.Isosurface(x=x.flatten(), y=y.flatten(), z=z.flatten(), value=values.flatten()))
fig.show()

Both libraries offer 3D visualization capabilities, but Plotly provides a more modern, web-based approach with interactive features, while Mayavi excels in performance for large scientific datasets and offers more advanced 3D rendering options.

12,383

Statistical data visualization in Python

Pros of Seaborn

  • Simpler API for creating statistical visualizations
  • Better integration with pandas DataFrames
  • More aesthetically pleasing default styles

Cons of Seaborn

  • Limited to 2D visualizations
  • Less customizable for complex plots
  • Slower performance for large datasets

Code Comparison

Seaborn:

import seaborn as sns
import matplotlib.pyplot as plt

sns.scatterplot(x="sepal_length", y="sepal_width", hue="species", data=iris)
plt.show()

Mayavi:

from mayavi import mlab
import numpy as np

x, y, z = np.ogrid[-10:10:100j, -10:10:100j, -10:10:100j]
s = np.sin(x*y*z)/(x*y*z)
mlab.contour3d(s)
mlab.show()

Seaborn excels at creating statistical visualizations with minimal code, while Mayavi is better suited for complex 3D visualizations. Seaborn's code is typically more concise and easier to read, but Mayavi offers more flexibility for advanced 3D plots. The choice between the two depends on the specific visualization needs of the project.

matplotlib: plotting with Python

Pros of Matplotlib

  • Widely adopted and extensively documented
  • Supports a broad range of 2D plotting capabilities
  • Highly customizable with a large ecosystem of extensions

Cons of Matplotlib

  • Limited 3D visualization capabilities
  • Steeper learning curve for complex visualizations
  • Can be slower for large datasets or real-time plotting

Code Comparison

Matplotlib:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()

Mayavi:

from mayavi import mlab
import numpy as np

x, y = np.mgrid[-10:10:100j, -10:10:100j]
z = np.sin(x*y)/(x*y)
mlab.surf(x, y, z)
mlab.show()

Key Differences

  • Matplotlib excels in 2D plotting, while Mayavi specializes in 3D visualization
  • Mayavi offers more advanced 3D rendering capabilities and interactive features
  • Matplotlib has a larger user base and more extensive documentation
  • Mayavi is better suited for scientific visualization and complex 3D data representation
3,289

Main repository for Vispy

Pros of Vispy

  • Modern, GPU-accelerated rendering for high-performance visualization
  • Flexible low-level API allowing fine-grained control over graphics
  • Active development with frequent updates and improvements

Cons of Vispy

  • Steeper learning curve due to lower-level API
  • Less comprehensive documentation compared to Mayavi
  • Fewer built-in 3D plotting functions and widgets

Code Comparison

Mayavi example:

from mayavi import mlab
mlab.figure()
x, y, z = np.ogrid[-10:10:100j, -10:10:100j, -10:10:100j]
s = np.sin(x*y*z)/(x*y*z)
mlab.contour3d(s)
mlab.show()

Vispy example:

from vispy import app, scene
canvas = scene.SceneCanvas(keys='interactive')
view = canvas.central_widget.add_view()
volume = scene.Volume(data, parent=view.scene)
view.camera = 'turntable'
app.run()

Mayavi provides a higher-level interface for 3D plotting, while Vispy offers more low-level control over rendering. Vispy is better suited for custom visualizations and performance-critical applications, whereas Mayavi excels in quick and easy scientific visualization tasks with its more extensive built-in functionality.

2,593

3D plotting and mesh analysis through a streamlined interface for the Visualization Toolkit (VTK)

Pros of PyVista

  • More modern and actively maintained codebase
  • Simpler API and easier to get started with
  • Better documentation and examples

Cons of PyVista

  • Less mature and potentially less stable for complex visualizations
  • Smaller ecosystem and fewer advanced features

Code Comparison

Mayavi:

from mayavi import mlab
mlab.figure()
x, y, z = np.ogrid[-10:10:100j, -10:10:100j, -10:10:100j]
s = np.sin(x*y*z)/(x*y*z)
mlab.contour3d(s)
mlab.show()

PyVista:

import pyvista as pv
import numpy as np
x, y, z = np.meshgrid(np.linspace(-10, 10, 100),
                      np.linspace(-10, 10, 100),
                      np.linspace(-10, 10, 100))
grid = pv.StructuredGrid(x, y, z)
grid["values"] = np.sin(x*y*z)/(x*y*z)
grid.contour().plot()

Both libraries offer 3D visualization capabilities, but PyVista provides a more streamlined API and is generally easier to use for beginners. Mayavi, being older, has a more extensive feature set but can be more complex to work with. PyVista is built on top of VTK, while Mayavi uses both VTK and Traits UI, which can make it more powerful but also more challenging to set up and use.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

====================================================== Mayavi: 3D visualization of scientific data in Python

Mayavi docs: http://docs.enthought.com/mayavi/mayavi/ TVTK docs: http://docs.enthought.com/mayavi/tvtk

.. image:: https://img.shields.io/pypi/v/mayavi.svg :target: https://pypi.org/project/mayavi/ :alt: Package on PyPI

.. image:: https://img.shields.io/badge/License-BSD%203--Clause-blue.svg :target: https://opensource.org/licenses/BSD-3-Clause :alt: BSD 3 Clause

Vision

Mayavi seeks to provide easy and interactive visualization of 3D data. It does this by the following:

- an (optional) rich user interface with dialogs to interact with all data
  and objects in the visualization.

- a simple and clean scripting interface in Python, including one-liners,
  a-la mlab, or object-oriented programming interface.

- harnesses the power of the VTK toolkit without forcing you to learn it.

Additionally Mayavi strives to be a reusable tool that can be embedded in your applications in different ways or combined with the envisage application-building framework to assemble domain-specific tools.

Mayavi is part of the Enthought Tool Suite (ETS).

Features

Mayavi is a general purpose, cross-platform tool for 2-D and 3-D scientific data visualization. Its features include:

* Visualization of scalar, vector and tensor data in 2 and 3 dimensions

* Easy scriptability using Python

* Easy extendability via custom sources, modules, and data filters

* Reading several file formats: VTK (legacy and XML), PLOT3D, etc.

* Saving of visualizations

* Saving rendered visualization in a variety of image formats

* Convenient functionality for rapid scientific plotting via mlab (see mlab
  documentation)

* See the Mayavi Users Guide for more information.

Unlike its predecessor MayaVi1_, Mayavi has been designed with scriptability and extensibility in mind from the ground up. While the mayavi2 application is usable by itself, it may be used as an Envisage plugin which allows it to be embedded in user applications natively. Alternatively, it may be used as a visualization engine for any application.

.. _MayaVi1: http://mayavi.sf.net

Quick start

If you are new to Mayavi it is a good idea to read the online user manual_ which should introduce you to how to install and use it.

If you have installed Mayavi as described in the next section, you should be able to launch the mayavi2 application and also run any of the examples in the examples directory.

.. _online user manual: http://docs.enthought.com/mayavi/mayavi/

Installation

By itself Mayavi is not a difficult package to install but its dependencies are unfortunately rather heavy. However, many of these dependencies are now available as wheels on PyPI. The two critical dependencies are,

  1. VTK_
  2. A GUI toolkit, either PyQt4_, PySide_, PySide2_, PyQt5_ or wxPython_.

The latest VTK wheels are available on all the major platforms (Windows, MacOS, and Linux), but only for 64 bit machines. Python 3.x is fully supported on all these operating systems and Python 2.7.x on MacOS and Linux. If you are out of luck, and your platform is not supported then you will need to install VTK yourself using your particular distribution as discussed in the General Build and Installation instructions <http://docs.enthought.com/mayavi/mayavi/installation.html#installing-ready-made-distributions>_

On Python 3.x you will need to install PyQt5_ and wheels are available for this. On 2.7.x you have more options, and can use PySide_, PyQt4_, and wxPython_. These can be installed from pip or from your package manager.

Currently, Mayavi itself should work with the new wxPython 4.x. However, traitsui_, pyface_, and other ETS packages do not yet support it so the UI will not work correctly. Older versions should work. PyQt/PySide/PySide2 should work largely out of the box.

.. _PyQt5: https://pypi.org/project/PyQt5/ .. _PySide: https://pypi.org/project/PySide .. _PySide2: https://wiki.qt.io/Qt_for_Python .. _PyQt4: https://pypi.org/project/PyQt4/ .. _wxPython: https://pypi.org/project/wxPython/ .. _VTK: https://www.vtk.org .. _traitsui: https://github.com/enthought/traitsui .. _pyface: https://github.com/enthought/pyface

Latest stable release

As of the latest release, i.e. 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::

$ pip install mayavi

$ pip install PyQt5

Thats it!

If you are unable to do this, read the documentation above and find a way to install VTK and a suitable UI toolkit and then repeat the above.

If you are interested in the jupyter notebook support as well, do the following (after ensuring that you have jupyter installed of course. Note, the Jupyter notebook function is only supported starting mayavi version 4.5.0)::

$ jupyter nbextension install --py mayavi --user $ jupyter nbextension enable --py mayavi --user

You will also need to have ipywidgets_ and ipyevents_ installed. These can be installed via pip_ or your favorite package manager.

.. _pip: https://pip.pypa.io/en/stable/ .. _ipywidgets: https://ipywidgets.readthedocs.io .. _ipyevents: https://github.com/mwcraig/ipyevents

Bleeding edge

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

$ git clone https://github.com/enthought/mayavi.git $ cd mayavi $ pip install -r requirements.txt $ pip install PyQt5 # replace this with any supported toolkit $ python setup.py install # or develop

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

Documentation

More documentation is available in the online user manual_ or in docs directory of the sources. This includes a man page for the mayavi2 application, a users guide in HTML and PDF format and documentation for mlab.

More documentation in the form of workshop/tutorial material is available here:

Tutorial Videos

Here are some tutorial videos that you can watch to learn Mayavi:

Examples

Examples are all in the examples directory of the source or the git clone. The docs and examples do not ship with the binary eggs. The examples directory also contains some sample data.

Test suite

The basic test suites for tvtk and mayavi can be run using nose::

nosetests -v tvtk/tests nosetests -v mayavi

The integration tests::

cd integrationtests/mayavi python run.py

Bug tracker, mailing list etc.

The bug tracker is available in github <https://github.com/enthought/mayavi/issues>_ Please provide info and details on platform, python, vtk and gui backends and their versions. If possible, a small example replicating the the problem.

If you have questions you could ask on the Mayavi-users mailing list <https://sourceforge.net/p/mayavi/mailman/mayavi-users/>. This is used by some folks and is not too active. Another mailing list that may be of use is the ETS Users mailing list <https://groups.google.com/forum/#!forum/ets-users>. This is a more general list where a lot of folks experienced with the Enthought Tool Suite are available.

Authors and Contributors

  • Core contributors:

    Prabhu Ramachandran: primary author.

  • Previous contributors:

    Gaël Varoquaux: mlab, icons, many general improvements and maintenance.

    Deepak Surti: Upgrade to VTK 5.10.1, VTK 6.x with new pipeline.

  • Support and code contributions from Enthought Inc.

  • Patches from many people (see the release notes), including K K Rai and R A Ambareesha for tensor support, parametric source and image data.

    Many thanks to all those who have submitted bug reports and suggestions for further enhancements.