Mathematica Graphics - 1

Bernd Thaller
Institute of Mathematics
University of Graz
Austria
03-24-1999

Literatur:

Tom Wickham-Jones: Mathematica Graphics - Techniques, Applications. TELOS, Springer Verlag 1994

Stephen Wolfram: The Mathematica book — 4th ed. Wolfram Research, 1999

2D Plotting

Mathematica has many built-in commands for plotting functions. First we consider two-dimensional plots. There are essentially two possibilities: Graphs of functions of a real variable, and parametric curves in the plane. Functions of one real variable are plotted with the command Plot.

Plot[Exp[-(x/10)] Sin[x], {x, -2, 10}]

Curves in parametric representation are plotted using the command ParametricPlot.

ParametricPlot[{Exp[(t/10)] Sin[t], Exp[(t/10)] Cos[t]}, {t, 0, 
        4 π}]

You can only plot functions that return real values, or lists of real values. For the parametric plot, the list describes the coordinates of the curve in two dimensions. If a list is plotted with the Plot command, then the components of the list are plotted as separate curves and combined in one graphics.

Plot[{Exp[(t/10)] Sin[t], Exp[(t/10)] Cos[t]}, {t, 0, 4 π}]

The appearance of these plots can be changed by setting options. These are optional arguments of the plot commands which are of the form name->value.

Plot[Exp[-(x^2)], {x, -5, 5}, PlotRange -> {-0.1, 1.1}, 
      Frame -> True, Axes -> False, 
      PlotStyle -> Dashing[{0.01, 0.02}]]

The options available for a particular Mathematica command can always be listed by the command Options[cmd]

Options[Plot]

Animation

An animation is created just by plotting several aligned graphics. This can be simply done with a Do-loop

Do[Plot[Sin[x] Sin[a x], {x, 0, π}], {a, 1, 3,  .1}]

The plot range can be kept fixed by setting an appropriate option

Do[Plot[Sin[x] Sin[a x], {x, 0, π}, 
        PlotRange -> {{0, π}, {-1, 1}}], {a, 1, 3,  .1}]

There is a standard package Graphics`Animation` which provides several commands for animating two and three-dimensional plots.

Graphics output

For every command, Mathematica produces some result. The result of a graphics command is a Mathematica expression of type Graphics. It appears after the Out prompt in a special output form which hides its structure.

Plot[x, {x, -1, 1}]

Usually, the default style for any output is OutputForm. We can examine the structure of the last output by the command InputForm. This command displays a Mathematica expression in a form that can be used as a new input to Mathematica.

InputForm[%]

We see that this is again a Mathematica command. It is called Graphics and it just holds the structure of a graphics object. Its arguments are "Graphics primitives", and a list of options. The only Graphics primitve appearing here is "Line".

We can send a Graphics command directly as input to Mathematica.

Graphics[Line[{{0, -1}, {2, 0}, {1, 1}, {3, 2}}]]

We can display a graphics object with the command Show

Show[%]

The Show command is very useful. It can be used to redisplay the graphics objects produced by the command Plot

gr1 = Plot[BesselJ[(1/2), x], {x, 0, 20}]

Since graphics objects are just Mathematica expressions, they can be assigned to be the value of a symbol ("gr1"). Using Show, we can redisplay the graphics object gr1:

Show[gr1]

We can also change the values of options:

Show[gr1, PlotRange -> {- .8,  .8}, Frame -> True, 
      FrameTicks -> {Automatic, {- .5, 0,  .5}, None, None}]

Show can also be used to combine graphics in one image.

gr2 = Plot[Sin[x], {x, 0, 20}]

Show[gr1, gr2]

DisplayFunction

We can produce Mathematica graphics objects without displaying the image

gr3 = 
      Plot[Abs[Sin[x]], {x, -π, π}, 
        DisplayFunction -> Identity]

Show[gr3, DisplayFunction -> $DisplayFunction]

Here $DisplayFunction is the default value which tells the Mathematica kernel to produce Postscript code (which is then interpreted by the Frontend).

$DisplayFunction

The command Display converts a graphics object into postscript. You can see the postscript code by selecting the graphics cell and chose "Show Expression" from the Format menu. $DisplayFunction is a "pure function". The command Show applies this function to the graphics object. The function Display generates postscript code which is then passed to $Display

$Display

The setting for $Display is machine dependent. We can generate postscript code directly,

Display["graph.ps", gr3]

This generates a text file "graph.ps" with the postscript code of the graphics "gr3".

Setting the DisplayFunction to Identity has the effect that no postscript code is generated. This is useful, if you want to produce several plots and display them together in a single image:

gr1 = 
      Plot[Sin[π x], {x, -1, 1}, DisplayFunction -> Identity]; 
    gr2 = 
      ParametricPlot[{Cos[t] Sin[t], Cos[t]}, {t, 0, 2 π}, 
        DisplayFunction -> Identity]; gr3 = Graphics[Disk[{- .7,  .5},  .1]]; Show[gr1, gr2, gr3, AspectRatio -> 1, 
      DisplayFunction -> $DisplayFunction]

Functions and pure functions

The DisplayFunction is by default given as a pure function. As you probably know, a function is usually defined by  :=, for example,

f[x_] := Sin[x] + Cos[2 x]

f[3]

The function f is available to the system until it is explicitly removed. It is often more useful to define the function only once, at the time when it is needed. This is done, for example, as follows:

Sin[#1] + Cos[2 #1]&

This pure function takes one argument, which is inserted for every #. Pure functions can be recognized by the & at the end. They are applied like any other function in Mathematica, i.e., the arguments are supplied between brackets.

(Sin[#1] + Cos[2 #1]&)[3]

Use #1, #2,.. to define a pure function with several arguments:

(Cos[#1] Sin[#2]&)[3, 5]

The pure function Cos[#1] Sin[#2] & is applied to its arguments and then discarded. This saves system resources. In the context of graphics commands, the option values for DisplayFunction and ColorFunction are often defined as pure functions.

Graphics primitives

A graphics object is generated by applying the command Graphics to a list of graphics primitives. Here are other graphics primitives which can be used for two-dimensional graphics:

Point[pt]   (pt is a list of two coordinates)

Line[{pt1,pt2,...}]   (a polygonal line joining several points)

Polygon[{pt1,pt2,pt3,..}]   (a polygon)

Circle[pt, r]   (a circle with radius r and center at pt)

Disk[pt, r]   (a filled circle with radius r and center at pt)

Rectangle[pt1,pt2]   (a rectangle, pt1=lower left, pt2=upper right)

Text[expr, pt]   (converts expr to a text primitive centered at pt)

Here is an example:

Show[
      Graphics[{Circle[{0, 0}, 4], Rectangle[{-1, -1}, {3, 1}], 
          Text["A rectangle", {1, 1.5}]}], AspectRatio -> 1]

There are other interesting graphics primitives:

Raster[bitmap]  (a grayscale bitmap)

Here "bitmap" is a two-dimensional array of numbers between 0 and 1, which are interpreted as grayscale values (0=black, 1=white):

grayvalues := {{ .2,  .3,  .4,  .5}, { .6,  .7,  .8,  .9}}; Show[Graphics[Raster[grayvalues]]]

grayvalues := 
      Table[(1/2) (1 + Sin[x] Cos[y]), {x, -4, 4,  .2}, {y, -4, 
          4,  .2}]; Show[Graphics[Raster[grayvalues]]]

There is a similar graphics primitive for color arrays.

RasterArray[bitmap]  (a color bitmap)

Here bitmap is a two-dimensional array of color directives.

colrs := Table[Hue[Random[]], {7}, {10}]; Show[Graphics[RasterArray[colrs]]]

The action of Raster can be mimicked by RasterArray as follows:

grayvalues := {{ .2,  .3,  .4,  .5}, { .6,  .7,  .8,  .9}}; 
    Show[Graphics[RasterArray[Map[GrayLevel, grayvalues, {2}]]]]

Map and Levels

The command Map[GrayLevel,grayvalues,{2}] applies the function GrayLevel to all elements of the expression "grayvalues" on level 2. The level-structure of an expression can be made visible with the command TreeForm

TreeForm[grayvalues]

Since the array "grayvalues" is a list of lists,level 1 corresponds to the two list expressions,and level 2 is formed by the elements of these lists

Level[grayvalues, {1}]

Level[grayvalues, {2}]

Evaluation of Expressions

Usually, Mathematica evaluates expressions step-by-step. The function appearing in the first argument of a Plot command is not evaluated in the standard way. The function is first kept as it is. The Plot command applies the function to a sequence of appropriately chosen numerical positions, and evaluates for each of these arguments separately. Sometimes this causes a delay, if the evaluation takes a long time. It is sometimes better to have Mathematica evaluate the expression first, and then apply the evaluated expression to the sequence of points. For example:

f[x_] := (∫_0^x) Sin[y] \[DifferentialD]y

Timing[Plot[f[x], {x, 0, 10}]]

Timing[Plot[Evaluate[f[x]], {x, 0, 10}]]

The Standard Evaluation procedure is described in the Mathematica Book, Sect. 2.5.4.