Introduction to Mathematica 
Graphics

The Class

Violeta M. Ivanova, Ph.D., Educational Technology Consultant, MIT Academic Computing
Contact: N42-250J, 617-252-1383, violeta@mit . edu
Class materials: http://web.mit.edu/acmath/math/IntroMathematica
Web site: http://web.mit.edu/violeta/www/IAP2006

References

Mathematica 5.2 Documentation, Wolfram Research Inc., http://documents.wolfram.com/mathematica
J. Glynn and T. Gray, "The Beginner's Guide to Mathematica Version 4" http://www.mathware.com

Built-In Functions

2D Graphics

Plot[{f1[x], f2[x], ...}, {x, xmin, xmax}]:

Plot[{Sin[x], Cos[x]}, {x, -π, π}]

ListPlot[{{x1, y1}, {x2, y2}, ...}]:

listPrimeFractions = 
        Table[{i, Prime[i + 1]/Prime[i]}, {i, 1, 100}];
ListPlot[listPrimeFractions]

ParametricPlot[{{x1[t], y1[t]}, {x2[t], y2[t]}, ...}, {t, tmin, tmax}]:

ParametricPlot[{{(t^2), t}, {t, t}}, {t, -1, 1}]

3D Graphics

Plot3D[{f1[x, y], f2[x, y], ...}, {x, xmin, xmax}, {y, ymin, ymax}]:

Plot3D[
      Sin [x] - y^2,  {x,  -2 Pi,  2 Pi},  {y,  -2,  
        2}]

ListPlot3D[{{x11, y11, z11}, {x12, y12, z12}, ...}]:

points = 
        Table[Sin[x] - y^2,  {x,  -2 Pi,  2 Pi,  
            Pi/12},  {y,  -2,  
            2,   .2}];
listplotpoints = 
      ListPlot3D[points,  
        Axes -> {{x,  -2 Pi,  2 Pi},  {y,  -2,  
              2. }}]

ParametricPlot3D[{x[t], y[t], z[t]}, {t, tmin, tmax}]: 
A parametric curve, e.g. a Helix (a=3, b = 1/3):

ParametricPlot3D[{3 Cos[t], 3 Sin[t],  
        t/3},  {t,  - 2 Pi,  2 Pi}]

A parametric surface, e.g. :

ParametricPlot3D[{{u, -(v^2), v}, {u, v, (u^2) (v^2)}}, {u, -1, 
        1}, {v, -1, 1}]

Contour and Density Plots

surface  =  
      Plot3D[Sin[x y],  {x,  -Pi/2,  Pi/2},  {y,  0,  
          Pi }]

ContourPlot[f[x, y], {x, xmin, xmax}, {ymin, ymax}]:

contours = 
      ContourPlot[
        Sin[x y],  {x,  -Pi/2,  Pi/2},  {y,  0,  Pi }]

DensityPlot[f[x, y], {x, xmin, xmax}, {ymin, ymax}]:

density = 
      DensityPlot[
        Sin[x y],  {x,  -Pi/2,  Pi/2},  {y,  0,  Pi }]

Multiple Plots

sinplot = 
      Plot[Sin[x],  {x,  -2 Pi,  2 Pi}]
sinploints = 
        Table[{x,  Sin[x]},  {x,  -2 Pi,  2 Pi,  
            Pi/6}]; 
sinlistplot = 
      ListPlot[sinploints,  
        PlotStyle -> {RGBColor[1,  0,  0],  
            PointSize[0.02]}]
Show[sinplot,  sinlistplot]

Show[
      GraphicsArray[{surface, contours,  density}]]

Graphics Options

Scene Graphs and Mathematica

A scene graph is the basic structure for representation of graphics in computer languages and programs. A scene graph contains nodes for every geometry, transformation, color, style, view point, etc. that is part of the graphics.  Mathematica's scene graphs, as it may be expected, can be represented in symbolic notation: Show[graphicstype[primitives, options]]. 
Plotting functions in Mathematica are generally executed as: plottingfunction[function, range1, range2, ...] or plottingfunction[array]. From this, Mathematica creates the graphics primitives (things like Lines, Points, etc.), assigns default values to options, returns a graphics type, and displays it. The basic graphics types are Graphics, Graphics3D, SurfaceGraphics, ContourGraphics, and DensityGraphics. Note that you can combine in the same plot only graphics of the same type. For example, combining graphics created with ListPlot and Plot works, because both functions create objects of type Graphics.  
To create a customized look for a graphics, options can be assigned as follows: plottingfunction[ ..., option1->value1, option2->value2, ...].  All available options for common graphics types are listed in the Help Browser under Built-In Functions->Graphics and Sound->Graphics Primitives.

Show[Graphics[Circle[{0,  0},  1]],  
      AspectRatio -> Automatic]
ParametricPlot[{Sin[x], Cos[x]},  {x,  0,  
        2 Pi},  AspectRatio -> Automatic]

Basic Options

Basic options apply either for all graphics or for all graphics of a certain type.  Some options may have sub-options,which are assigned as lists of values: plottingfunction[..., option1->{value11, valaue12, ...}, option2->...].  Options may also include lists of values when more than one function is plotted. Some options, e.g. the plot label, can have only one value per graphic; others, like line thickness or color, can have a different value for every plot on the graphic.

\(Plot[{Sin[x], 
        Cos[x]}, {x, \(-π\), π}, 
PlotLabel -> "SINE AND COSINE PLOTS", 
PlotStyle -> {{Hue[1. ], 
            Thickness[0.01], 
            Dashing[{0.05, 0.02}]}, 
{RGBColor[0, 0, 1], 
            Thickness[
              0.02]}}, 
Ticks -> {{\(-π\), \(-\(π\/2\)\), 0, π\/2, π}, {\(-1\), \(-\(1\/2\)\), 0, 1\/2, 
            1}}, 
AxesStyle -> 
        Thickness[
          0.01], 
TextStyle -> {FontFamily -> "Times", FontSize -> 14}]\)

first50 = 
        ListPlot[
          listPrimeFractions[[
            Range[1, 
              50]]], 
PlotStyle -> {RGBColor[0, 0.6, 0], 
              PointSize[0.02]}, 
PlotRange -> {{0, 
                100}, {1, 2}}, 
AspectRatio -> 1.2, 
          GridLines -> Automatic];
second50 = 
        ListPlot[
          listPrimeFractions[[
            Range[51, 100]]], 
          PlotStyle -> {RGBColor[0.6, 0, 0], 
              PointSize[0.02]}, 
PlotRange -> {{50, 
                100}, Automatic}];
Show[first50, second50]

3D Options

3D options are valid for SurfaceGraphics and Graphics3D.

\(TraditionalForm\`Plot3D[
      Sin\ [x] - y^2, \ {x, \ \(-2\)\ Pi, \ 2\ Pi}, \ {y, \ \(-2\), \ 
        2}, 
		Boxed -> 
        False, \ \ 
		AxesEdge -> {{\(-1\), \ \(-1\)}, \ {1, \ \(-1\)}, \ {1, \ 
            1}}, 
		MeshStyle -> {Hue[
            0.1]}\ , 
		AmbientLight -> 
        RGBColor[0. , \ 0. , \ 1. ]]\)

Use Input->3D ViewPoint Selector ... to open the 3D PointView Selector to set the ViewPoint.

\(TraditionalForm\`Plot3D[
      Sin\ [x] - y^2, \ {x, \ \(-2\)\ Pi, \ 2\ Pi}, \ {y, \ \(-2\), \ 
        2}, 
		Boxed -> False, \ 
      BoxRatios -> {1, \ 2, \ 
          1.5}, 
		Shading -> False, \ 
      Axes -> False, \ ViewPoint -> {2.135, \ 2.613, \ 0.252}]\)

Other Options

contours = 
      ContourPlot[
        Sin[x y],  {x,  -Pi/2,  Pi/2},  {y,  0,  Pi },  
        Contours -> 20,  ContourLines -> False]

Mathematica Packages

Standard Add-On Packages

Packages are files that contain definitions of functions in addition to those that load when you start Mathematica. The directory StandardPackages inside the Add-Ons & Links directory in the Mathematica Help Browser contains several subdirectories with available packages that come with Mathematica. To use a function in any of these packages, you need to load the package first. For example, to use the graphics function FilledPlot, you need to load the package FilledPlot.

<< Graphics`FilledPlot`

The two single-quotes used here are "back-quotes" usually found on the same key with ~. They are not the same as single-quotes!

FilledPlot[{Sin[x], Cos[x]},  {x,  0,  2 Pi}, 
      Color -> {1,  0,  0}]

The << command always loads the named package.  Needs loads the specified package only if it has not already been loaded in the same session.

Needs["Graphics`FilledPlot`"]

Potential Problem

This applies to all packages, not only to Graphics packages.

Using a function before loading its package will cause a problem! Why?

Show[Graphics3D[Helix[2,  3,  3,  60]]]

Needs["Graphics`Shapes`"]

By using Helix before loading the package, you made Mathematica create an instance of the symbol Helix in the Global` context.  Loading Helix.m creates a new definition for Helix in the special context Graphics`Helix`.  When looking for a definition, Mathematica starts in the Global` context.
Use function Remove to solve the problem.

Remove[Helix]

Available Packages

Standard add-on packages come as part of Mathematica. See a list of standard add-on packages for your version of Mathematica in the Help Browser.
Free packages are available from the MathSource web site at http://www.wolfram.com/mathsource/ and from other sources.
Packages for sale are available from Wolfram Research and others. See http://www.wolfram.com/applications/index.html a current listing of packages for sale.

Standard Add-On Graphics

Available packages for graphics can be found under Add-Ons & Links -> Standard Packages -> Graphics in the Mathematica Help Browser. Functions from the add-on packages can use the basic options for graphics; usually, they also have some additional options.

2D Graphics

The FilledPlot and Shapes packages were discussed above.
Functions LogPlot, PolarPlot, BarChartPlot, PieChart, etc. are in the package Graphics.

Needs["Graphics`Graphics`"]

\(TraditionalForm\`PolarPlot[{2, \ t}, \ {t, \ 0, \ 
        2\ Pi}, \ 
		PlotStyle -> {{Hue[1], \ 
            Thickness[0.02]}, \ 
			\ \ \ {Hue[2/3], \ 
            Thickness[0.01]}}]\)

\(TraditionalForm\`BarChart[{20, \ 30, \ 40, \ 50, \ 60, \ 70, \ 80, \ 
        80, \ 70, \ 55, \ 40, \ 
        30}, \ 
		BarLabels -> {"Jan", \ "Feb", \ "Mar", \ "Apr", \ "May", \ "Jun", \ "Jul", \ "Aug", \ "Sep", \ "Oct", \ "Nov", \ "Dec"}, \ 
		\ BarGroupSpacing -> 0.5, 
      AxesLabel -> "Typical Temperature in Boston [F]"]\)

3D Graphics

Function ScatterPlot3D and other functions are in the package Graphics3D.

<< Graphics`Graphics3D`

?ScatterPlot3D

scatterpoints = 
        Flatten[Table[{x, y, Sin[x] - (y^2)}, {x, -2 π, 
              2 π, (π/6)}, {y, -2, 2,  .2}], 1];
scatterplotpoints = 
      ScatterPlot3D[scatterpoints, 
        PlotStyle -> {Hue[0], PointSize[0.02]}]

paramplotpoints = 
      ParametricPlot3D[{x, y, Sin[x] - (y^2)}, {x, -2 π, 
          2 π}, {y, -2, 2}]

Show[scatterplotpoints, paramplotpoints]

Packages RealTime3D and Default3D allow switching between real time 3D rotation and default 3D view.

<< RealTime3D`

<< Default3D`

Implicit Plots

ImplicitPlot allows plotting functions that are implicitly defined, e.g. by equations, in two dimensions.

Needs["Graphics`ImplicitPlot`"]

ImplicitPlot[{((((x^2) + (y^2)))^2) == 2 (x^2) - 2 (y^2), 
          (x^3) + (y^3) == 3 x y}, {x, -2, 2}, 
        PlotStyle -> {{Hue[1], Thickness[0.01]}, {Hue[(2/3)], 
              Thickness[0.01]}}];

ContourPlot3D allows plotting multiple contours of a function of three parameters. The default is a single contour at zero. Therefore, to plot the values where f = g, you can equivalently plot the contour where f - g = 0.  For example, to plot (x^2) + (y^2) + (z^2)=1, you can use ContourPlot3D to plot (x^2) + (y^2) + (z^2)-1.

Needs["Graphics`ContourPlot3D`"]

ContourPlot3D[
        (x^2) + (y^2) + (z^2) - 1,  {x,  -1,  1},  {y,  -1,  
          1},  {z,  -1,  1}];

Other Graphics

Other Graphics packages include, for example: ParametricPlot3D, which loads functions for plotting in spherical and cylindrical coordinates;  PlotField and PlotField3D, which load functions for plotting vector fields; and Polyhedra, which loads functions for plotting polyhedra.

Needs["Graphics`PlotField`"]

\(\(PlotVectorField[{Sin[x], Cos[y]}, {x, \ 0, \ Pi}, \ {y, \ 0, \ 
          Pi}, \ 
					ColorFunction -> 
          Hue];\)\)

The package Colors expands on the initial set of colors that is defined by the kernel. The package Legend loads functions for creating a plot legend.

Needs["Graphics`Colors`"]
Needs["Graphics`Legend`"]
?PlotLegend

\(\(Plot[{Sin[x], 
          Cos[x]}, {x, \(-π\), π}, \ 
		PlotStyle -> {Brick, \ 
            NavyBlue}, \ 
		PlotLegend -> {"sin(x)", \ "cos(x)"}];\)\)

Additional Graphics

Defining Graphics Functions

If a graphics function does not exist, you can always define it yourself, just like any other function. For example, let us define a function TrigonometricPlot, which creates a customized plot of trigonometric functions. The argument fx__ stands for "any sequence of expressions".

TrigonometricPlot[fx__, xmin_, xmax_] := 
      Plot[fx, {x, xmin, xmax}, 
        Ticks -> {{xmin, (xmin/2), 0, (xmax/2), xmax}, {-1, 0, 1}}, 
        PlotLabel -> "Various Sine and Cosine Functions"]
TrigonometricPlot[{Sin[x], Cos[x], Sin[x] + Cos[x], Sin[x] - Cos[x], 
        2 Sin[x]}, -π, π]

Would that have worked, if we had used fx_ instead of fx__? Yes, because Plot is a Listable function, so any function that we define that uses it will also be Listable. We could also make a new function Listable by using the command SetAttributes[newfunction, Listable].

Let us now define a function PlotListPlot that creates a customized plot of a function and a list of points.

\(\(PlotListPlot[fx_, xmin_, xmax_, 
          alist_List] := 
		Show[
          Plot[fx, {x, xmin, 
              xmax}, 
				PlotStyle -> {Hue[0.5], 
                Thickness[
                  0.01]}, 
				Ticks -> {{xmin, 
                  xmin\/2, 0, xmax\/2, xmax}, 
                Automatic}], 
			ListPlot[
            alist, 
				PlotStyle -> {RGBColor[1, 
                  0, 0], PointSize[0.02]}]];\)\)
PlotListPlot[Sin[x], -π, π, 
        Table[{t + (π/4), 
            Sin[t]}, {t, -π, π, (π/12)}]];

Other Add-On Packages

There are many add-on packages, besides those in Graphics, which include relevant functions. For example, DiscreteMath`ComputationalGeometry` loads functions for creating Delauney triangulations, Voronoi diagrams, and 3D meshes from scatter points.  Statistics`ContinuousDistributions` loads functions for creating, among other things, PDFs and CDFs of continuous probability distributions, such as the exponential distribution, which can be plotted with Plot.  Statistics`NonlinearFit` and NumericalMath`PolynomialFit` load functions for nonlinear and polynomial fit, respectively, to a list of points.

Print and Web

To print from Mathematica, change the Print Style Environment to Printout from the Format menu.   
To create hyperlinks in Mathematica documents, use the Find->Add/Remove Cell Tags... menu to create cell tags in text cells of the current notebook, and then the Input->Create Hyperlink... menu to create hyperlinks to those cell tags or to other notebooks and web sites. 
To create web pages from Mathematica notebooks, use the File->Save As Special... menu to save in HTML or XML format.

Sound Demo

The built-in function Play in directory Built-In Functions->Graphics and Sound->Sound Generation can play sound in Mathematica. Additional sound functions are included in the add-on package Audio in the Add-Ons & Links->Standard Packages->Miscellaneous directory.
Open the notebook sound_examples . nb to play some tunes.

Animation Demo

Various functions for creating animation are included in the add-on package Animation in the Add Ons & Links->Standard Packages->Graphics directory.  
Open the notebook animation_examples . nb to see some animations.