Three-Dimensional Mathematica Graphics (Section 2.3)

Extract from Mathematica Graphics: An Intensive Tutorial

by Tom Wickham-Jones

This HTML document is based on Mathematica Graphics: An Intensive Tutorial by Tom Wickham-Jones. It was adapted by Martin Kraus for non-commercial use.

Mathematica and MathLink are registered trademarks, and MathReader, MathSource and 3-Script are trademarks of Wolfram Research, Inc.

All other product names mentioned are trademarks of their producers.

Copyright 1992 by Wolfram Research, Inc.

All rights reserved. No part of this document may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior written permission of the copyright holder.

2.3 Graphics Packages

Mathematica contains a powerful programming language. Programs can be written in top-level Mathematica code which operate on graphics primitives and produce some particular type of graphic. There are many types of three-dimensional plots which are desirable, such as contour plots, bar charts or vector fields. Mathematica comes with code to produce these. This code is stored in what we call a Mathematica package.

The Mathematica packages are well described in the Mathematica 3.0 on-line help and in the book Mathematica 3.0 Standard Add-on Packages by Wolfram Research, Inc. These describe each of the packages and give examples of its functions. In this section I shall give a brief look at a few of them. One important thing to remember about these packages is that for the functions to be available to Mathematica the package must be loaded.

This loads the package which defines polyhedra. In[36]:= <<Graphics`Polyhedra`

By using this notation, with a back quote `, Mathematica ensures that the same command will load the package on every computer system on which Mathematica runs, even when the file naming conventions for those systems are different.

These are the packages which have been loaded. In[37]:= $Packages

Out[37]= {"Utilities`FilterOptions`", "Geometry`Polytopes`", "Graphics`Polyhedra`", "Global`", "System`"}

2.3.1 More Data Plots

In the previous section the command ListPlot3D was suggested as a method to plot data. However, you may wish to plot data points which are not regularly spaced over a grid. This is done by the function ListSurfacePlot3D which corresponds to ParametricPlot3D with two parameters. ListSurfacePlot3D is defined in the package Graphics`Graphics3D`.

This will load the package. In[38]:= <<Graphics`Graphics3D`
This is an array of three-dimensional data points. In[39]:= data = Table[{r Cos[phi], r Sin[phi], Exp[-r^2]},
{r, 0, 2, 0.4}, {phi, 0, 2 Pi, 2 Pi/10}];

This plots the surface. In[40]:= ListSurfacePlot3D[data]
Out[40]= - Graphics3D -

Plotting a list instead of an array of points is done with the function ScatterPlot3D which corresponds to ParametricPlot3D with one parameter. ScatterPlot3D is also defined in the package Graphics`Graphics3D`, thus we do not need to load the package again.

This generates a list of data points. In[41]:= data = Table[{Cos[t], Sin[t], Cos[2 t]}, {t, 0, 2 Pi, 0.2}];
This produces a scatter plot. In[42]:= ScatterPlot3D[data]
Out[42]= - Graphics3D -
ScatterPlot3D has some additional options like PlotJoined and PlotStyle. In[43]:= ScatterPlot3D[data, PlotJoined -> True, PlotStyle -> Thickness[0.04]]
Out[43]= - Graphics3D -

The package Graphics`Graphics3D` also defines an alternative to ListPlot3D, namely BarChart3D.

This is an array of random numbers. In[44]:= data = Table[Random[], {i, 4}, {j,4}];
This plots a bar chart in three dimensions. In[45]:= BarChart3D[data]
Out[45]= - Graphics3D -

Sometimes a combination of a three-dimensional plot and a density plot is useful. This can be done with ListShadowPlot3D for data and ShadowPlot3D for function plotting. Both commands are defined in the package Graphics`Graphics3D`.

This produces a three-dimensional surface and its flat shadow. The option PlotPoints controls the number of sampling points. In[46]:= ShadowPlot3D[-Cos[x^2] + Sin[y^2], {x, -2, 2}, {y, -2, 2}, PlotPoints -> 8]
Out[46]= - Graphics3D -

In the package Graphics`Graphics3D` facilities are given to change the position of the shadow and the mesh style which is used. The Mathematica 3.0 on-line help and the Wolfram Research book Mathematica 3.0 Standard Add-on Packages describe this and all other Mathematica packages in detail.

2.3.2 ContourPlot3D

So far we have plotted lines and surfaces which were either parametrized by one or two variables or given by some data points. On the other hand ContourPlot3D plots the surface showing a particular value (by default 0) of a function of x, y and z. CountourPlot3D and the corresponding data plotting command ListCountourPlot3D are defined in the package Graphics`CountourPlot3D`.

First the package is loaded. In[47]:= <<Graphics`ContourPlot3D`
This plots the surface defined by the equation x^2 + y^2 + y^3 / 2 - z^2 - 1/3 == 0, i. e. the surface where the left-hand side of the equation is 0. In[48]:= ContourPlot3D[x^2 + y^2 + y^3 / 2 - z^2 - 1/3, {x, -1, 1}, {y, -2, 1}, {z, -1, 1}, PlotPoints -> {4,3}, Boxed -> False]
Out[48]= - Graphics3D -

2.3.3 PlotVectorField3D

PlotVectorField3D (together with PlotGradientField3D and ListPlotVectorField3D) is defined in the package Graphics`PlotField3D`.

This loads the package. In[49]:= <<Graphics`PlotField3D`
This plots a field of vectors in three-dimensions. Vector heads could be added with VectorHeads -> True. In[50]:= PlotVectorField3D[(z^2 + 1) {-y, x, z / 10}, {x, -1, 1}, {y, -1, 1}, {z, 0, 2}]
Out[50]= - Graphics3D -

next page: 2.4 Animation back to table of contents