# Using Mayavi’s contour3d function

This post is from an earlier year, meaning the information here is likely outdated. You should look for a newer post from the current year to get the newest exercises and notes.

The Mayavi library that we’re using in this years course holds a great amount of functions to plot 3D and 2D data. Among these is the contour3d function that is very useful to plot equipotential surfaces in 3D space. This could for instance be used to plot a potential. In the exercise set from week 3, you may use this to visualize the potential inside and outside a uniformly charged sphere.

The solution program for this exercise is shown below:

from numpy import *
from enthought.mayavi.mlab import *

x,y,z = mgrid[-100:101:5., -100:101:5., -100:101:5.]
R = 40
Q = 1.0

r = sqrt(x**2 + y**2 + z**2)
V = 0*x
for i in range(len(r)):
for j in range(len(r)):
for k in range(len(r)):
if r[i][j][k] < R:
V[i][j][k] = Q / (8 * pi * R) * (3 - r[i][j][k]**2/R**2)
else:
V[i][j][k] = Q / (4 * pi * r[i][j][k])

contour3d(x, y, z, V, contours=20, opacity=0.5)

Note that we are setting contours=20 to have 20 equipotential surfaces and opacity=0.5 to be able to see through the surfaces. Otherwise we would only be able to see the outermost surface. You can read more about these and other settings by typing “help contour3d” in IPython (after importing mayavi) or by looking at the online reference.

The result is shown in the figure below:

A close up of the potential from a uniformly charged sphere. The rings are spherical shells coloured according to the level of the potential. They are semi-transparent, so that you can see the inner shells (the orange is the innermost shell.)

Do you see how the potential rises and lowers quickly close to the edge of the sphere? The edge of the sphere is located approximately where the color goes from yellow-ish to green. What does this tell you about the field in this area?