Skip to content

Commit

Permalink
Merge pull request enthought#41 from joonro/patch-1
Browse files Browse the repository at this point in the history
added numpy (as np), mayavi.mlab import statements and changed numpy to ...
  • Loading branch information
GaelVaroquaux committed Jul 12, 2012
2 parents 036f9c8 + 75ec491 commit d86253a
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions docs/source/mayavi/mlab_animating.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

.. _mlab-animating-data:

Animating the data
Expand All @@ -12,11 +13,13 @@ animations. To do this, mlab provides a very convenient way to change
the data of an existing mlab visualization. Consider a very simple
example. The `mlab.test_simple_surf_anim` function has this code::

x, y = numpy.mgrid[0:3:1,0:3:1]
s = mlab.surf(x, y, numpy.asarray(x*0.1, 'd'))
import numpy as np
from mayavi import mlab
x, y = np.mgrid[0:3:1,0:3:1]
s = mlab.surf(x, y, np.asarray(x*0.1, 'd'))

for i in range(10):
s.mlab_source.scalars = numpy.asarray(x*0.1*(i+1), 'd')
s.mlab_source.scalars = np.asarray(x*0.1*(i+1), 'd')

The first two lines define a simple plane and view that. The next three
lines animate that data by changing the scalars producing a plane that
Expand All @@ -36,39 +39,39 @@ below::

# Produce some nice data.
n_mer, n_long = 6, 11
pi = numpy.pi
pi = np.pi
dphi = pi/1000.0
phi = numpy.arange(0.0, 2*pi + 0.5*dphi, dphi, 'd')
phi = np.arange(0.0, 2*pi + 0.5*dphi, dphi, 'd')
mu = phi*n_mer
x = numpy.cos(mu)*(1+numpy.cos(n_long*mu/n_mer)*0.5)
y = numpy.sin(mu)*(1+numpy.cos(n_long*mu/n_mer)*0.5)
z = numpy.sin(n_long*mu/n_mer)*0.5
x = np.cos(mu)*(1+np.cos(n_long*mu/n_mer)*0.5)
y = np.sin(mu)*(1+np.cos(n_long*mu/n_mer)*0.5)
z = np.sin(n_long*mu/n_mer)*0.5

# View it.
l = plot3d(x, y, z, numpy.sin(mu), tube_radius=0.025, colormap='Spectral')
l = plot3d(x, y, z, np.sin(mu), tube_radius=0.025, colormap='Spectral')

# Now animate the data.
ms = l.mlab_source
for i in range(10):
x = numpy.cos(mu)*(1+numpy.cos(n_long*mu/n_mer +
numpy.pi*(i+1)/5.)*0.5)
scalars = numpy.sin(mu + numpy.pi*(i+1)/5)
x = np.cos(mu)*(1+np.cos(n_long*mu/n_mer +
np.pi*(i+1)/5.)*0.5)
scalars = np.sin(mu + np.pi*(i+1)/5)
ms.set(x=x, scalars=scalars)

Notice the use of the `set` method above. With this method, the
visualization is recomputed only once. In this case, the shape of the
new arrays has not changed, only their values have. If the shape of the
array changes then one should use the `reset` method as shown below::

x, y = numpy.mgrid[0:3:1,0:3:1]
s = mlab.surf(x, y, numpy.asarray(x*0.1, 'd'),
x, y = np.mgrid[0:3:1,0:3:1]
s = mlab.surf(x, y, np.asarray(x*0.1, 'd'),
representation='wireframe')
# Animate the data.
fig = mlab.gcf()
ms = s.mlab_source
for i in range(5):
x, y = numpy.mgrid[0:3:1.0/(i+2),0:3:1.0/(i+2)]
sc = numpy.asarray(x*x*0.05*(i+1), 'd')
x, y = np.mgrid[0:3:1.0/(i+2),0:3:1.0/(i+2)]
sc = np.asarray(x*x*0.05*(i+1), 'd')
ms.reset(x=x, y=y, scalars=sc)
fig.scene.reset_zoom()

Expand Down

0 comments on commit d86253a

Please sign in to comment.