This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

This page is about the tvtk.tools.mlab module. You are strongly advised to use the more recent mayavi.mlab module which can also be used from ipython -wthread or as a library inside another application.

The mlab.py module allows for simple 3D plotting of objects. It provides an object oriented approch to 3d visualization.

It relies on the simple TVTK module, rather than on the full blown Mayavi application. As a result it has less dependencies. However, it is harder to extend and more limited. The developers are not planning any feature addition to this module, and we strongly advise you to use the Mayavi mlab module, which provides the same usecases, but with more functionalities and is under constant development. For more information, you can read the relevant section of the Mayavi user guide

Table of Contents

A Simple Example

Important: All these examples must be run in "ipython -wthread" or in a Wx application (like pycrust, or the Mayavi2 application). They will not work if you don't use this option.

Start with ipython -wthread and paste the following code::

   1 import scipy
   2 
   3 # prepare some interesting function:
   4 def f(x, y):
   5     return 3.0*scipy.sin(x*y+1e-4)/(x*y+1e-4)
   6 
   7 x = scipy.arange(-7., 7.05, 0.1)
   8 y = scipy.arange(-5., 5.05, 0.1)
   9 
  10 # 3D visualization of f:
  11 from enthought.tvtk.tools import mlab
  12 fig = mlab.figure()
  13 s = mlab.SurfRegular(x, y, f)
  14 fig.add(s)

simple_example.png

Changing axis and colour

   1 from scipy import *
   2 
   3 [x,y]=mgrid[-5:5:0.1,-5:5:0.1]
   4 r=sqrt(x**2+y**2)
   5 z=sin(3*r)/(r)
   6 
   7 from enthought.tvtk.tools import mlab
   8 
   9 # Open a viewer without the object browser:
  10 f=mlab.figure(browser=False)
  11 
  12 s=mlab.Surf(x,y,z,z)
  13 f.add(s)
  14 s.scalar_bar.title='sinc(r)'
  15 s.show_scalar_bar=True
  16 # LUT means "Look-Up Table", it give the mapping between scalar value and color
  17 s.lut_type='blue-red'
  18 # The current figure has two objects, the outline object originaly present, 
  19 # and the surf object that we added.
  20 f.objects[0].axis.z_label='value'
  21 t=mlab.Title()
  22 t.text='Sampling function'
  23 f.add(t)
  24 # Edit the title properties with the GUI:
  25 t.edit_traits()

tvtk.mlab_example.png

List of different functionalities

The implementation provided here is object oriented and each visualization capability is implemented as a class that has traits. So each of these may be configured. Each visualization class derives (ultimately) from MLabBase which is responsible for adding/removing its actors into the render window. The classes all require that the RenderWindow be a pyface.tvtk.scene.Scene instance (this constraint can be relaxed if necessary later on).

This module offers the following broad class of functionality:

To see nice examples of all of these look at the test_* functions at the end of this file. Here is a quick example that uses these test functions:

   1 from enthought.tvtk.tools import mlab
   2 f = mlab.figure()
   3 mlab.test_surf(f) # Create a spherical harmonic.
   4 f.pop() # Remove it.
   5 mlab.test_molecule(f) # Show a caffeine molecule.
   6 f.renwin.reset_zoom() # Scale the view.
   7 f.pop() # Remove this.
   8 mlab.test_lines(f) # Show pretty lines.
   9 f.clear() # Remove all the stuff on screen.

SciPy: Cookbook/MayaVi/mlab (last edited 2015-10-24 17:48:23 by anonymous)