Interactively Exploring your Data with Mayavi

The purpose of this code is to demonstrate the interactive data visualization capabilities of the Mayavi module.

In [1]:
import numpy
import matplotlib
import matplotlib.pyplot
import mayavi
import mayavi.mlab

use matplotlib to display a simple function as a heatmap

In [2]:
X, Y = numpy.mgrid[ -5:5:0.1, -5:5:0.1 ]
Z = X*X + Y*Y

matplotlib.pyplot.imshow(Z);

use mayavi to display a simple function as a surface in an interactive browser

In [3]:
X, Y = numpy.mgrid[ -5:5:0.1, -5:5:0.1 ]
Z = X*X + Y*Y

mayavi.mlab.surf(Z)
mayavi.mlab.axes()
mayavi.mlab.outline()
mayavi.mlab.show()

use mayavi to display a more complicated function as a surface in an interactive browser

In [4]:
dphi, dtheta = numpy.pi/250.0, numpy.pi/250.0
[phi,theta] = numpy.mgrid[0:numpy.pi+dphi*1.5:dphi,0:2*numpy.pi+dtheta*1.5:dtheta]
m0 = 4; m1 = 3; m2 = 2; m3 = 3; m4 = 6; m5 = 2; m6 = 6; m7 = 4;
r = numpy.sin(m0*phi)**m1 + numpy.cos(m2*phi)**m3 + numpy.sin(m4*theta)**m5 + numpy.cos(m6*theta)**m7
x = r*numpy.sin(phi)*numpy.cos(theta)
y = r*numpy.cos(phi)
z = r*numpy.sin(phi)*numpy.sin(theta)

mayavi.mlab.mesh(x, y, z)
mayavi.mlab.axes()
mayavi.mlab.outline()
mayavi.mlab.show()