# Mathematica 3D Graphics

This document looks at some of the special Mathematica commands suitable for making plots of 3D quantities. You might also want to refer to the documents Mathematic Command Glossary or Mathematic 2D Graphics

## ContourPlot3D

In three dimensions, a contour plot is most often used to display a surface that is too tricky to handle with the Plot3D or ParametricPlot3D commands.

If you can define an equation satisfied by the points on the surface, then you can easily get a plot. We'll use a sphere for our example, although half a sphere is easy to draw with Plot3D, and the whole thing can be handled with no trouble by ParametricPlot3D. We just want an example we understand, for comparison:

```
Needs["Graphics`ContourPlot3D`"]
f[x_,y_,z_] := x^2 + y^2 + z^2 - 4
ContourPlot3D[ f[x,y,z], { x, -2, 2 }, { y, -2, 2 }, { z, -2, 2 } ]
```

## Display

The Display command allows you to save a picture as a file, so that you can use the picture in other programs.

The most common form of the command is

```
Display [ "file.eps", plot, "EPS" ]
```
where file.eps is the name of the file to be created, plot is the name of the plot to be stored in the file, and EPS specifies that you want the file to be an Encapsulated PostScript file.

Note that, if you don't say otherwise, Mathematica will want to put the output file in a subdirectory of the Mathematica folder. This makes the file hard to find, and if this directory happens to be on a locked disk, then the command will fail, without any warning or message. (How thoughtful). For both reasons, it might be wise to specify a full directory path, so the file goes exactly where you want to. On the Macintosh, you must specify an "absolute path name", which starts with the name of the disk, and proceeds through any subdirectory names, down to the file name, with the items separated by colons. On our system, for instance I might type:

```
Display [ "User:myplots:file.eps", plot, "EPS" ]
```

The options for the format of the output file include:

• "EPS", Encapsulated PostScript;
• "GIF", GIF;
• "PBM", Portable Bit Map;
• "PDF", Adobe Acrobat PDF format;
• "PICT", Macintosh picture format;
• "TIFF", tagged image file format;
• "XBitmap", X Windows bitmap;

The graphics file created can sometimes be slightly imperfect. In my case, I asked for EPS format, which should be a text file. Somehow, Mathematica convinced the Macintosh that this was actually a binary file. Thus, when I went to transfer the file to another computer, the line control (carriage returns and line feeds) was all wrong, and the file looked like one long line, which made the file not usable. I had to fix the line control problem before the file would work.

On the Macintosh, there is an option that allows you to highlight a graphic cell, and save it as an EPS file. However, the resulting EPS file is poorly constructed, and will not work with many other PostScript programs. For one thing, it lacks a "showpage" command, but there are more problems than that.

You might also be interested in the ThreeScript command, which gives you access to the original 3D data in the picture.

## Graphics3D

Graphics3D is not actually a command, but it almost looks like one. It is used to tell Mathematica that the corresponding object is a 3D Graphics object. Wrapping a Show command around this structure will cause it to be displayed.

As a simple example, let's define 50 random points and display them:

```
Clear [ p, rcoord ]

rcoord := { Random[], Random[], Random[] };

p = Table [ Point[rcoord], { 50 } ];

Show [ Graphics3D [ p ] ]
```

## Needs

Many useful commands are not in the main part of Mathematica. For instance, there is a command called SpinShow, which you might want to use some time. But because it's not in the main part of Mathematica, if you simply type the command SpinShow, two bad things will happen:

• The correct command SpinShow will not be carried out, because Mathematica doesn't know where to find it;
• Mathematica will create a "ghost" quantity called SpinShow, with no particular properties, which will keep you from ever finding and using the correct SpinShow command.

Anyway, whenever you are using a command that is not part of the main portion of Mathematica, you need to make sure that you give enough information so that that particular command can be found and properly loaded. Mathematica keeps its extra commands in packages, and so before you use a command, you just specify what package you will be needing, using the Needs command. What follows is actually the appropriate command to make the SpinShow command accessible. Be careful to type in the double quotes and the backward quotes exactly as they appear here!

```
Needs["Graphics`Animation`"]
```

The command << can be used instead of Needs. However, every time it is invoked, the command reads in a new copy of the requested package, and does not wipe out the old copies. Reading the same package can cause memory or logic problems for Mathematica.

```
<< Graphics`Animation`
```

## ParametricPlot3D

The ParametricPlot3D command can be used to display plots of space curves defined parametrically, using the format

```
ParametricPlot3D[ { x[t], y[t], z[t] }, { t, tmin, tmax } ]
```

To define a space curve, you need to specify functions that give the x, y and z coordinates of a point that follows the curve, as a function of some parameter, usually named t. For instance, a curve that spins around the surface of a sphere can be defined as follows:

```
Clear [ t, x, y, z ]

x[t_] := Sin[t] * Cos [ 5 * t ]
y[t_] := Sin[t] * Sin [ 5 * t ]
z[t_] := Cos[t]

ParametricPlot3D[ { x[t], y[t], z[t] }, { t, 0, Pi } ]
```
For this particular plot, you might like to use some of the following options:
```
ParametricPlot3D[ { x[t], y[t], z[t] }, { t, 0, Pi }, PlotPoints->200,
Axes->False, Boxed->False, DefaultColor->RGBColor[0,0,1] ]
```

The ParametricPlot3D command can also be used to display plots of space surfaces defined parametrically, using the format

```
ParametricPlot3D[ { x[s,t], y[s,t], z[s,t] }, { s, smin, smax },
{ t, tmin, tmax } ]
```

To define a torus, for instance, we again specify functions that give the x, y and z coordinates of a point on the surface, but now as a function of two parameters, usually named s and t:

```
Clear [ r1, r2, t, x, y, z ]

r1 = 1.0;
r2 = 3.0;

x[t_] := ( r2 + r1 * Cos[s] ) * Cos[t]
y[t_] := ( r2 + r1 * Cos[s] ) * Sin[t]
z[t_] :=        r1 * Sin[s]

ParametricPlot3D[ { x[t], y[t], z[t] }, {s, 0, 2*Pi}, { t, 0, 2*Pi } ]
```

## Plot3D

The Plot3D command can be used to create a plot of a function of two variables, of the form

```
z = f(x,y)
```

Here is an example of the command:

```
Clear [ f, x, y ]
f[x_,y_] := 10 + x * y^2
Plot3D [ f[x,y], { x, 0, 5 }, { y, -2, 2 } ]
```

Some useful options include:

• PlotPoints->20 specifies the number of sample points in each direction, used to construct the plot;
• Mesh->False turns off the mesh lines normally drawn on the surface;
• ViewPoint->{x,y,z} lets you specify the viewpoint;
• Shading->False turns off the coloring of the surface;

## ScatterPlot3D

The ScatterPlot3D command can be used to draw a set of points in 3D, or to draw a broken line in 3D.

As a simple example of the first case, we can compute 10 random points and plot them as follows:

```
Needs["Graphics`Graphics3D`"]
points = Table [ { Random[], Random[], Random[] }, { 10 } ]
ScatterPlot3D [ points ]
```

For an example of a broken line, we suppose we have data describing a path. Here, I'll get it by sampling a function at unevenly spaced points:

```
Needs["Graphics`Graphics3D`"]
tvals = { 0.0, 0.1, 0.3, 0.5, 1.0, 1.3, 1.4, 2.0, 4.0, 4.2, 4.8, 5.0,
6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0 }

points = Table [ { Cos[tvals[[i]], Sin[tvals[[i]], tvals[[i]] },
{ i, 1, 20 } ]

ScatterPlot3D [ points, PlotJoined -> True ]
```

## SpinShow

The SpinShow command helps you to understand the plot of a 3D object by rotating it.

To use the command, you must first generate and name an plot, which has to be of the Graphics3D type. Then you pass that name to the command:

```
Needs["Graphics`Animation`"]
Clear [ plot, t, x, y, z ]

x[t_] := Sin[t] * Cos [ 5 * t ]
y[t_] := Sin[t] * Sin [ 5 * t ]
z[t_] := Cos[t]

plot = ParametricPlot3D[ { x[t], y[t], z[t] }, { t, 0, Pi } ]

SpinShow [ plot ]
```

You may find that the plots spin too rapidly. In that case, you may want to try to set the parameter AnimationDisplayTime to a larger value, like 0.5 seconds. You can also highlight the individual images, and choose Animate Selected Graphics from the menu. Then you can control the rate of spin using the number keys on the numeric keypad (1 = slowest, 9 = fastest).

## SurfaceOfRevolution

The SurfaceOfRevolution command produces a plot of the 3D surface of revolution generated by revolving a given curve z=f(x) about the vertical or z axis.

SurfaceOfRevolution is not a built-in command, so you have to tell Mathematica to read it in. The safest method uses the Needs command. Beware! You must type in the double quotes and backward quotes exactly as they appear here!

```
Needs["Graphics`SurfaceOfRevolution`"]
```

The simplest version of the SurfaceOfRevolution command assumes that we have a function z=f(x) which we wish to rotate about the z axis. If this is all we want, we have only to specify the domain of the function in order to get our plot.

```
Clear[x]

SurfaceOfRevolution[ Sin[x], {x, 0, Pi}]
```

If Mathematica did not draw a plot in response to your command, then you probably have not loaded the SurfaceOfRevolution command properly.

Although we ususally want to rotate the curve through a full revolution, you can control this by specifying a third argument, of the form {theta, theta_min, theta_max }:

```
SurfaceOfRevolution[ Sin[x], {x, 0, Pi}, {t, 0, 2 Pi/3} ]
```

But suppose you want to rotate the sine function about the x axis, getting a cigar shape? This can be accomplished by specifying the value of the option RevolutionAxis. By default, this option has the value {0,0,1}, specifying revolution about the z axis. You never want to rotate about the y axis in this setup. To rotate about the x axis, specify an axis of {1,0,0}.

```
SurfaceOfRevolution[ Sin[x], {x, 0, Pi}, RevolutionAxis->{1,0,0} ]
```

Warnings:

• Plots can take up a lot of memory. If you can't save your notebook to a floppy disk because it's too large, try deleting some of your pictures. As long as you leave the actual Plot commands in your file, you can easily get your pictures back the next time you run Mathematica.
• The online documentation for SurfaceOfRevolution is just horrible, and there isn't any documentation at all for this command in Wolfram's big black book.

## ThreeScript

It's not too hard to make plots of 3D data, or to save a picture of your plot, using the Display command. However, a picture of a 3D object is sometimes not what you want; you might want a description of the 3D object, saying exactly where each point, line and polygon was in the original space. This is the difference between a 3D model and a 2D rendering.

Mathematica has a fairly simple file format called "3-Script" or "ThreeScript" which can be used to store the 3D information in a 3D plot. To use the command, you need to generate a 3D plot, name the output, and pass that output to the appropriate command. For instance:

```
Needs["Graphics`ThreeScript`"]
f[x_,y_] := 10 + x * y^2
picture = Plot3D [ f[x,y], { x, 0, 5 }, { y, -2, 2 } ]
ThreeScript[ "User:myplots:object.ts", picture ]
```
In this example, the file name follows the Macintosh convention, allowing me to include the full path to the file, starting with the disk it is on, and proceeding through any directories, separating items with colons. If you only specify a bare filename, Mathematica will probably put your file into a subdirectory of the Mathematica folder, where you might not think to find it.