3-D Graphics

Index

This index gives a list of keywords that are associated with various cells in this Notebook. If you Command-Double-click one of the keywords below,  Mathematica  will find the cell associated with that keyword, select it, and display it in the center of  your window.

ContourGraphics  
DensityGraphics  
Graphics  
Graphics`combining`3D  
Graphics`converting`3D  
Graphics`directives`3D  
Graphics`primitives`3D  
GraphicsArray  
ParametricPlot3D`advanced  
ParametricPlot3D`basic  
ParametricPlot3D`combining  
ParametricPlot3D`options  
Plot3D`basic  
Plot3D`options  
Show    
Show`graphics`primitives  
SurfaceGraphics

Basic 3-D Graphics

Syntax (see pages 134 and 394)

Plot3D

Plot3D[f, {x, xmin, xmax},{y, ymin, ymax }]  makes a three-dimensional plot of f  as a function of the variables x  and y .

ParametricPlot3D

ParametricPlot3D[{fx, fy, fz },{t, tmin, tmax }] produces a parametric plot of a curve in three dimensions.

ParametricPlot3D[{fx, fy, fz },{t, tmin, tmax },            {u, umin, umax }]   produces a parametric plot of a surface in three dimensions.

Three-Dimensional Surface Plot

bp =  Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}];

Parametric Plot of a Curve

ParametricPlot3D[{Sin[t], Cos[t], t/3},
                                    {t, 0, 12}];

Parametric Plot of a Sphere

ParametricPlot3D[
     {Cos[t] Cos[u], Sin[t] Cos[u], Sin[u]},
           {t, 0, 2Pi}, {u, -Pi/2, Pi/2},
                                  Ticks -> None];

Using Plot3D with Options

Syntax (see page 154)

Plot3D[f, {x, xmin, xmax },{y, ymin, ymax }]  makes a three-dimensional plot of f  as a function of the variables x  and y .

Plot3D[f, {x, xmin, xmax },{y, ymin, ymax },             option -> value ]  makes a three-dimensional plot, specifying a particular value for an option.

Show[plot ] redraws a plot.

Show[plot, option ->value ] redraws a plot with options changed.

The following options can be given in Plot or Show. The default value of the option is given.

AmbientLight -> GrayLevel[0.]  gives the ambient illumination level.

AspectRatio ->  Automatic  gives the ratio of height to width.

Axes ->  False   determines whether to draw axes.

AxesEdge  ->  Automatic   determines on which edges to put axes.

AxesLabel  ->  None  gives axes labels.

AxesStyle  ->  Automatic  gives graphics directives to specify the style for axes.

Background ->  Automatic  gives the background color for the plot.

Boxed ->  True   determines whether to draw the bounding box.

BoxRatios  ->  Automatic  gives the bounding 3D box ratios.

BoxStyle ->  Automatic  gives  graphics directives to specify the style for the box.

FaceGrids  ->  None   determines grid lines to draw on the bounding box.

Lighting  -> True   determines whether to use simulated illumination.

PlotLabel  ->  None   gives a label for the plot.

PlotRange ->  Automatic   gives the range of values to include.

PlotRegion  ->  Automatic   gives the final display region to be filled.

RenderAll  ->  True   determines whether to render all polygons.

Shading  ->  True   determines whether to shade polygons.

SphericalRegion  ->  False   determines whether to make the circumscribing sphere fit in the final display area.

Ticks  -> Automatic   gives the tick marks.

ViewCenter ->  Automatic   gives the point to put at the center of final display area.

ViewPoint ->  {1.3, -2.4, 2.}   gives the viewing position.

ViewVertical  ->  {0, 0, 1}   gives the direction to make vertical.

The following option can be given for Plot3D. It cannot be used in Show. The default value of the option is given.

PlotPoints -> 15   gives the  number of sample points in each direction.

For information on additional options, see pages 842 and 803.

Plot with Default Options

Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}];

Axes Labels

Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3},
        AxesLabel -> {"Time", "Depth", "Value"}];

Axes Labels Using Show

Note that this gives the same plot as the last example.

Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}];

Show[ %, 
        AxesLabel -> {"Time", "Depth", "Value"}];

A Note on Using Show

Mathematica  saves information about every plot you produce, so you can later redraw it using Show. When you redraw plots, you can change some of the options you use. You may want to do this, for example, if you are trying to get the best possible setting of options. With a few exceptions, all the options that can be used in Plot3D can also be used in Show.

Basic Plot

bp =  Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}];

Changing the Plot Range

Show[bp, PlotRange -> {-0.5, 0.5}];

Adding Face Grids

Show[bp, FaceGrids -> All];

Specifying the Aspect Ratio

Show[ bp, Axes -> False, AspectRatio -> 0.3];

Specifying the Box Ratios

Show[bp, BoxRatios -> {1, 5, 1}];

Removing the Ticks

Show[bp, Ticks -> None];

Removing the Mesh

Show[bp, Mesh -> False];

Specifying the View Point

Show[bp, ViewPoint -> {0, -2, 0},
               AxesLabel -> {"x", "y", "z"}];

Specifying the Center of the Image

With this setting for ViewCenter, a corner of the box appears in the center of your image.

Show[bp, ViewCenter -> {1, 1, 1},
               AxesLabel -> {"x", "y", "z"}];

Specifying the Vertical in the Image

This setting for ViewVertical makes the x  axis of the box appear vertical in your image.

Show[bp, ViewVertical -> {1, 0, 0},
               AxesLabel -> {"x", "y", "z"}];

A Note on Specifying the View

When you set the options ViewPoint, ViewCenter and ViewVertical, you can think about it as specifying how you look at a physical object. ViewPoint specifies where your head is relative to the object.  ViewCenter specifies where you are looking (the center of your gaze). And ViewVertical specifies which way up your head is.

Switching Off the Simulated Lighting

Lighting -> False  switches off the simulated lighting and instead shades surfaces with gray levels determined by height.

Show[bp, Lighting -> False];

Turning Off All Shading

Show[bp, Shading -> False];

Specifying the Shading

This shows a surface whose height is determined by Sin[xy] but whose shading is determined by GrayLevel[x/3]. Note that this must be done with Plot3D and is not an option that can be used in Show.

Plot3D[{Sin[x y], GrayLevel[x/3]}, 
                         {x, 0, 3}, {y, 0, 3}];

Increasing the Number of Plot Points

You will need to increase the number of plot points to get a good picture of a function that wiggles a lot. Note that this option cannot be used in Show.

Plot3D[
 10 Sin[ x + Sin[y]], {x, -10, 10}, {y, -10, 10},
                          PlotPoints -> 40];

Parametric Plots

Syntax (see page 167)

ParametricPlot3D[{fx, fy, fz }, {t, tmin, tmax }]  produces a parametric plot of a curve in three dimensions.

ParametricPlot3D[{fx, fy, fz },{t, tmin, tmax },                       {u, umin, umax }]  produces a parametric plot of a surface in three dimensions.

ParametricPlot3D[{fx, fy, fz },{t, tmin, tmax },                   option ->value ]  produces a plot specifying a particular value for an option.

Parametric Plot of a Helix

ParametricPlot3D[{Sin[t], Cos[t], t/3},
                                    {t, 0, 15}];

Parametric Plot of a Sphere

sphere = ParametricPlot3D[
     {Cos[t] Cos[u], Sin[t] Cos[u], Sin[u]},
           {t, 0, 2Pi}, {u, -Pi/2, Pi/2},
                                  Ticks -> None];

Parametric Plot of a Torus

torus = ParametricPlot3D[
{Cos[t] (3 + Cos[u]), 
                Sin[t] (3 + Cos[u]), Sin[u]},
{t, 0, 2Pi}, {u, 0, 2Pi}];

Combining Plots

Show[ torus, sphere];

A Note on Using Options with ParametricPlot3D

You can use many of the same options with ParametricPlot3D as you use with Plot3D.  Most of these options can be given in Show. For more information, see the section above on Using Plot3D with Options and pages 803 and 839.

Converting and Combining Graphics

Syntax (see page 162)

Show[ContourGraphics[g ]]  converts to a contour plot.

Show[DensityGraphics[g ]]  converts to a density plot.

Show[SurfaceGraphics[g ]]  converts to a surface plot.

Show[ plot1, plot2, ...] combines several plots

Show[GraphicsArray[{{plot1 }, {plot2 }, ...}]]   draws several plots side by side.

Show[GraphicsArray[{{plot11, plot12,...},  ...}]]    draws a rectangular array of plots.

Show[GraphicsArray[plots, GraphicsSpacing -> {h, v }]]    puts the specified horizonal and vertical spacing between the plots.

Show[Graphics[ {Rectangle[ {x1min, y1min }, {x1max, y1max }, plot1 ], Rectangle[ {x2min, y2min }, {x2max, y2max },       plot2 ], ...}]]     draws the plots in specified rectangles.

Converting a Surface Plot to a Contour Plot

sg = Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}];

cg = Show[ ContourGraphics[ %]];

A Note on Converting Plots

Contour, density and surface plots are three different ways to display essentially the same information about a function. In all cases, you need the value of a function at a grid of points. The Mathematica  functions ContourPlot, DensityPlot and Plot3D all produce Mathematica  graphics objects that include a list of the values of your function on a grid.  As a result, having used any of these functions, Mathematica  can easily take its output and use it to produce another type of graphics.

A Graphics Array

Note that cg and sg were defined in the previous example.

Show[GraphicsArray[{cg, sg}]];

Specifying the Position of Plots

Show[Graphics[ {Rectangle[{0,0}, {1, 1}, cg],
      Rectangle[{0.8, 0.8}, {1.2, 1.4}, sg]} ]];

A Note on Combining Plots

Mathematica  gives you a great deal of flexibility in combining plots or plotting arrays. For more information, see the section on Combining Plots in 2DGraphics, and pages 424 and 449.

Graphics Primitives

Syntax (see page 429)

Graphics

Graphics[primitives ] represents a three-dimensional graphics image.

Show

Show[Graphics[primitives ], options ] displays the three-dimensional graphics with specified options.

Point[{x, y, z }]   gives a  point at position {x , y , z }.

Line[{{x1, y1, z1 }, {x2, y2, z2 }, ... }]    gives a  line through the points {x1 , y1 , z1 }, {x2 , y2 , z2 }, ....

Polygon[{{x1, y1, z1 }, {x2, y2, z2 }, ... }]    gives a filled polygon with specified list of vertices.

Cuboid[{xmin, ymin, zmin }, {xmax, ymax, zmax }]  gives a  cuboid with specified corners .

Text[expr, {x, y, z }] gives text centered at the point {x , y , z }.

A Function for Generating Random Points

We will use this function in the examples below. Every time you evaluate rcoord, it generates a random coordinate in three dimensions.

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

rcoord

Points

This shows a plot of 20 random points. The semicolon after the first line supresses the display of the output line.

pts = Table[Point[rcoord], {20}];

Show[Graphics3D[ pts ] ];

Lines

This gives a plot showing a line through 10 random points in three dimensions.

Show[Graphics3D[ Line[Table[rcoord, {10}] ] ] ];

A Polygon

This draws a single random triangle.

Show[Graphics3D[ 
           Polygon[ {rcoord, rcoord,rcoord} ]  ] ];

Cuboids

This draws 20 random unit cubes in three-dimensional space.

Show[Graphics3D[ 
           Table[ Cuboid[10 rcoord], {10}]  ]  ];

Built-in Polyhedra Primitives

The first line inputs the package which defines lists of primitives for various polyhedra. The second line gives a list of polygons corresponding to a tetrahedron centered at the origin.

<<Graphics`Polyhedra` ;

Tetrahedron[ ]

Displaying a Polyhedron

Show[ Graphics3D[ Dodecahedron[ ] ] ];

Stellating a Polyhedron

stel = Stellate[Icosahedron[ ] ];

Show[Graphics3D[stel]];

Using Options

Note that stel was defined in the previous example.

Show[Graphics3D[stel], Boxed -> False,
                    BoxRatios -> {1, 1, 5}];

A Note on Using Options with Primitives

When you display a three-dimensional graphics object using Show,  you can use many of the same options you use with Plot3D.   For more information, see the section Using Plot3D with Options above, and page 803.

Text

Show[Graphics3D[{stel, 
            Text["Mathematica", {1, 1, 1.5}]}]];

Directives

Syntax (see page 434)

AbsoluteDashing[{d1, d2, ... }] gives an absolute line dashing specification.

AbsolutePointSize[d ] gives an absolute point size specification.

AbsoluteThickness[d ] gives an absolute line thickness specification.

CMYKColor[c, m, y, k ] gives a color specification.

Dashing[{r1, r2, ... }] gives a line dashing specification.

EdgeForm[spec ] gives  a polygon edge specification.

FaceForm[spec ] givesa polygon face specification.

GrayLevel[i ] gives an intensity specification.

Hue[h ] gives a hue specification.

PointSize[r ] gives a point size specification.

RGBColor[r, g, b ] gives a color specification.

SurfaceColor[spec ] gives a  surface properties specification.

Thickness[r ] gives a line thickness specification.

Directives Affecting Only Specified Primitives

Note that the Thickness directive only affects the line in the sublist that contains it.

Show[Graphics3D[
{{Thickness[0.02], Line[{{0, 1, 0}, {1, 1, 1}}]},
 {Line[{{0, 1, 0}, {1, 0, 1}}]}} ] ];

A Note on Graphics Directives.

When you set up a graphics object in Mathematica , you typically give a list of graphical elements. You can include in that list graphics directives which specify how subsequent elements in the list should be specified. In general, the graphical elements in a particular graphics object can be given in a collection of nested lists. When you insert graphics directives in this kind of structure, the rule is that a particular graphics directive affects all subsequent elements of the list it is in, together with all elements of sublists that may occur.  The graphics directive does not, however, have any effect outside the list it is in.

Function for Generating Random Points

We will use this function in the examples below. Every time you evaluate rcoord, it generates a random coordinate in three dimensions.

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

rcoord

Adjusting Point Size

This shows a plot of 20 random points. Each point is displayed as a circle whose radius is 5% of the display area width.

pts = Table[Point[rcoord], {20}];

Show[Graphics3D[ {PointSize[0.05], pts} ] ];

Line Thickness and Dashing

This gives a plot showing a line through 10 random points in three dimensions. The lines are dashed with a thickness of 2 printers points.

lines = Line[Table[rcoord, {10}] ];

Show[Graphics3D[ 
{AbsoluteThickness[2], AbsoluteDashing[{5, 5}],
         lines } ] ];

Specifying the Color of Polygons

The first line inputs the package which defines lists of primitives for various polyhedra. The second line draws an icosahedron, using the same gray level for all faces, and all edges. Note that the option setting Lighting -> False  is necessary to turn off the simulated lighting model.

<<Graphics`Polyhedra` ;

gicos = Show[Graphics3D[
      {GrayLevel[0.7], Icosahedron[ ]}],
                          Lighting -> False];

Using the Illumination Model

Note that gicos was defined in the previous example.

Show[ gicos, Lighting -> True];

A Note on the Color of Polygons

All the graphics directives for two dimensions work the same in three dimensions, except for Polygon objects. In two dimensions, polygons are assumed to have an intrinsic color, specified directly by graphics directives such as RGBColor. In three dimensions, however, Mathematica  also provides the option of coloring polygons using a more physical approach based on simulated illumination. With the default option setting Lighting -> True  for Graphics3D objects, Mathematica  ignores explicit colors specified for polygons, and instead determines all polygon colors using the simulated illumination model. Even in this case, however, explicit colors are used for points and lines.

Adjusting the Edges of Polygons

The first line inputs the package that defines lists of primitives for various polyhedra.

<<Graphics`Polyhedra` ;

Show[Graphics3D[
  {EdgeForm[{GrayLevel[0.5], Thickness[0.02]}],
                     Dodecahedron[ ] } ] ];

Adjusting the Faces of Polygons

The first line inputs the package that defines lists of primitives for various polyhedra. The second line defines a dodecahedron with one face removed by droppingthe sixth polygon from the defining list.. The third line draws the resulting polyhedra with the outside of each face light gray and the back face dark gray.

<<Graphics`Polyhedra` ;

d = Drop[Dodecahedron[], {6}];

Show[Graphics3D[
  {FaceForm[GrayLevel[0.8], GrayLevel[0.3]], d}],
                    Lighting -> False];

Adjusting the Bounding Box

The first line inputs the package that defines lists of primitives for various polyhedra.

<<Graphics`Polyhedra` ;

Show[Graphics3D[Dodecahedron[ ],
  BoxStyle -> Dashing[{0.02, 0.02}],
     Axes -> True, AxesStyle -> Thickness[0.01]]];