# Three-Dimensional Mathematica Graphics (Section 2.1)

### 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.

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.1 Graphics Commands

### 2.1.1 Function Plotting

Mathematica has a number of built-in commands which plot functions. The term function is used here to mean some Mathematica expression which evaluates to a real number or a list of real numbers. The function plotting commands produce a wide range of images.

 Plot ParametricPlot Plot3D ContourPlot DensityPlot ParametricPlot3D Play
Mathematica commands which graph functions.
 This is a three-dimensional surface where the height is given by Sin[x y] for x from 0 to 2 Pi and y from 0 to Pi. In[1]:= Plot3D[Sin[x y], {x, 0, 2 Pi}, {y, 0, 2 Pi}] Out[1]= - SurfaceGraphics - Sometimes we know how x, y, z depends on some parameter. For this use ParametricPlot3D. In[2]:= ParametricPlot3D[{Cos[t]^3, Cos[t]^2 Sin[t], Sin[t]^4}, {t, 0, 2 Pi}] Out[2]= - Graphics3D - ParametricPlot3D can also plot surfaces. Many of the typical Mathematica graphics are generated with ParametricPlot3D. In[3]:= ParametricPlot3D[(3 Cos[t]^2 - 1)^2 {Sin[t] Cos[p], Sin[t] Sin[p], Cos[t]}, {t, 0, Pi}, {p, 0, 2 Pi}, PlotPoints -> {21, 10}] Out[3]= - Graphics3D -

### 2.1.2 Data Plotting

We have now seen how to graph a Mathematica function and in certain situations this is useful. However you may have data in a file and these are what you wish to plot. In this section we shall see how to plot data. Each image shown for function plotting can be made for data plotting.

 Function plotting List plotting Plot ListPlot Plot3D ListPlot3D ContourPlot ListContourPlot DensityPlot ListDensityPlot Play ListPlay
Mathematica commands which plot functions and the corresponding commands to plot lists of data.

However before we look at plotting data we should see how to import data into Mathematica. These data may have been written by another application such as a spreadsheet or by some custom program which you have written. If the data are written in an ASCII form, that is, if they can be looked at with a text editor, they can easily be read into Mathematica. For the examples given here to work the data files which are loaded must exist. To prevent this being a problem I have included the Mathematica commands which will create the files used in these examples.

 This will write the file so we can use it. In[4]:= (Do[WriteString["file1.dat", i^0, " ", i^1, " ", i^2, " ", i^3, "\n"], {i, 8}]; Close["file1.dat"]) Out[4]= file1.dat This shows the contents of a data file. Notice that each line consists of four numbers. In[5]:= !!file1.dat 1 1 1 1 1 2 4 8 1 3 9 27 1 4 16 64 1 5 25 125 1 6 36 216 1 7 49 343 1 8 64 512 This reads the numbers in the file and builds a list. The list is set to be the value of the symbol mydata. In[6]:= mydata = ReadList["file1.dat", Number, RecordLists -> True] Out[6]= {{1, 1, 1, 1}, {1, 2, 4, 8}, {1, 3, 9, 27}, {1, 4, 16, 64}, {1, 5, 25, 125}, {1, 6, 36, 216}, {1, 7, 49, 343}, {1, 8, 64, 512}} The symbol mydata evaluates to the matrix of numbers. In[7]:= mydata Out[7]= {{1, 1, 1, 1}, {1, 2, 4, 8}, {1, 3, 9, 27}, {1, 4, 16, 64}, {1, 5, 25, 125}, {1, 6, 36, 216}, {1, 7, 49, 343}, {1, 8, 64, 512}} We can now plot mydata with ListPlot3D. It takes lists of numbers instead of a function. In[8]:= ListPlot3D[mydata] Out[8]= - SurfaceGraphics3D -

The range of x and y are determined by the dimensions of the matrix. To read in a set of data and carry out a command such as ListPlot3D, ListContourPlot or ListDensityPlot with different ranges is a little more complex. The data points must be regularly spaced over a grid. In addition you must specify the bounds of the x and y coordinates. First we shall make the file.

 This writes the file we want to use for this example. This file is a little more complex than the previous example. I close the file for neatness. In[9]:= (WriteString["file2.dat", -1, "  ", 1, "\n"]; WriteString["file2.dat", -2, "  ", 2, "\n"]; Do[WriteString["file2.dat", 3 i, " ", 2 i, " ", i, " ", 2 i, " ", 3 i, "\n"], {i, -2, 2}]; Close["file2.dat"]) Out[9]= file2.dat The first two lines contain the x and y range. The rest is the array of heights. In[10]:= !!file2.dat -1  1 -2  2 -6 -4 -2 -4 -6 -3 -2 -1 -2 -3 0 0 0 0 0 3 2 1 2 3 6 4 2 4 6 We open the file so we can read in the different parts. In[11]:= stm = OpenRead["file2.dat"]; This is the x range. In[12]:= {xmin, xmax} = Read[stm, {Number, Number}] Out[12]= {-1, 1} This is the y range. In[13]:= {ymin, ymax} = Read[stm, {Number, Number}] Out[13]= {-2, 2} The rest of the file contains the data. Then we close the file. In[14]:= (data1 = ReadList[stm, {Number, Number, Number, Number, Number}]; Close[stm]) Out[14]= file2.dat This makes a surface plot. We could have used ListContourPlot or ListDensityPlot here. Note that the axes use the values for the mesh. In[15]:= ListPlot3D[data1, MeshRange -> {{xmin, xmax}, {ymin, ymax}}] Out[15]= - SurfaceGraphics3D -

### GraphicsArray

A last type of data plotting can be done with the command GraphicsArray. Here the data which are plotted are graphics objects.

 We use Table to make an array of objects. DisplayFunction -> Identity is given to suppress the generation of graphical images at this point. In[16]:= Table[ParametricPlot3D[{Cos[i t], Sin[j t], Cos[2 t]}, {t, 0, 2 Pi}, DisplayFunction -> Identity], {i, 1, 2}, {j, 1, 2}] Out[16]= {{- Graphics3D -, - Graphics3D -}, {- Graphics3D -, - Graphics3D -}} This is an array of the images. In[17]:= Show[GraphicsArray[%]] Out[17]= - GraphicsArray -