Mayavi2: using from ipython
Recently Prabhu and I have been ironing the library aspect of Mayavi2 (library as opposed to application). One of the usecases we are interested in, is interative use, via for instance ipython, a la pylab.
Most people think of Mayavi as a big and powerful application, maybe a bit clunky to script and to get to interact with other bits of code. With the recent additions you can use mayavi just as you would use matplotlib, to complete matplotlib’s 3D plotting.
3D plotting from ipython
As Mayavi relies on wxPython (technical details, yes I know you don’t care), to use it with ipython, you have to start ipython with iptyhon -wthread. Using ipython from svn you can start ipython with both the -pylab and the -wthread options to use both pylab and mayavi for 2D and 3D plotting (beware of namespace mangling, don’t use from module import *).
The matlab/pylab-like interface to Mayavi is found in enthought.mayavi.tools.mlab (this will most probably change to enthought.mayavi.mlab, or to something else shorter), import this module to have familiar functions. Documentation is a bit missing for now, (you can grab some kind of a embryo at https://svn.enthought.com/enthought/attachment/wiki/MayaVi/mlab.pdf?format=raw ), so I’ll just show an example:
from enthought.mayavi.toosl import mlab as M
from numpy import *
f = lambda x, y: sin(x + y) + sin(2*x - y ) + cos(3*x + 4*y)
x, y = mgrid[-7:7.05:0.1, -5:5.05:0.05]
M.surf(x, y, f)
M.axes()
M.title('Demoing mlab.surf')

Apart from the surf command, the different commands used have equivalents in pylab. surf is inspired from matlab: let us continue pylab’s work here. OK, the keyword arguments are not exactly the same, and not all pylab features are available through the mayavi/mlab interface. But the good news is that the objects created are VTK objects, even though it is a bit hidden by this simplified interface. This means that there is the power of VTK, and that you can always modify the resulting objects to fine tune properties that you cannot (yet) tweak with keyword arguments.
Modifying the plot from the GUI
OK, as you see, we can control Mayavi without all the fuss of the user interface. We get a really simple window that does not get in our way. But is this still Mayavi? This clunky UI was convenient to interact with the visualization. I can pop the pipeline up using M.show_engine(). Once I have the pipeline I can easily double click on any of the items I want to modify, and I get the usual Mayavi dialogs that are so convenient when trying to tweak a scene, for instance to modify the colormap:
This is still work in progress, but mlab is completely useable for real work (I use it whenever I want to make a figure in 3D). Beware that the API is not cast into stone (that’s a good moment to make remarks) and that it might change. Documentation is also lacking. Don’t worry it will come pretty soon, but I also have a thesis to defend :->.
Subscribe
On Twitter
Posted
on
Friday, January 4th, 2008 at 3:38 am under

[...] on people wanting to use Mayavi2, we have been working on decoupling the two. As I showed in a previous post, Mayavi2 can now fully be used without Envisage. But this is in the development version, and some [...]
February 5th, 2008 at 2:37 amHi, I’ve installed mayavi2 in ubuntu hardy but doesn’t work fine.
I’ve created your example in a .py file, but when i put python superficie.py then opens mayavi2 but appears these two messages:
Exception
In /usr/lib/python2.5/site-packages/enthought/mayavi/core/pipeline_base.py:113
AttributeError: ‘DecoratedScene’ object has no attribute ‘add_widgets’ (in add_actors)
and the other one is
Current object is not active, please select an active object
i have no idea because it happens one, only i know that chaco2 works me fine
Have you got an idea because i have got this problem?
Thanks
July 2nd, 2008 at 7:10 pmhi
June 13th, 2009 at 5:48 pmI wanted to know how to use the package in ubuntu mlab
I mayavi2 but I did not recognize mlab
Help
This blog post is terribly old. This is no longer the recommended way of doing this. All you need is to change the import:
from enthought.mayavi import mlab as M
Also, there is an extensive documentation on line, please see:
June 13th, 2009 at 5:54 pmhttp://code.enthought.com/projects/mayavi/docs/development/html/mayavi/mlab.html#simple-scripting-with-mlab
Also, Anthon, make sure that you have a reasonably recent mayavi install. The one in jaunty is good, but if you are using intrepid or older, you will need to use the packages available at:
June 13th, 2009 at 5:55 pmhttps://launchpad.net/~gael-varoquaux/+archive/ppa
I tried this code
but I get this:
biblio@biblio-desktop:~$ ipython -wthread
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
Type “copyright”, “credits” or “license” for more information.
IPython 0.8.1 — An enhanced Interactive Python.
? -> Introduction to IPython’s features.
%magic -> Information about IPython’s ‘magic’ % functions.
help -> Python’s own help system.
object? -> Details about ‘object’. ?object also works, ?? prints more.
In [1]: from enthought.mayavi.tools import mlab as M
Set Envisage to use the workbench UI: True
Set Envisage to use the workbench UI: True
In [2]: from numpy import *
In [3]: f=lambda x,y: sin(x+y)+sin(2*x-y)+cos(3*x+4*y)
In [4]: x,y=mgrid[-7.:7.05:0.1, -5.:5.05:0.05]
In [5]: M.axes()
(python:9639): Gtk-CRITICAL **: gtk_widget_set_colormap: assertion `!GTK_WIDGET_REALIZED (widget)’ failed
Warning: invalid value encountered in divide
Out[5]:
In [6]: M.title(’anton’)
Out[6]:
In [7]: M.show_engine()
—————————————————————————
Traceback (most recent call last)
/home/biblioteca/ in ()
: ‘module’ object has no attribute ’show_engine’
In [8]:
June 13th, 2009 at 6:07 pmKeyboardInterrupt - Press to continue.
KeyboardInterrupt - Press to continue.
Same issue: the ’show_engine’ has been phased out a while ago (I hadn’t realized we had actually suppressed it, but it has been depreciated for more than a year). Use ’show_pipeline()’.
You should trust the documentation much more than this blog post, it is totally obsolete.
June 13th, 2009 at 6:10 pmhi
June 13th, 2009 at 6:11 pmI tried but I read all this packaging. deb
but the page giving me these are in many parts
also assumes that mayavi2 because I already have this in my sinaptic
but I do not agree to import mlab when I run code in python
I don’t really understand completely you last post. I’ll try to reply to what I understand.
To use the PPA, you need to add the relevant line to your sources, as explained on the page. Don’t try to install the different Debian packages by hand, it will be a loss of time, and changes are you will get it wrong. Just add the url to the list of repositories in synpatic, refresh and upgrade mayavi2 using synaptic.
I don’t get the sentence about importing mlab.
June 13th, 2009 at 6:14 pmgrasias
for your answer
I wrote what came out this
In [8]: show_pipeline ()
—————————————————————————
Traceback (most recent call last)
/home/biblioteca/ in ()
: name ’show_pipeline’ is not defined
In [9]:
June 13th, 2009 at 6:14 pmM.show_pipeline(), if you have imported mlab as M.
June 13th, 2009 at 6:15 pmwhat happens is that when I run this code Pyhton:
from pylab import *
from enthought.mayavi import mlab
I get:
Traceback (most recent call last):
File “/home/biblioteca/Escritorio/eje.py”, line 2, in
from enthought.mayavi import mlab
ImportError: cannot import name mlab
help
June 13th, 2009 at 6:20 pmI get:
In [10]: M.show_pipeline ()
—————————————————————————
Traceback (most recent call last)
/home/biblioteca/ in ()
: ‘module’ object has no attribute ’show_pipeline’
June 13th, 2009 at 6:23 pmWhat is your version of mayavi2? I suspect 2.2.1 or something like this. Please upgrade to 3.x. It is really easy: just a question of adding the PPA to your sources in synaptic.
We cannot support too old versions of mayavi2, especially since they are not well-documented compared to the newest ones, so we have to answer questions all the time.
Ubuntu main packages had fallen behind with mayavi2 version, which is a pitty, but it has been fixed: the new version in Jaunty is just fine, and the Ubuntu and Debian packagers are tracking things closely.
June 13th, 2009 at 6:24 pmhi
I am a student of the academic professional school of computer scienceUNMSM
and I am very interested in my projects to make mayavi Undergraduate
also
I have
ubuntu 8.04
mayavi2 2.0.2a1-4ubuntu2 2.0.2a1-4ubuntu2
python 2.5
I can do to correctly use mayavi2
June 13th, 2009 at 6:39 pmI appreciate your help