Sunday, June 10, 2012

Success! - First Mayavi Plot

Recently, I started Python from a fresh re-install of Python(x, y).  Confident I had everything, including Mayavi, installed correctly.  I tried to re-run some examples I found online.  This time I did not receive any error messages regarding that Mayavi could not be recognized.  However, I was/am still having problems.

I decided to try to open Mayavi from the Start Menu in Windows under the Python(x, y) folder structure.  It worked!  That is, Mayavi opened as a separate console/program.  I found Run Python Script under the File heading.  I tried to run one of my previous files, and this time it really did work!!

This is the spherical harmonic example using the mesh() function (although the example is a little different, maybe I got it from somewhere else; can't remember) from the Mayavi website:
http://docs.enthought.com/mayavi/mayavi/auto/examples.html
http://docs.enthought.com/mayavi/mayavi/index.html
http://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html#
http://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html#mesh

Edit:  I found the example, it is the provided demo - http://docs.enthought.com/mayavi/mayavi/mlab.html#simple-scripting-with-mlab

from numpy import *
from enthought.mayavi import mlab
# Create the data.
dphi, dtheta = pi/250.0, pi/250.0
[phi,theta] = mgrid[0:pi+dphi*1.5:dphi,0:2*pi+dtheta*1.5:dtheta]
m0 = 4; m1 = 3; m2 = 2; m3 = 3; m4 = 6; m5 = 2; m6 = 6; m7 = 4;
r = sin(m0*phi)**m1 + cos(m2*phi)**m3 + sin(m4*theta)**m5 + cos(m6*theta)**m7
x = r*sin(phi)*cos(theta)
y = r*cos(phi)
z = r*sin(phi)*sin(theta);
# View it.
f = mlab.figure()
s = mlab.mesh(x, y, z)

My screenshots:




However, I could not get the flow() function example to work, :(, which I would like to use for 3D streamline plots.

import numpy
import enthought
from mayavi.mlab import *

def test_flow():
    x, y, z = numpy.mgrid[0:5, 0:5, 0:5]
    r = numpy.sqrt(x**2 + y**2 + z**4)
    u = y*numpy.sin(r)/r
    v = -x*numpy.sin(r)/r
    w = numpy.zeros_like(z)
    obj = flow(u, v, w)
    return obj

Edit: I got the test flow to work.  Just remove the def and return and unindent the rest.  (However, I still have very little grasp on how MayaVi works as I am having great difficulty in trying to produce streamlines from a velocity vector field.  Concept seems simple enough but...  I also still cannot get MayaVi to work from Spyder.  Very frustrating!!!!)

import numpy
import enthought
from mayavi.mlab import *

x, y, z = numpy.mgrid[0:5, 0:5, 0:5]
r = numpy.sqrt(x**2 + y**2 + z**4)
u = y*numpy.sin(r)/r
v = -x*numpy.sin(r)/r
w = numpy.zeros_like(z)
obj = flow(u, v, w)

I did get another 3D plot example I found to work which also uses the mesh() function.

http://www.sam.math.ethz.ch/~hheumann/Tutorial/html/mayavi2_tips.html

from numpy import mgrid, real, conj, ones, zeros
from numpy.fft.fftpack import fft2
from numpy.fft.helper import fftshift
from enthought.mayavi import mlab

# A mesh grid
X,Y = mgrid[-100:100, -100:100]

# The initial function: a 2D unit step
Z = zeros((200,200))
Z[0:6,0:6] = 0.3*ones((6,6))

# The fourier transform: a 2D sinc(x,y)
W = fftshift(fft2(Z))
W = real(conj(W)*W)

# Display the data with mayavi
# Plot the original function
#mlab.mesh(X, Y, Z)
# Plot the fourier transformed function
mlab.mesh(X, Y, W)
mlab.savefig("mayavi_fft_plot.png")


No comments:

Post a Comment