Upload
phungquynh
View
218
Download
0
Embed Size (px)
Citation preview
Overview of Basic Graphics Commands
In[1]:= << Graphics‘
Two-Dimensional Graphics
Å Plot
The simplest example of Mathematica’s graphing capabilities is a graph of a function of one variable created with Plot. Plot takes a function to be graphed and a domain for the variable, and generates a two-dimensional graph.
Plot[Sin[x]/x, {x, -10, 10}]
-10 -5 5 10
-0.2
0.2
0.4
0.6
0.8
1
ú Graphics ú
Plot also accepts a list of functions to plot on the same set of axes.
Plot@8Sin@xD, Cos@xD<, 8x, -p, p<D;
-3 -2 -1 1 2 3
-1
-0.5
0.5
1
1.1.2Å Options
There are dozens of options we can use to control almost every aspect of a graph. Options[FunctionName] returns a list of the options available for a function, along with their default values.
Options@PlotD
9AspectRatio�1
þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþGoldenRatio
, Axes � Automatic, AxesLabel� None,
AxesOrigin� Automatic, AxesStyle� Automatic, Background� Automatic,
ColorOutput� Automatic, Compiled� True, DefaultColor� Automatic,Epilog � 8<, Frame � False, FrameLabel� None, FrameStyle� Automatic,
FrameTicks� Automatic, GridLines� None, ImageSize� Automatic,
MaxBend � 10., PlotDivision� 30., PlotLabel� None, PlotPoints� 25,
PlotRange� Automatic, PlotRegion� Automatic, PlotStyle� Automatic,
Prolog � 8<, RotateLabel� True, Ticks � Automatic,
DefaultFont� $DefaultFont, DisplayFunction� $DisplayFunction,
FormatType� $FormatType, TextStyle� $TextStyle=
For example, by default Mathematica uses an algorithm to choose the most “interesting” y-range for a graph. In the above list, we see that the default value for PlotRange is Automatic.
2 Mathematica Graphics
Plot@Sin@x2D�x2, 8x, -10, 10<D;
-10 -5 5 10
-0.1
-0.05
0.05
0.1
0.15
We can override the default setting by giving a different value to the PlotRange option.
Plot@Sin@x2D�x2, 8x, -10, 10<, PlotRange -> 8-0.25, 1.05<D;
-10 -5 5 10
-0.2
0.2
0.4
0.6
0.8
1
In every case, options are added after the required arguments to the function. We set an option by typing the name of the option, an arrow made by the two characters - and > (or the special character � made by typing Ê->Ê), and the new value of the option. Note that most plotting commands have a similar set of options.
Mathematica Graphics 3
Plot@Sin@xD�x, 8x, -10, 10<, Frame -> True,
PlotLabel -> "sinc function", GridLines -> Automatic,
PlotRange -> 88-11, 11<, 8-0.5, 1.15<<,
AspectRatio -> 1D;
-10 -5 0 5 10
-0.25
0
0.25
0.5
0.75
1
sinc function
In the following sections we will change many of a graph’s default option settings.
See also SetOptions, FullOptions, FullGraphics
1.1.3Å ParametricPlot
ParametricPlot plots a two-dimensional curve described by two functions of the same parameter, one that describes movement in the x direction and one for the y direction. This allows us to plot curves that are not functions, in the mathematical sense. Here is a parametric plot of a circle.
4 Mathematica Graphics
ParametricPlot@8Sin@tD, Cos@tD<, 8t, 0, 2 p<D;
-1 -0.5 0.5 1
-1
-0.5
0.5
1
The option AspectRatio controls the relative sizes of units on the two axes; the setting Automatic makes them equal (that is, makes one unit on the vertical axis equal to one unit on the horizontal axis).
ParametricPlot@8Sin@tD, Cos@tD<, 8t, 0, 2 p<, AspectRatio� AutomaticD;
-1 -0.5 0.5 1
-1
-0.5
0.5
1
ParametricPlot can be used to plot graphs of complicated curves that cannot be expressed as a function of two variables.
Mathematica Graphics 5
ParametricPlot@84 Cos@-5 t� 4D + 7 Cos@tD, 4 Sin@-5 t�4D + 7 Sin@tD<,
8t, 0, 8 p<, AspectRatio� AutomaticD;
-10 -5 5 10
-10
-5
5
10
ParametricPlot takes many of the same options as Plot.
6 Mathematica Graphics
ParametricPlot@84 Cos@-11 t�4D + 7 Cos@tD, 4 Sin@-11 t� 4D + 7 Sin@tD<,
8t, 0, 8 p<, AspectRatio� Automatic, Axes -> False,
Frame -> True, FrameLabel -> 8"x", "y"<D;
-10 -5 0 5 10x
-10
-5
0
5
10y
1.1.4Å ImplicitPlot
ImplicitPlot allows us to plot implicit relations, rather than functions. It is defined in one of the standard packages, so we must load it first with the Needs command.
Needs@"Graphics‘ImplicitPlot‘"D
ImplicitPlot[eqn, {x, a, b}] draws a graph of the set of points that satisfy eqn. The variable x is associated with the horizontal axis and ranges from a to b. The remaining variable in the equation is associated with the vertical axis. We can also specify a vertical range for the graph using the form ImplicitPlot[eqn, {x, a, b}, {y, c, d}].
Mathematica Graphics 7
ImplicitPlot@3 x2 + 3 x y + 12 y2 == 12, 8x, -2.5, 2.5<, AxesOrigin� 80, 0<D;
-2 -1 1 2
-1
-0.5
0.5
1
Like most graphing functions, ImplicitPlot accepts a list of functions to plot on the same set of axes.
ImplicitPlot@
83 x2 + 3 x y + 12 y2 == 12, 12 x2 + 3 x y + 3 y2 == 12, 3 x2 + 12 x y + 3 y2 == 1<,
8x, -2.5, 2.5<, 8y, -2.5, 2.5<, AxesOrigin� 80, 0<D;
-2 -1 1 2
-2
-1
1
2
1.1.5Å Graphics Directives and Plot Styles
Mathematica contains several objects called graphics directives, which specify the style in which a graph should be drawn. Graphics directives control the color, thickness, point size, and dashing of lines, points, and other objects.
For example, to specify that lines should be drawn with a specified thickness, we use the directive Thickness[t], where t is given as a percentage of the width of a graph.
8 Mathematica Graphics
All two-dimensional graphing functions have an option called PlotStyle, which allows us to specify a list of graphics directives that control how the actual curve (as opposed to the surrounding axes, grid lines, etc.) is drawn. To draw a sine wave so that the curve is drawn with a thickness 2% of the width of the graph, we set the option PlotStyle -> Thickness[0.02].
Plot@Sin@xD, 8x, -3, 3<, PlotStyle -> [email protected]<D;
-3 -2 -1 1 2 3
-1
-0.5
0.5
1
To draw a curve with a dashed line, we use the directive Dashing. Dashing[{d}] draws a line so that it alternates between line segments d percent of the width of the graph and gaps d percent of the width of the graph. Dashing[{d1,d2}] alternates between line segments d1 long and gaps d2 long, and Dashing[{d1,d2,…}] applies the successive widths cyclically. The following graph uses line segments twice as long as the gaps.
Plot@Sin@xD, 8x, -3, 3<, PlotStyle -> [email protected], 0.02<D<D;
-3 -2 -1 1 2 3
-1
-0.5
0.5
1
There are several ways to specify colors using graphics directives. RGBColor[r,g,b] describes a color made up of r, g, and b percent of red, green, and blue, respectively. Thus, RGBColor[1,0,0] is red, and RGBColor[1,0,1] is purple. (The parameters r, g, and b must all be between 0 and 1.)
Other color functions are Hue[h], which represents the spectrum of colors going through red, orange, yellow, green, blue, purple, and back to red; and GrayLevel[g] (0� g � 1), where GrayLevel[0] is black and GrayLevel[1] is white.
Mathematica Graphics 9
To change the style of each graph in a list given to Plot, we give a list containing as many graphics directives as there are functions being plotted. Here we draw the first curve in the list using a thick line (Thickness[0.02]), and the second curve using a green line (RGBColor[0,1,0]).
Plot@8Sin@xD, Cos@xD<, 8x, -3, 3<,
PlotStyle -> [email protected], RGBColor@0, 1, 0D<D;
-3 -2 -1 1 2 3
-1
-0.5
0.5
1
To apply multiple styles to each function in a list, we surround the styles that apply to each function inside a set of list brackets.
Plot@8Sin@xD, Cos@xD<, 8x, -3, 3<,
PlotStyle -> [email protected], [email protected]<,
[email protected], 0.02<D, [email protected]<<D;
-3 -2 -1 1 2 3
-1
-0.5
0.5
1
One tricky case to be aware of is that in order to specify more than one graphics directive in the plot style of a single function, we must surround the graphics directives with the double list brackets {{ and }}.
10 Mathematica Graphics
Plot@Sin@xD�x, 8x, -10, 10<,
PlotStyle� 88RGBColor@0, 0, 1D, [email protected]<<D;
-10 -5 5 10
-0.2
0.2
0.4
0.6
0.8
1
See also PointSize, CMYKColor, AbsoluteThickness, AbsolutePointSize, Graphics‘Colors‘, Graphics‘ArgColor‘
1.1.6Å Combining Graphs
Show allows us to display a previously computed graph without having to recompute any of the points that make up the curve(s).
Show[graphics, options] displays two- and three-dimensional graphics using the new option settings specified. Show accepts options that affect the way a graph or its surrounding elements (axes, frame, etc.) is drawn, without requiring any points of the graph to be recomputed.
p1 = Plot@ Sin@xD �x, 8x, -10, 10<D;
-10 -5 5 10
-0.2
0.2
0.4
0.6
0.8
1
None of the options given below require points on the graph to be recomputed, so we can use them inside Show.
Mathematica Graphics 11
Show@p1, Frame � True, AxesStyle� Hue@0D,
PlotRange� 8-0.1, 0.8<, PlotLabel� "p1"D;
-10 -5 0 5 10
0
0.2
0.4
0.6
0.8p1
When given a list of graphics, Show combines them onto the same set of axes. Here is another two-dimensional plot.
p2 = Plot@BesselJ@2, xD, 8x, -10, 10<, PlotStyle� [email protected]<D;
-10 -5 5 10
-0.2
0.2
0.4
We combine p1 and p2 by placing their names in a list and giving the list to Show.
12 Mathematica Graphics
Show@8p1, p2<D;
-10 -5 5 10
-0.2
0.2
0.4
0.6
0.8
1
Show also combines three-dimensional graphics. Here are two three-dimensional graphs.
p3 = Plot3D@1 - x, 8x, 0, 1<, 8y, 0, 1<, ColorFunction� HueD;
p4 = Plot3D@y, 8x, 0, 1<, 8y, 0, 1<, ColorFunction� GrayLevelD;
0
0.25
0.5
0.75
1 0
0.25
0.5
0.75
1
0
0.25
0.5
0.75
1
0
0.25
0.5
0.75
1
Mathematica Graphics 13
0
0.25
0.5
0.75
1 0
0.25
0.5
0.75
1
0
0.25
0.5
0.75
1
0
0.25
0.5
0.75
1
We put them in the same box using Show.
Show@8p3, p4<D;
0
0.25
0.5
0.75
10
0.25
0.5
0.75
1
0
0.25
0.5
0.75
1
0
0.25
0.5
0.75
1
The object GraphicsArray takes an array of graphs, and when used with Show, displays the graphs in an array. Here is an array containing the four previous graphs.
14 Mathematica Graphics
Show@GraphicsArray@88p1, p3<, 8p4, p2<<DD;
00.250.5
0.7510
0.250.50.751
00.250.50.751
00.250.5
0.751
-10 -5 5 10-0.2
0.2
0.4
-10 -5 5 10-0.2
0.20.40.60.81
00.250.5
0.7510
0.250.50.751
00.250.50.751
00.250.5
0.751
See also DisplayFunction, $DisplayFunction, Identity
Mathematica Graphics 15
Exercises: Two-Dimensional Graphics
Í Make a simple two-dimensional plot of x2 - 20 cosHx2L between -10 and 10. (If there are obvious flaws in the graphic, plot it again using more plot points.)
Plot@x2 - 20 Cos@x2D, 8x, -10, 10<D;
-10 -5 5 10
-20
20
40
60
80
100
120
Í Use Options[Plot] or the on-line help to find a list of all the options that Plot accepts. Plot the same function as above, this time changing at least five of Mathematica’s default options.
Plot@x2 - 20 Cos@x2D, 8x, -10, 10<, PlotPoints� 75, Frame � True,
GridLines� Automatic, PlotLabel� "Exercise One", AspectRatio� 0.5D;
-10 -5 0 5 10-20
0
20
40
60
80
100
120Exercise One
16 Mathematica Graphics
1.2 Three-Dimensional Graphics
1.2.1Å Plot3D
Plot3D is the three-dimensional analog of the Plot command. Given a function of two variables and a domain for each variable, Plot3D produces a surface plot.
Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<D
-2
0
2
-2
0
2
-1
-0.5
0
0.5
1
-2
0
2
ú SurfaceGraphics ú
Applying options to three-dimensional graphics works the same as with two-dimensional graphics; in fact, many of the options are the same.
One of the differences between two- and three-dimensional plotting in Mathematica is point sampling. Instead of adaptive sampling, three-dimensional plots rely on a fixed grid of points at which to evaluate the function. By default, a 15 × 15 grid is used, resulting in 152 = 225 points plotted; raising this number results in a smoother graph, but takes more time and memory to generate.
Here is a smoother graph of the same function as above.
Mathematica Graphics 17
Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<,
Axes � False, FaceGrids� All, PlotPoints� 25D;
See also HiddenSurface, RenderAll, Lighting, ColorFunction
1.2.2Å Changing the Viewpoint
One important option to three-dimensional plotting functions is the viewpoint, the point in space from which the observer looks at the object. ViewPoint is an option to all three-dimensional graphics functions. Its default value is {1.3, –2.4, 2.0}, which can be changed by entering a new value directly as an option.
Show[%, ViewPoint->{0,3,2}];
18 Mathematica Graphics
Mathematica provides an easier way to do this using the 3D ViewPoint Selector. To use this front-end feature we pull down the Input menu and choose 3D ViewPoint Selector. Rotating the box with the mouse will have Mathematica compute the point from which to view the object. The Paste button enters the viewpoint at the current text insertion point.
1.2.3Å ParametricPlot3D
ParametricPlot3D is the three-dimensional analog of ParametricPlot. Depending on the input, ParametricPlot3D produces a space curve or a surface.
When we give ParametricPlot3D a list of three parametric functions in one parameter, the result is a space curve.
ParametricPlot3D@8Sin@tD, Cos@tD, t�3<, 8t, 0, 6 p<, Axes � FalseD;
A list of three parametric functions in two parameters results in a surface.
Mathematica Graphics 19
ParametricPlot3D@
8Sin@vD Cos@uD, Sin@vD Sin@uD, Cos@vD<, 8u, 0, 1.5 p<, 8v, 0, p<D;
-1-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-1-0.5
0
0.5
1
-1
-0.5
0
0.5
1
Like most graphing functions, ParametricPlot3D accepts a list of sets of parametric equations and plots the surfaces together.
20 Mathematica Graphics
ParametricPlot3D@88Sin@vD Cos@uD, Sin@vD Sin@uD, Cos@vD<,
8 Sin@vD Cos@4 u� 3D �2, Sin@vD Sin@4 u�3D� 2, Cos@vD �2<<,
8u, 0, 1.5 p<, 8v, 0, p<D;
-1-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-1-0.5
0
0.5
1
-1
-0.5
0
0.5
1
Options are given to ParametricPlot3D the same way as for Plot3D. Most of the options are the same.
Mathematica Graphics 21
Exercises: Three-Dimensional Graphics
Í Make a three-dimensional plot of the function sin(x+sin(y)) between -3 and 3 on both axes.
Plot3D@Sin@x + Sin@yDD, 8x, -3, 3<, 8y, -3, 3<D;
-2
0
2
-2
0
2
-1
-0.5
0
0.5
1
-2
0
2
Í Enter Options[Plot3D] or use the on-line help to find a list of all the options that Plot3D accepts. Plot the same function again, this time changing at least four of Mathematica’s default options, including the options that control the smoothness of the plot and the color.
changedplot3d =
Plot3D@Sin@x + Sin@yDD, 8x, -3, 3<, 8y, -3, 3<, PlotPoints� 815, 45<,
Mesh � False, ColorFunction� Hue, FaceGrids� AllD;
-2
0
2
-2
0
2
-1
-0.5
0
0.5
1
-2
0
2
22 Mathematica Graphics
Mathematica Graphics 23
1.3 Contour and Density Graphics
1.3.1Å ContourPlot and DensityPlot
Mathematica plots contour and density plots of functions of two or three variables. These plotting functions work much like Plot and Plot3D with the exception of special options that apply only to contour and density plots.
ContourPlot displays a graphic of a function of two variables, where regions of different intensities of gray have (nearly) the same function value.
ContourPlot@Exp@xD Sin@yD, 8x, -3, 3<, 8y, -3, 3<D;
-3 -2 -1 0 1 2 3
-3
-2
-1
0
1
2
3
DensityPlot by default generates a grid of gray levels, where the lighter gray areas have greater function values than the darker gray areas.
24 Mathematica Graphics
DensityPlot@Exp@xD Sin@yD, 8x, -3, 3<, 8y, -3, 3<D;
-3 -2 -1 0 1 2 3
-3
-2
-1
0
1
2
3
See also ColorFunction, Mesh, Contours, ContourLines, ContourStyle
1.3.2Å ContourPlot3D
The function ContourPlot3D provides a way to plot surfaces showing particular values of a function of three variables. This function is defined in one of the standard add-on packages, so we must load the package before using the function.
Needs["Graphics‘ContourPlot3D‘"]
ContourPlot3D[fun, {x, x0, x1}, {y, y0, y1}, {z, z0, z1}] plots the surface implicitly defined by fun[x, y, z] == 0. Setting the option Contours to {val1, val2, … } plots the level surfaces corresponding to the values val1, val2, ….
Mathematica Graphics 25
ContourPlot3D@Sqrt@x2 + y2 + z2D, 8x, -1, 1<,
8y, 0, 1<, 8z, 0, 1<, Contours � 80.25, 0.5, 0.75<D;
26 Mathematica Graphics
Exercises: Contour and Density Graphics
Í Create a density plot of the function sin(x - sin(y)) over any range that includes the origin. Render the graphic with twice as many PlotPoints. Experiment with other options.
DensityPlot@Sin@x - Sin@yDD, 8x, -10, 10<, 8y, -10, 10<D;
-10 -5 0 5 10
-10
-5
0
5
10
Mathematica Graphics 27
DensityPlot@Sin@x - Sin@yDD, 8x, -10, 10<, 8y, -10, 10<,
PlotPoints� 30, Mesh -> False, FrameLabel -> 8"x", "y"<D;
-10 -5 0 5 10x
-10
-5
0
5
10
y
Í Repeat the above exercise using ContourPlot instead of DensityPlot. Experiment with the options to ContourPlot that do not apply to DensityPlot.
28 Mathematica Graphics
ContourPlot@Sin@x - Sin@yDD, 8x, -10, 10<, 8y, -10, 10<D;
-10 -5 0 5 10
-10
-5
0
5
10
ContourPlot@Sin@x - Sin@yDD, 8x, -10, 10<, 8y, -10, 10<,
PlotPoints� 30, Contours� 30, ContourLines� FalseD;
-10 -5 0 5 10
-10
-5
0
5
10
Mathematica Graphics 29
30 Mathematica Graphics
1.4 Plotting Data
There are many occasions when we want to work with data rather than functions. There are several functions designed to visualize data in two or three dimensions. For these examples, we need data to work with. In practice, we would most likely read these data from a file or use the output of other calculations. For this demonstration we will create a list of ordered pairs to use as data.
exampleData = N@Table@8n, n + Sin@nD + Random@D<, 8n, 0, 5 p, 0.2 p<DD;
ListPlot takes an array of data and plots it in two dimensions. Given a one-dimensional set of data such as {10, 20, 30, 40}, Mathematica plots the ordered pairs {{1, 10}, {2, 20}, {3, 30}, {4, 40}}. In this case we supply a list of ordered pairs and Mathematica plots the points using our explicit x values. (The graphics directive PointSize[p] specifies that points should be drawn so they are p percent of the width of the graph.)
pointplot = ListPlot@exampleData, PlotStyle� [email protected];
2.5 5 7.5 10 12.5 15
2.5
5
7.5
10
12.5
15
Options to ListPlot include nearly all of those applicable to Plot. One exception is the option PlotJoined, which when set to True draws a line connecting each of the points.
Mathematica Graphics 31
joinedplot =
ListPlot@exampleData, PlotJoined� True, PlotStyle� RGBColor@0, 0, 1DD;
2.5 5 7.5 10 12.5 15
2.5
5
7.5
10
12.5
15
At this point in our analysis we can easily find a good least-squares fit to these data. The function Fit takes as arguments a set of data, a set of basis functions for the best-fit polynomial, and a list of variables to be used. Here we include only constant, linear, and quadratic terms for the best-fit function.
exampleFit = Fit@exampleData, 81, x, x2<, xD
0.918174+ 0.853911x + 0.0105764x2
Here is a plot of the best-fit quadratic polynomial.
fitplot = Plot@exampleFit, 8x, 0, 5 p<, PlotStyle -> [email protected]<DD;
2.5 5 7.5 10 12.5 15
2.5
5
7.5
10
12.5
15
Here we combine the previous three graphs.
32 Mathematica Graphics
Show@8pointplot, joinedplot, fitplot<D;
2.5 5 7.5 10 12.5 15
2.5
5
7.5
10
12.5
15
When working with three-dimensional data, we use analogs to Plot3D, DensityPlot, and ContourPlot. ListPlot3D plots a three-dimensional surface from a rectangular array of height values.
examplearray=
Table@n + Sin@nD + 3 Random@D, 8i, 1, 5 p, 0.3 p<, 8n, 1, 5 p, 0.3 p<D;
ListPlot3D@examplearrayD;
5
10
15
5
10
15
0
5
10
15
5
10
15
ListContourPlot and ListDensityPlot create density and contour plots from rectangular arrays of data.
Mathematica Graphics 33
ListDensityPlot@examplearrayD;
0 2.5 5 7.5 10 12.5 15
0
2.5
5
7.5
10
12.5
15
34 Mathematica Graphics
Exercises: Plotting Data
Í Create tabular data from the curve x cosHxL - sinHx2L between x = -2 p and x = 2 p and display the data using ListPlot.
mydata = Table@N@8x, x Cos@xD - Sin@x2D<D, 8x, -2 p, 2 p, 0.05 p<D;
ListPlot@mydata, PlotStyle� [email protected];
-6 -4 -2 2 4 6
-6
-4
-2
2
4
6
Í Create a list of the first 20 prime numbers. (Hint: use Table and Prime[n], which gives the nth prime number.) Plot the list using ListPlot, then fit the data to a quadratic polynomial, and plot the data and the best-fitting curve on the same set of axes. Use options to change the color and other aspects of the graphic.
primes = Table@Prime@nD, 8n, 1, 20<D
General::spell1 : Possible spelling error: new symbol
name "primes" is similar to existing symbol "Primes".
82, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71<
Mathematica Graphics 35
pointplot = ListPlot@primes, PlotStyle� 8Red, [email protected]<D;
5 10 15 20
10
20
30
40
50
60
70
fitline = Fit@primes, 81, x, x2<, xD
-1.92368+ 2.2055 x + 0.0746753x2
fitplot = Plot@fitline, 8x, 0, 20<, PlotStyle� 8Blue<D;
5 10 15 20
10
20
30
40
50
60
70
36 Mathematica Graphics
Show@8pointplot, fitplot<D;
5 10 15 20
10
20
30
40
50
60
70
Mathematica Graphics 37
38 Mathematica Graphics
2Using Packages
A number of tools that we use to create and customize Mathematica graphics are defined in the standard packages that ship with Mathematica. Generally, these packages either define new functions or graphics primitives. We may browse these packages using the help browser.
As an example, The Graphics‘Arrow‘ package defines graphics primitives and options for drawing arrows. To load these functions into the Mathematica kernel, we execute the following Needs command.
? Arrow
Arrow@start, finish, HoptsLD is a graphics primitiverepresenting an arrow starting at start and ending at finish.
Needs["Graphics‘Arrow‘"]
? Arrow
Arrow@start, finish, HoptsLD is a graphics primitive
representing an arrow starting at start and ending at finish.
It is important to load the package defining a function before we first invoke that function, for if we don’t, we may shadow the package definition and prevent it from being correctly defined when we do load the package.
Each set of related packages also contains a master package that defines the names of all Mathematica symbols defined in those packages. If we load this package first, using any function defined in any of the packages will cause that package to load automatically.
Needs["Graphics‘Master‘"]
If we use certain package functions often, it is a good idea to load them automatically by adding the appropriate lines to the init.m file.
init.m is a user-configurable package that is loaded last in the startup process. init.m can contain any code that we would like to be evaluated automatically each time we launch the Mathematica kernel. Here are some examples of code that might be found in an init.m file.
We could add a new directory to the search path contained in $Path.
AppendTo@$Path, "Hard Drive: Mathematica: Mathematica Applications "D
Syntax::bktmcp :
"Expression \"\!\HAppendTo@\H\H$Path, \H\H\"Hard Drive:\H\HMathematica : \H\HMathematica\\ \H\H÷ 12 ø\L\L\"D\L\L\L\L\L\L\L\L\L\" has no closing \"\!\H\"D\"\L\".\!\H\"\"\L"
AppendTo@$Path, "Hard Drive: Mathematica: MathematicaApplications "DThe following line changes the default viewpoint for three-dimesional graphics.
Mathematica Graphics 39
SetOptions@8Plot3D, ListPlot3D, Graphics3D, SurfaceGraphics<,
ViewPoint� 8-2.581, -1.357, 1.717<D;
Placing the following code in the init.m file will automatically load the Graphics‘Colors‘ package each time the kernel is launched.
Needs@"Graphics‘Colors‘"D;
40 Mathematica Graphics
Mathematica Graphics 41
3Selected Graphics Packages
3.1 Shapes
The standard package Graphics‘Shapes‘ provides Mathematica definitions of common three-dimensional shapes.
Needs["Graphics‘Shapes‘"]
Once a package is loaded, we can get a list of all the objects that it defines by using ? with the full package name and * (to denote all objects).
?Graphics‘Shapes‘*
AffineShape MoebiusStrip SphereCone OutlinePolygons TorusCylinder PerforatePolygons TranslateShapeDoubleHelix RotateShape WireFrameHelix ShrinkPolygons
Torus[r1, r2, n, m] creates a list of n m polygons approximating a torus centered around the z axis with radii r1 and r2. Default values of r1, r2, n, and m are 1, 0.5, 20 r1, and 20 r2, respectively. The other shapes are defined similarly.
We use the Show command to render the object in our notebook.
Show@Graphics3D@Torus@1, 0.75, 25, 25DDD;
42 Mathematica Graphics
Transformations on these Mathematica objects, such as WireFrame and RotateShape are wrapped around the shape, using the following syntax.
Show@WireFrame@Graphics3D@Torus@1, 0.75, 25, 25DDDD;
3.2 Polyhedra
Graphics‘Polyhedra‘ defines the polyhedra Tetrahedron, Cube, Hexahedron, Octahedron, Dodecahedron, SmallStellatedDodecahedron, GreatDodecahedron, GreatStellatedDodecahedron, Icosahedron, and GreatIcosahedron, as well as functions to operate on them: Faces, Geodesate, OpenTruncate, Stellate, Truncate, and Vertices.
Needs@"Graphics‘Polyhedra‘"D
Triangle::shdw :
Symbol Triangle appears in multiple contexts 8Geometry‘Polytopes‘,Graphics‘MultipleListPlot‘<; definitions in context
Geometry‘Polytopes‘ may shadow or be shadowed by other definitions.
Polyhedron[name] gives a Graphics3D object representing the specified solid centered at the origin and with unit distance to the midpoints of the edges. The possible names are in the list Polyhedra.
Polyhedra
8Tetrahedron, Cube, Octahedron, Dodecahedron, Icosahedron,
Hexahedron, GreatDodecahedron, SmallStellatedDodecahedron,
GreatStellatedDodecahedron, GreatIcosahedron<
Mathematica Graphics 43
DisplayTogetherArray@8
8Show@Polyhedron@TetrahedronD, Boxed � FalseD,
Show@Polyhedron@CubeD, Boxed � FalseD,
Show@Polyhedron@OctahedronD, Boxed � FalseD,
Show@Polyhedron@DodecahedronD, Boxed � FalseD,
Show@Polyhedron@IcosahedronD, Boxed � FalseD<,
8Show@Polyhedron@HexahedronD, Boxed � FalseD,
Show@Polyhedron@GreatDodecahedronD, Boxed � FalseD,
Show@Polyhedron@SmallStellatedDodecahedronD, Boxed � FalseD,
Show@Polyhedron@GreatStellatedDodecahedronD, Boxed � FalseD,
Show@Polyhedron@GreatIcosahedronD, Boxed � FalseD<<D;
Polyhedron[name, center, size] uses the given center and size.
44 Mathematica Graphics
Show@
8 Polyhedron@Dodecahedron, 84, 1, 0<, 2D,
Polyhedron@Icosahedron, 84, 2, 5<, .5D,
Stellate@Polyhedron@IcosahedronDD
< D;
Alternatively, the polyhedra can be used as functions in their own right, each of which can take center and size options. This allows the easier inclusion of other primitives and directives.
? Dodecahedron
Dodecahedron is a polyhedron with twelve faces
and twenty vertices, for use with polytope functions.
It may also be used with the Polyhedron function.
Mathematica Graphics 45
DisplayTogetherArray@8Show@
Graphics3D@Stellate@Dodecahedron@80, 0, 0<, 10DDD, Axes -> TrueD,
Show@Graphics3D@Stellate@Dodecahedron@810, 10, 10<, 10DDD,
Axes -> TrueD<D;
-10
0
10
-100
10
-10
0
10
-10
0
10-10
0
10
0
102030
010
2030
0
10
20
30
0
102030
0
10
20
30
The package also includes functions for operating on polyhedra, such as Stellate, Geodesate, Truncate, and OpenTruncate.
DisplayTogetherArray@8
Show@Stellate@Polyhedron@IcosahedronDD, Boxed � FalseD,
Show@Geodesate@Polyhedron@DodecahedronD, 3D, Boxed � FalseD,
Show@Truncate@Polyhedron@DodecahedronDD, Boxed � FalseD,
Show@OpenTruncate@Polyhedron@DodecahedronDD, Boxed � FalseD<D;
Notice in our examples that complex graphics objects are made up of several polygons. EdgeForm allows us to specify directions for the drawing of the edges of these polygons. EdgeForm[ ] draws no lines at all on the edges of the polygons, while EdgeForm[g], where g is a list of graphics directives, draws polygon edges in the styles defined by g.
46 Mathematica Graphics
Show@ Graphics3D@
8EdgeForm@D,
Dodecahedron@82, 2, 2<, 1.2D,
EdgeForm@[email protected], [email protected]<D,
Stellate@Icosahedron@80, 0, 0<, .5DD <
DD;
Lighting is an option to Show that can be True or False. In the default case, Lighting is True, and we control the lighting of the surface just as we do for three-dimensional plotting, by giving options such as LightSources to Show. Turning this option off allows us to control the shading of a surface via its intrinsic qualities. FaceForm[gfront, gback] allows the use of graphics directives for the front and back faces of each polygon. To see the back face of a polygon, we first remove some of the pieces of an object. The internal form of Dodecahedron[ ] is a list of polygon specifications. We choose part of this list just as we would take part of a list of numbers.
partialdodec = Drop@ Dodecahedron@ D, 84, 6< D;
Mathematica Graphics 47
Show@Graphics3D@8FaceForm@SlateGray, MaroonD, partialdodec< D,
Lighting -> False, Boxed -> FalseD;
The Polyhedra package also provides functions for accessing the coordinates of the vertices and the vertex numbers of the faces for the various polyhedra.
Vertices@DodecahedronD �� First
9$%%%%%%%%%%%%%%%%%%%%%%%%%1þþþþ2
-1
þþþþþþþþþþþþþþ2 �!!!!5 ,
1þþþþ2I3 -
�!!!!5 M, $%%%%%%%%%%%%%%%%%%%%%%%%%1þþþþ2
+1
þþþþþþþþþþþþþþ2�!!!!5 =
Faces@IcosahedronD �� First
81, 3, 2<
Polyhedron@OctahedronD �� First
8Polygon@880., 0., 1.41421<, 81.41421, 0., 0.<, 80., 1.41421, 0.<<D,Polygon@880., 0., 1.41421<, 80., 1.41421, 0.<, 8-1.41421, 0., 0.<<D,Polygon@880., 0., 1.41421<, 8-1.41421, 0., 0.<, 80., -1.41421, 0.<<D,Polygon@880., 0., 1.41421<, 80., -1.41421, 0.<, 81.41421, 0., 0.<<D,[email protected], 0., 0.<, 80., -1.41421, 0.<, 80., 0., -1.41421<<D,[email protected], 0., 0.<, 80., 0., -1.41421<, 80., 1.41421, 0.<<D,Polygon@880., 0., -1.41421<, 80., -1.41421, 0.<, 8-1.41421, 0., 0.<<D,Polygon@880., 1.41421, 0.<, 80., 0., -1.41421<, 8-1.41421, 0., 0.<<D<
48 Mathematica Graphics
3.3 Arrow
Arrow[start, finish] is a graphics primitive representing an arrow starting at start and ending at finish. Various options control the shape of the head of the arrow. We can control the proportions of the head of the arrow with HeadLength, HeadWidth, and HeadCenter, or we can specify an arbitrary graphic to use as the head of the arrow with HeadShape. HeadScaling controls the size of the head of the arrow, with default setting Automatic the head will be proportional to the length of the arrow. The setting Absolute will keep all arrowheads the same size.
Needs@"Graphics‘Arrow‘"D
Show@Graphics@Arrow@80, 0<, 81, 1<DDD;
Show@Graphics@
8Table@8Hue@Random@DD, Arrow@80, 0<, t 8Cos@tD, Sin@tD<D<,
8t, 0, 2 Pi, 0.25<D< D,
AspectRatio -> Automatic, PlotRange -> AllD;
Mathematica Graphics 49
Show@Graphics@
8Table@Arrow@8t, 0<, 8t, 1<,
HeadCenter -> t, HeadLength -> .2D,
8t, 0, 1, .2<D<D,
AspectRatio -> Automatic,
PlotRange -> 88-.25, 1.25<, All<D;
Since Arrow is a graphics primitive, it can be used with Epilog.
Plot@Sin@xD, 8x, 0, 2 p<,
Epilog � 8Arrow@84, .5<, 8p, 0<D, Text@"Hp,0L", 84.1, .6<D<D;
1 2 3 4 5 6
-1
-0.5
0.5
1
Hp,0L
3.4 Legend
The package Graphics‘Legend‘ provides the ability to create a legend and place it in any two-dimensional graphic. There are two ways to use legends. First, as an option to Plot and, second, as a graphics object that can be combined with any two-dimensional graphics object.
50 Mathematica Graphics
Needs["Graphics‘Legend‘"]
3.4.1Å Legends as a Plot Option
Specifying the option PlotLegend -> {text1, text2, … } in any plot command will produce a basic legend, with text for each curve plotted. If there are more curves than text, the text will be repeated cyclically.
Plot@8Sin@xD�x, 1�x<, 8x, -10, 10<,
PlotLegend -> 8"Sin@xD�x", "1�x"<D;
-10 -5 5 10
-2
-1
1
2
1�xSin@xD�x
The legend will incorporate styles assigned to the curves.
Plot@ 8Sin@xD�x, 1� x<, 8x, -10, 10<,
PlotStyle -> [email protected], [email protected]<D<,
PlotLegend -> 8Sin@xD �x, 1� x<D;
-10 -5 5 10
-2
-1
1
2
1þþþþx
Sin@xDþþþþþþþþþþþþþþþþþþ
x
The form of each texti can be altered by using StyleForm.
Mathematica Graphics 51
Plot@ 8Sin@xD�x, 1� x<, 8x, -10, 10<,
PlotStyle -> [email protected], [email protected]<D<,
PlotLegend ->
8StyleForm@Sin@xD�x,
FontFamily -> "Times",
FontSlant -> "Italic",
FontSize -> 14D,
StyleForm@1�x,
FontFamily -> "Helvetica",
FontWeight -> "Bold",
FontSize -> 14D<D;
-10 -5 5 10
-2
-1
1
2
1þþþþþ
x
Sin@xDþþþþþþþþþþþþþþþþþþ
x
There are numerous options that affect the way the legend is drawn. As the examples show, Mathematica will do a good job creating, sizing, and placing legends on its own, but there are many aspects that may be controlled. LegendPosition, LegendSize, LegendShadow, LegendOrientation, LegendLabel, LegendTextDirection, LegendTextOffset, LegendSpacing, LegendTextSpace, LegendLabelSpace, LegendBorderSpace, LegendBorder, and LegendBackground allow us to determine the look and placement of the legend. Following is an example with many of these options changed.
52 Mathematica Graphics
Plot@ 8Sin@xD�x, 1� x<, 8x, -10, 10<,PlotStyle -> 88RGBColor@1, 0, 0D<, [email protected]<D<<,PlotLegend ->
8StyleForm@Sin@xD� x,FontFamily -> "Times",
FontSlant -> "Italic", FontSize -> 14D,StyleForm@1�x,
FontFamily -> "Helvetica",
FontWeight -> "Bold", FontSize -> 12D<,LegendPosition -> 8-1, 0.15<,LegendSize -> 80.75, 0.8<,LegendShadow -> 80.075, 0.1<,LegendLabel ->
StyleForm@"Example", FontFamily -> "Times",
FontWeight -> "Bold", FontSize -> 18D,LegendBorder ->
[email protected], 1, 0.7D, [email protected]<,LegendBackground -> Yellow,
ShadowBackground -> SlateGray
D;
-10 -5 5 10
-2
-1
1
21þþþþþþ
x
Sin@xDþþþþþþþþþþþþþþþþþþ
x
Example
3.4.2Å Legends in Other Types of Graphics
The form ShowLegend[graphic, legend1, legend2, …] will place the legends specified in the graphic, where graphic is any graphic. Each legendi has the form {{{box1, text1}, … }, opts} where each boxi is a color primitive or graphic and each texti is an expression. A special form for a legend with n boxes, each colored according to the function colorfunction is {colorfunction, n, minstring, maxstring, opts}, where minstring and maxstring are optional text strings to be placed with the end boxes.
Mathematica Graphics 53
The next example uses this form. Since we know that ContourPlot shades each contour by mapping the function value onto GrayLevel, we can easily mimic this in the legend.
ShowLegend[ ContourPlot[ Sin[x y], {x, 0, Pi}, {y, 0, Pi}, ContourLines -> False,Contours -> 35, DisplayFunction -> Identity],
{GrayLevel[1-#]&, 10, " 1", "-1", LegendPosition -> {1.1, -.4}}];
0 0.5 1 1.5 2 2.5 30
0.5
1
1.5
2
2.5
3
-1
1
54 Mathematica Graphics
ShowLegend@DensityPlot@Sin@x2 - Cos@y2DD, 8x, -3, 3<, 8y, -3, 3<,
Mesh -> False,
PlotPoints-> 30, DisplayFunction-> IdentityD,8GrayLevel@1 - #D &, 10, " 1", "-1",
LegendPosition-> 81.1, -0.4<<D;
-3 -2 -1 0 1 2 3-3
-2
-1
0
1
2
3
-1
1
Needs["Graphics‘Colors‘"]
func[a_,b_,c_] := c/(b x)^(a Log[b x]);
Mathematica Graphics 55
ShowLegend[ Plot[{func[2,2,1], func[2,4,1], func[4,4,1]}, {x, 0.001, 1},
PlotStyle -> {Red, Green, Blue},PlotLabel -> StyleForm[c/(b x)^(a Log[b x]),
FontFamily -> "Helvetica", FontWeight -> "Bold", FontSize -> 12], DisplayFunction -> Identity],
{{{Red, "2,2,1"}, {Green, "2,4,1"}, {Blue, "4,4,1"}},LegendPosition -> {1, -.4}, LegendSize -> {1,1},LegendLabel -> StyleForm["Values of a, b, c",
FontFamily -> "Helvetica", FontWeight -> "Bold", FontSize -> 10]}];
0.2 0.4 0.6 0.8 1
0.2
0.4
0.6
0.8
1c Hb xL-a Log@b xD
4,4,1
2,4,1
2,2,1
Values of a, b, c
A legend may be created as a standalone graphics object by using the function Legend[legendargs, opts], where legendargs is of the form {{box1, text1}, … }. We may also generate just the graphics primitives describing the legend’s box and drop shadow with the command ShadowBox[pos, size, opts]. Options for ShadowBox are ShadowOffset, ShadowBackground, ShadowForeground, and ShadowBorder. Note that these options can also be included in calls to ShowLegend, PlotLegend, or Legend.
56 Mathematica Graphics
Exercises: Selected Graphics Packages
Í Load the package Graphics‘Master‘. This loads the name of every function defined in a graphics package into memory, and tells Mathematica to load the appropriate package when a package function is first used.
Needs["Graphics‘Master‘"]
Í Have Mathematica draw the default Cylinder. Draw another cylinder whose length is twice its diameter and use twice the number of polygons as Mathematica would by default. Use RotateShape to rotate this Cylinder off the vertical. Use AffineShape to deform the cylinder in any interesting way.
?Cylinder
Cylinder@Hr:1, h:1, Hn:20rLLD is a list of
n polygons approximating an open cylinder centeredaround the z-axis with radius r and half height h.
Show@Graphics3D@Cylinder@ DD, Boxed � FalseD;
Mathematica Graphics 57
Show@Graphics3D@Cylinder@1, 2, 40DD, Boxed � FalseD;
?RotateShape
RotateShape@graphics3D, phi, theta, psiD rotates the three-
dimensional graphics object by the specified Euler angles.
58 Mathematica Graphics
ShowAGraphics3DARotateShapeACylinder@1, 2, 40D, 0,
pþþþþ2,
pþþþþ2EE,
Boxed � FalseE;
?AffineShape
AffineShape@graphics3D, 8x, y, z<D multiplies
all coordinates of the three-dimensional graphics
object by the respective scale factors x, y, and z.
Show@Graphics3D@AffineShape@Cylinder@1, 2, 40D, 80.2, 0.5, 0.02<DD,Boxed � FalseD;
Í Do the same with any other shape.
Mathematica Graphics 59
Í Create a two-dimensional plot with two or more functions and attach a legend to it.
Plot@8x, x2, x3<, 8x, 0, 10<,PlotStyle -> [email protected], [email protected]<D, Hue@0D<,PlotLegend -> 8x, x^2, x^3<D;
2 4 6 8 10
25
50
75
100
125
150
x3
x2
x
Í Modify the legend in the previous exercise using the various Legend options.
Plot@8x, x2, x3<, 8x, 0, 10<,PlotStyle -> [email protected], [email protected]<D, Hue@0D<,PlotLegend -> 8x, x^2, x^3<,LegendPosition-> 81.1, -0.4<,LegendSize -> 80.9, 1<,LegendLabel -> StyleForm@"Exponents",FontFamily -> "Times", FontWeight -> "Bold", FontSize -> 18D,ShadowBackground-> [email protected];
2 4 6 8 10
25
50
75
100
125
150
x3
x2
x
Exponents
60 Mathematica Graphics
4Standard Add-on Packages
In addition to the graphics functions that are built into Mathematica, many of the functions defined in the standard add-on packages increase Mathematica’s graphical capabilities. These functions allow us to create additional types of graphics, simplify some plotting tasks, or enhance functions already defined in the Mathematica kernel.
This section will provide an overview of all the graphics functions contained in the standard add-on packages. For more in-depth coverage of these functions, consult the Help Browser or the text Mathematica 3.0 Standard Add-on Packages, which is included with most versions of Mathematica 3.0.
4.1 Graphics
This group of packages contains most of the graphics functions found in the standard add-on packages. It is important to note that we could load each package indivdually as we needed to use the functions in that package, but there is a more convenient way to do this automatically. This is through the use of master packages.
4.1.1Å Animation
This package contains various animation functions for regular x-y, density, contour, and parametric curve plots. Animation of three-dimensional plots and rotation of two-dimensional plots is also supported.
Evaluate the following code to see examples of animation functions available in the Animation package.
Needs@"Graphics‘Animation‘"D
Animate@Plot@Sin@x tD, 8x, -3, 3<, PlotRange� 8-1, 1<D, 8t, 0, 1<D
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
Mathematica Graphics 61
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
62 Mathematica Graphics
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
Mathematica Graphics 63
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
64 Mathematica Graphics
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
Mathematica Graphics 65
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
66 Mathematica Graphics
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
Mathematica Graphics 67
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
68 Mathematica Graphics
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-3 -2 -1 1 2 3
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
MoviePlot@Sin@x tD, 8x, -5, 5<, 8t, 0, 1<, PlotRange� 8-1, 1<
Syntax::bktmcp : Expression "MoviePlot@Sin@x tD,8x, -5, 5<, 8t, 0, 1<, PlotRange� 8-1, 1<" has no closing "D".
MoviePlot@Sin@x tD, 8x, -5, 5<, 8t, 0, 1<, PlotRange� 8-1, 1<
MoviePlot3D@Sin@x y tD, 8x, -2, 2<,8y, -2, 2<, 8t, 0, 1<, PlotRange� 8-1, 1<, Frames � 24D
Mathematica Graphics 69
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
70 Mathematica Graphics
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
Mathematica Graphics 71
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
72 Mathematica Graphics
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
Mathematica Graphics 73
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
74 Mathematica Graphics
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
Mathematica Graphics 75
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
76 Mathematica Graphics
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
Mathematica Graphics 77
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
78 Mathematica Graphics
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
Mathematica Graphics 79
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
80 Mathematica Graphics
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-2
-1
0
1
2
-2
-1
0
12
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
MovieParametricPlotA8Sin@x tD, Cos@x tD<, 8x, -p, p<, 9t, 0, 1,1
þþþþþþ11
=,PlotRange� 88-1, 1<, 8-1, 1<<, AspectRatio� 1E
Mathematica Graphics 81
-1 -0.75 -0.5 -0.25 0.25 0.5 0.75 1
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-1 -0.75 -0.5 -0.25 0.25 0.5 0.75 1
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
82 Mathematica Graphics
-1 -0.75 -0.5 -0.25 0.25 0.5 0.75 1
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-1 -0.75 -0.5 -0.25 0.25 0.5 0.75 1
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
Mathematica Graphics 83
-1 -0.75 -0.5 -0.25 0.25 0.5 0.75 1
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-1 -0.75 -0.5 -0.25 0.25 0.5 0.75 1
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
84 Mathematica Graphics
-1 -0.75 -0.5 -0.25 0.25 0.5 0.75 1
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-1 -0.75 -0.5 -0.25 0.25 0.5 0.75 1
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
Mathematica Graphics 85
-1 -0.75 -0.5 -0.25 0.25 0.5 0.75 1
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-1 -0.75 -0.5 -0.25 0.25 0.5 0.75 1
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
86 Mathematica Graphics
-1 -0.75 -0.5 -0.25 0.25 0.5 0.75 1
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
-1 -0.75 -0.5 -0.25 0.25 0.5 0.75 1
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
SpinShow@Plot3D@Sin@x yD, 8x, -2, 2<, 8y, -2, 2<, Axes � None, Boxed � FalseDD;
Mathematica Graphics 87
88 Mathematica Graphics
Mathematica Graphics 89
90 Mathematica Graphics
Mathematica Graphics 91
92 Mathematica Graphics
Mathematica Graphics 93
94 Mathematica Graphics
Mathematica Graphics 95
96 Mathematica Graphics
Mathematica Graphics 97
98 Mathematica Graphics
Mathematica Graphics 99
4.1.2Å ArgColors
This package provides functions for specifying the color or gray level of graphics elements using the argument of a complex number.
Needs@"Graphics‘ArgColors‘"D
func@z_D := ExpA 2 p É zþþþþþþþþþþþþþþþ10
E
circ1 =
Graphics@Table@8ArgShade@func@zDD,
Disk@8Re@func@zDD, Im@func@zDD<, 0.2D<,8z, 10<D, AspectRatio� AutomaticD;
circ2 =
Graphics@Table@8ArgColor@func@zDD,
Disk@8Re@func@zDD, Im@func@zDD<, 0.2D<,8z, 10<D, AspectRatio� AutomaticD;
circ3 =
Graphics@Table@8ColorCircle@func@zDD,
Disk@8Re@func@zDD, Im@func@zDD<, 0.2D<,8z, 10<D, AspectRatio� AutomaticD;
100 Mathematica Graphics
Show[GraphicsArray[ {circ1, circ2, circ3} ]];
4.1.3Å Colors
This package provides functions that convert a color expressed in one of several color systems to an RGBColor. It also allows you to find the appropriate RGBColor for a particular color name (such as Blue).
Needs@"Graphics‘Colors‘"D
The following example code will display a vertical listing of the name and a swatch of each color in the Colors package.
Use this definition for colors for ten random colors.
colors = Sort@Table@AllColors@@Random@Integer, 81, Length@AllColorsD<DDD, 810<DD;
Use this initial value for colors if you want an alphabetical listing of the all colors available.
colors = AllColors;
Use these two lines to create a listing of all colors available sorted by their CMYKColor value.
colors = H8#1, ToColor@ToExpression@#1D, CMYKColorD< &L �ë AllColors;
colors = First@Transpose@Sort@colors, OrderedQ@8#1P2T, #2P2T<D &DDD;
The rest of this code can be used by either value of colors above to display the color names and swatches.
Mathematica Graphics 101
colors = Table@8ToExpression@colorsPiTD,Rectangle@80, -10 i<, 815, -10 i - 10<D, RGBColor@0, 0, 0D,
Text@colorsPiT, 820, -10 i - 5<, 8-1, 0<D<, 8i, Length@colorsD<D;Show@Graphics@colorsD,
AspectRatio� Automatic,
PlotRange� 880, 80<, 80, -HLength@colorsD 10 + 10L<<D;
SmokeSnowFloralOldLaceCornsilkLemonChiffonIvoryTitaniumLightYellowZincSeashellLinenPapayaWhipAntiqueLavenderBlushBlanchedAlmondEggshellBisqueMoccasinMistyRoseNavajoPeachPuffPinkLightPinkHotPinkMediumVioletRedWhiteYellowCadmiumLemonGoldDarkGoldenrodGoldenrodLightCadmiumYellowDeepNaplesYellowAureolineYellowRawUmberBrickCadmiumYellowCarrotDarkOrangeYellowOchreGoldOchreOrangeMarsYellowRawSiennaChocolateSaddleBrownDeepOchreMarsOrangeCadmiumOrangeBrownOchreVanDykeBrownBurntSiennaOrangeRedApricotSepiaGreenishUmberRaspberryDeepPinkMaroonPermanentRedVioletBrownAlizarinCrimsonRoseMadderGeraniumLakeDeepMadderLakeBrownMadderBurntUmberFirebrickIndianRedLightCadmiumRedRedDeepCadmiumRedVenetianRedEnglishRedTomatoOakCoralLightSalmonGhostSalmonWheatVioletRedMintCreamLightBeigeSandyBrownAliceBlueKhakiLightCoralLightGoldenrodAzurePaleGoldenrodHoneydewGainsboroBananaMelonBurlywoodPaleVioletRedYellowBrownLavenderPeruThistleCyanWhiteGrayPlumRosyBrownDarkKhakiSiennaLightGrayOrchidBeigeMagentaChromeOxideGreenLightSteelBlueGreenYellowLightBluePowderBlueWarmGrayPaleTurquoiseMintMediumOrchidYellowGreenOliveDrabChartreusePeachLawnGreenVioletColdGrayLightSlateGrayDeepCobaltVioletDarkSeaGreenLightSkyBlueSlateGrayTerreVerteMediumPurpleSkyBlueDarkOrchidDimGrayPurpleDarkOliveGreenIvoryBlackDarkVioletCinnabarGreenBlackPaleGreenLightSlateBlueBlueVioletMediumSlateBlueCornflowerBlueUltramarineVioletSlateBlueCadetBlueSapGreenOliveAquamarineGreenLightViridianMediumAquamarinePrussianBlueLampBlackSteelBlueDarkSlateBlueDarkSlateGrayLimeGreenCobaltCobaltGreenRoyalBlueDodgerBlueDeepSkyBluePeacockCeruleanMediumTurquoiseDarkTurquoiseLightSeaGreenCyanManganeseBlueTurquoiseSeaGreenMediumSeaGreenTurquoiseBlueEmeraldGreenForestGreenMediumSpringGreenPermanentGreenSpringGreenDarkGreenIndigoMidnightBlueBlueMediumBlueUltramarineNavyNavyBlue
4.1.4Å ComplexMap
This package plots the images of Cartesian coordinate lines and polar coordinate lines under a user-supplied function f.
Needs@"Graphics‘ComplexMap‘"D
102 Mathematica Graphics
DisplayTogetherArrayA9CartesianMap@Cos, 80.2, p - 0.2<, 8-2, 2<D,CartesianMap@Zeta, 80.1, 0.9<, 80, 20<D,CartesianMapA 1
þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþConjugate@#1D &, 9-2, 2,
4þþþþþþ19
=, 9-2, 2,4
þþþþþþ19
=,PlotRange� AllE=E;
-3-2-1 1 2 3
-3-2-1
123
-2 -1 1 2 3
-2
-1
1
-7.5-5-2.52.557.5
-7.5-5
-2.5
2.55
7.5
DisplayTogetherArrayA9PolarMap@Log, 80.1, 10, 0.5<, 8-3, 3, 0.15<D,PolarMapA 1
þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþConjugate@#1D &, 80.1, 5.1, 0.5<, 9-p, p,
pþþþþþþ12
=E,PolarMap@Sqrt, 81<, 8-p - 0.0001, p<D,PolarMapAIdentity, 81, 2<, 9-p, p,
pþþþþþþ12
=, Frame � TrueE=E;
-2-1 1 2
-3-2-1
123
-1-0.50.51
-1-0.5
0.51
0.20.40.60.81
-1
-0.5
0.5
1
-2-1 0 1 2-2-1012
4.1.5Å ContourPlot3D
This package introduces ContourPlot3D and ListContourPlot3D, the three-dimensional analogs of the built-in functions ContourPlot and ListContourPlot.
Needs@"Graphics‘ContourPlot3D‘"D
Mathematica Graphics 103
DisplayTogetherArrayA9ContourPlot3D@x y z, 8x, -1, 1<, 8y, -1, 1<, 8z, -1, 1<,
Contours � 8.1<D,ContourPlot3DA�!!!!!!!!!!!!!!!!!!!!!!x2 + y2 + z2 , 8x, -2, 2<, 8y, -2, 2<, 8z, -2, 2<,
Contours � 81.<E,ContourPlot3DA
SinA�!!!!!!!!!!!!!!!!!!!!!!x2 + y2 + z2 E, 8x, -2, 2<, 8y, 0, 2<, 8z, -2, 2<,Contours � 80.75<E=E;
data = Table@x2 + 2 y2 + 3 z2,
8z, -1, 1, 0.25<, 8y, -1, 1, 0.25<, 8x, -1, 1, 0.25<D;ListContourPlot3D@data,
MeshRange� 88-1, 1<, 8-1, 1<, 8-1, 1<<, Contours� 83.<D;
104 Mathematica Graphics
4.1.6Å FilledPlot
This package allows us to fill the space between a plotted function and the x-axis or between a pair of plotted functions with a color.
Needs@"Graphics‘FilledPlot‘"D
DisplayTogetherArrayA9FilledPlotA Sin@xD
þþþþþþþþþþþþþþþþþx
, 8x, -10, 10<E,
FilledPlotA9 Sin@xDþþþþþþþþþþþþþþþþþ
x,
Cos@xDþþþþþþþþþþþþþþþþþ
x,
1þþþþx=, 8x, -10, 10<E,
FilledPlotA9 x2þþþþþþ18
, Cos@xD, Sin@xD=, 8x, 0, 2 p<,Fills � 8881, Axis<, [email protected]<, 882, 3<, [email protected]<<,Curves � FrontE=E;
-10 -5 5 10-0.2
0.20.40.60.81
-10 -5 5 10-0.2
0.20.40.60.81
-10-5 5 10-1
-0.5
0.51
-10-5 5 10-1
-0.5
0.51
1 2 3 4 5 6-1-0.50.51
1.52
1 2 3 4 5 6-1-0.50.51
1.52
FilledListPlot[{2, 3, 1, 5, 4}];
2 3 4 5
1
2
3
4
5
2 3 4 5
1
2
3
4
5
Mathematica Graphics 105
DisplayTogetherArray@8Show@Graphics@
[email protected], Polygon@880, 0<, 81, 0<, 81, 1<<D<,AxesFront� False, Axes � True, AxesOrigin� 80.5, 0.5<DD,
Show@Graphics@[email protected], Polygon@880, 0<, 81, 0<, 81, 1<<D<,
AxesFront� True, Axes � True, AxesOrigin� 80.5, 0.5<DD<D;
0 0.2 0.4 0.6 0.8 1
0
0.2
0.4
0.6
0.8
1
0 0.2 0.4 0.6 0.8 1
0
0.2
0.4
0.6
0.8
1
0 0.2 0.4 0.6 0.8 1
0
0.2
0.4
0.6
0.8
1
4.1.7Å Graphics
This package provides special functions for plotting in two dimensions. Special formats include bar charts, pie charts, log plots, polar plots, and error bar plots.
Needs@"Graphics‘Graphics‘"D
LinearScale@1, 2D
881., 1<, 81.2, 1.2<, 81.4, 1.4<, 81.6, 1.6<, 81.8, 1.8<, 82., 2<<
Short@ LogScale@1, 10D, 10 D
881., 10<, 83., 1000<, 85., 100000.<, 87., 1.�107<, 89., 1.�109<,÷27ø, 88.65321, , 80.00375, 0.<, [email protected]<<,88.74819, , 80.00375, 0.<, [email protected]<<,88.82607, , 80.00375, 0.<, [email protected]<<,88.89209, , 80.00375, 0.<, [email protected]<<,88.94939, , 80.00375, 0.<, [email protected]<<<
UnitScale@2, 10, 0.7D
882.8, 2.8<, 84.2, 4.2<, 85.6, 5.6<, 87., 7.<, 88.4, 8.4<, 89.8, 9.8<<
PiScale@0, 10D
980., 0<, 91.5708, pþþþþ2=, 83.14159, p<, 94.71239, 3 p
þþþþþþþþ2
=,
86.28319, 2 p<, 97.85398, 5 pþþþþþþþþ2
=, 89.42478, 3 p<=
106 Mathematica Graphics
DisplayTogetherArray@8TextListPlot@
881.5, 2.5<, 81.6, 2.6<, 81.7, 2.7<, 81.8, 2.8<<,TextStyle -> 8FontSize -> 6<D,
[email protected], 2.5, 4<, 81.6, 2.6, 3<, 81.7, 2.7, 2<, 81.8, 2.8, 1<<,
TextStyle -> 8FontSize -> 6<D,[email protected], 2.5, 1<, 81.6, 2.6, 2<, 81.7, 2.7, 3<, 81.8, 2.8, 4<<,
TextStyle -> 8FontSize -> 6<D<D;
1.551.61.651.71.751.8
2.552.62.652.72.752.8
1
2
3
4
1.551.61.651.71.751.8
2.552.6
2.652.7
2.752.8
4
3
2
1
1.551.61.651.71.751.8
2.552.62.652.72.752.8
1
2
3
4
DisplayTogetherArray@88LogPlot@Sin@xD, 8x, 0.1, 3.1<D,
LogPlot@Exp@4 xD, 8x, 1, 5<, Frame � TrueD<,8LogPlot@Exp@2 xD, 8x, 1, 5<,
Frame � True, GridLines� 8Automatic, LogGridMajor<D,LogPlot@Exp@4 xD, 8x, 1, 3<,
Frame � True, GridLines� 8Automatic, LogGridMinor<D<<D;
1 2 3 4 510501005001000500010000
1 1.5 2 2.5 3100500100050001000050000100000.
0 0.5 1 1.5 2 2.5 3
0.10.150.20.30.50.71
1 2 3 4 5100
10000
1.�1061.�108
Mathematica Graphics 107
DisplayTogetherArrayA9LogListPlotATableA9 i
þþþþ2, i2=, 8i, 20<EE,
LogLogPlot@Sin@xD, 8x, 0.1, 3.1<D,LogLogListPlot@Table@8i2, i3<, 8i, 10<DD=E;
0 2 4 6 810151050100
11.52 3
0.10.150.20.30.50.71
12 51020501001510501005001000
DisplayTogetherArrayA9PolarPlot@8Cos@tD, Sin@2 tD<, 8t, 0, 2 p<D,PolarListPlotATableA9 t
þþþþ2, Cos@tD=, 8t, 0, 2 p, 0.1<EE=E;
-0.75-0.5-0.25 0.250.50.751
-0.75
-0.5
-0.25
0.25
0.5
0.75
0.511.52
-1
1
2
DisplayTogetherArrayA9ErrorListPlotATableA9i, �!!!i =, 8i, 10<EE,ErrorListPlot@Table@8Sin@tD, Cos@tD, t<, 8t, 10<DD=E;
2 4 6 8 10
24681012
-1 -0.5 0.5 1
-10
-5
5
108 Mathematica Graphics
data = TableA9 nþþþþþþ15
, I nþþþþþþ15
M2 + 2 + Random@Real, 8-0.3, 0.3<D=, 8n, 15<E;datafit = Fit@data, 81, x, x2<, xD;ListAndCurvePlot@data, datafit, 8x, 0, 1<D;
0.2 0.4 0.6 0.8 11.8
2.2
2.4
2.6
2.8
3
3.2
DisplayTogetherArray@8BarChart@Table@i, 8i, 1, 10<DD,BarChart@Table@8Sin@tD, Sin@tD<, 8t, 0.6‘3, 3‘3, 0.6‘3<DD<D;
1 2 3 4 5 6 7 8 910
246810
0.5650.9320.9740.680.14
0.20.40.60.8
DisplayTogetherArray@8PieChart@Table@i, 8i, 5<DD,PieChart@Table@8i, A@iD<, 8i, 7<DD<D;
1
23
4
5
A@1DA@2D
A@3DA@4D
A@5D
A@6D A@7D
Mathematica Graphics 109
DisplayTogetherArray@[email protected], .3, .1<D,[email protected], .3, .1<, PieExploded� AllD,[email protected], .3, .1<, PieExploded� 83, .2<D<D;
1
2 3
1
2 3
1
2 3
PlotStyle@Plot@Sin@xD, 8x, 0, p<DD
0.5 1 1.5 2 2.5 3
0.2
0.4
0.6
0.8
1
8<
110 Mathematica Graphics
PlotStyle@Plot@Sin@xD, 8x, 0, p<,PlotStyle� [email protected], .02<D, [email protected]<<DD
0.5 1 1.5 2 2.5 3
0.2
0.4
0.6
0.8
1
[email protected], 0.02<D, [email protected]<<
? TransformGraphics
TransformGraphics@graphics, fD applies thefunction f to all lists of coordinates in graphics.
g1 = Plot@Sin@tD, 8t, 0, 2 p<, DisplayFunction� IdentityD;g2 = TransformGraphics@g1, HSin@#D + Cos@#DL &D;Show@GraphicsArray@8g1, g2<DD;
1 2 3 4 5 6
-1
-0.5
0.5
1
-1 -0.5 0.5 1-0.25
0.250.50.75
11.25
g1 = Plot@Sin@tD, 8t, 0, p<, DisplayFunction� IdentityD;g2 = SkewGraphics@g1, 881, 2<, 80, 1<<D;Show@GraphicsArray@8g1, g2<DD;
0.5 1 1.5 2 2.5 3
0.20.40.60.81
2.252.52.75 3.253.53.75
0.20.40.60.81
Mathematica Graphics 111
4.1.8Å Graphics3D
This package provides special functions for plotting in three dimensions. Special formats include bar charts, scatter plots, surface plots, shadow plots, and projections.
Needs@"Graphics‘Graphics3D‘"D
Needs@"Graphics‘ParametricPlot3D‘"Dg1 = CylindricalPlot3D@r2, 8r, 0, 1<, 8f, 0, 2 p<,
DisplayFunction� IdentityD;g2 = TransformGraphics3D@g1, Cos@#1D &D;Show@GraphicsArray@8g1, g2<DD;
-1-0.5
00.5
1 -1-0.500.51
00.250.5
0.751
-1-0.5
00.5
10.60.70.80.9 1
0.60.70.80.9
1
0.60.70.80.91
0.60.70.80.9 1
0.60.70.80.9
1
Show@Graphics3D@Plot@Sin@tD, 8t, 0, p<DDD;
0.5 1 1.5 2 2.5 3
0.2
0.4
0.6
0.8
1
112 Mathematica Graphics
0 1 2 3
00.250.50.75
1
DisplayTogetherArray@8CylindricalPlot3D@r2, 8r, 0, 1<, 8f, 0, 2 p<,
Axes -> FalseD,Show@SkewGraphics3D@g1, 881, 2, 0<, 80, 1, 0<, 80, 0, 1<<D,
Axes -> FalseD<D;
Mathematica Graphics 113
g1 = Table@Plot@xn, 8x, 0, 5<,TextStyle -> 8FontSize -> 6<, DisplayFunction� IdentityD, 8n, 4<D;
DisplayTogetherArray@8Show@GraphicsArray@Partition@g1, 2DDD,Show@StackGraphics@g1DD<D;
1 2 3 4 5
204060
1 2 3 4 5
20406080100120
1 2 3 4 5
12345
1 2 3 4 5
510152025
0240
10203040
0240
10203040
BarChart3D@881, 2, 3<, 84, 5, 6<<D;
0.5
1
1.5
2
2.5
1
2
3
0
2
4
6
0.5
1
1.5
2
2.5
0
2
4
6
114 Mathematica Graphics
DisplayTogetherArray@8ScatterPlot3D@Table@8t, Sin@tD, Cos@tD<, 8t, 0, 10, 0.1<D,
Axes � FalseD,ScatterPlot3D@Table@8t, Sin@tD, Cos@tD<, 8t, 0, 10, 0.1<D,
PlotJoined� True, Axes � FalseD<D;
ListSurfacePlot3D@Table@8i, j, Sin@i - Cos@jDD<, 8i, 1, 10, .5<, 8j, 1, 10, .5<DD;
g1 = ParametricPlot3D@8x, Cos@tD Sin@xD, Sin@tD Sin@xD<,8x, -p, p<, 8t, 0, 2 p<,Axes � False, DisplayFunction� IdentityD;
g2 = Shadow@g1, ZShadow � False, DisplayFunction� IdentityD;g3 = Show@Project@g1, 80, 1, 0<D, DisplayFunction� IdentityD;Show@GraphicsArray@8g1, g2, g3<D, DisplayFunction� $DisplayFunctionD;
Mathematica Graphics 115
4.1.9Å ImplicitPlot
The built-in function Plot requires one to specify an explicit function. Many simple graphs (e.g., circles, ellipses, etc.) are not functions. ImplicitPlot allows one to plot figures defined by equations.
Needs@"Graphics‘ImplicitPlot‘"D
DisplayTogetherArrayA99ImplicitPlot@x2 + y2 == x y + 3, 8x, -3, 3<D,ImplicitPlotAHx2 + y2L2 == x2 - y2, 8x, -2, 2<E,ImplicitPlot@x3 + y3 == 3 x y, 8x, -3, 3<D,ImplicitPlot@x2 + y2 == 3 x y + 3,
8x, -10, 10<, PlotRange� 88-10, 10<, 8-10, 10<<D=,9ImplicitPlotA9Hx2 + y2L2 == x2 - y2, Hx2 + z2L2 == 2 x z=, 8x, -2, 2<,
PlotStyle� 8GrayLevel@0D, [email protected]<D<E,ImplicitPlot@x2 + y2 == p, 8x, -2, 2<D,ImplicitPlot@
Sin@2 xD + Cos@3 yD == 1, 8x, -2 p, 2 p<, 8y, -2 p, 2 p<D,ImplicitPlot@x3 + x y + y2 == 1, 8x, -2 p, 2 p<, 8y, -2 p, 2 p<D==E;
-1-0.5 0.51
-0.75-0.5-0.25
0.250.50.75
-1.5-1-0.50.511.5
-1.5-1
-0.5
0.51
1.5
-6-4-20246-6-4-20246
-6-4-20246-6-4-20246
-2-1 1 2
-2
-1
1
2
-1-0.5 0.51-0.3-0.2-0.10.10.20.3 -3-2-1 1 2 3
-4-3-2-1
12
-10-7.5-5-2.52.557.510
-10-7.5-5
-2.52.55
7.510
4.1.10Å MultipleListPlot
This package contains a function for plotting multiple sets of data in a graph and related utilities.
Needs@"Graphics‘MultipleListPlot‘"D
list1 = TableA8x, Sin@xD<, 9x, 0, 2 p,pþþþþ6=E;
list2 = TableA8x, Cos@xD<, 9x, 0, 2 p,pþþþþ6=E;
MultipleListPlot@list1, list2, PlotJoined -> True,D;
MultipleListPlot::badargs :
MultipleListPlot has been given bad arguments. Input should be data
sets consisting of points or points with ErrorBar specifications.
116 Mathematica Graphics
MultipleListPlot@82,81.5, 3.2<,82.5, [email protected]<,884.4, 5.2<, [email protected], 0.3<D<,885.5, 2.1<, [email protected], 0.3<, 8-0.2, 0.5<D<<,
Frame -> TrueD;
1 2 3 4 5
2
2.5
3
3.5
4
4.5
5
5.5
Show@Graphics@RegularPolygon@10, 1, 80, 0<, 0, 3DD,AspectRatio -> AutomaticD;
Mathematica Graphics 117
Show@Graphics@MapIndexed@8#1, Line@880, -First@#2D<, 81, -First@#2D<<D< &,
8Dashing@8Dot<D,Dashing@8Dot, Dash<D,Dashing@8Dot, Dash, Dot, LongDash<D,Dashing@8Dot, Dot, Dash<D<DDD;
4.1.11Å ParametricPlot3D
This package allows us to specify the step size of the grid used by ParametricPlot3D. In contrast, the built-in function ParametricPlot3D requires that the grid be specified using the option PlotPoints. The package also introduces the functions PointParametricPlot3D, SphericalPlot3D, and CylindricalPlot3D.
Needs@"Graphics‘ParametricPlot3D‘"D
118 Mathematica Graphics
DisplayTogetherArrayA99ParametricPlot3D@
8x, y, x y<, 8x, -1, 1, 0.2<, 8y, -1, 1<, Axes � FalseD,ParametricPlot3DA9Cos@tD, Sin@tD, t
þþþþ6=,
9t, 0, 4 p,p
þþþþþþ12
=, Axes � FalseE,
CylindricalPlot3DAr2, 8r, 0, 1<, 9f, -pþþþþ4,
5 pþþþþþþþþ4
=,Axes � False, ViewPoint� 81.3, -2.4, 1.6<E=,
9SphericalPlot3DASin@qD2, 8q, 0, p<, 9f, 0,3 pþþþþþþþþ2
=, Axes � FalseE,CylindricalPlot3DA
1.5�!!!!!!!!!!!!1 + r2 , 8r, 0, 2<, 8f, 0, 2 p<, Axes � FalseE,
ParametricPlot3D@8Cosh@zD Cos@fD, Cosh@zD Sin@fD, z<,8z, -2, 2<, 8f, 0, 2 p<, Axes � FalseD==E;
4.1.12Å PlotField
This package does plots of vector fields in the plane. PlotVectorField allows us to specify the functions describing the two components of the field. PlotGradientField and PlotHamiltonianField plot the respective vector fields associated with a scalar function. PlotPolyaField plots the field associated with a complex-valued function. ListPlotVectorField plots a rectangular array of vectors.
Needs@"Graphics‘PlotField‘"D
Mathematica Graphics 119
DisplayTogetherArray@8PlotVectorField@8Sin@x yD, Cos@x yD<, 8x, 0, p<, 8y, 0, p<D,PlotGradientField@x3 + y4, 8x, 0, 10<, 8y, 0, 10<D,PlotPolyaField@Hx + I yL4, 8x, 5, 10<, 8y, 5, 10<D<D;
4.1.13Å PlotField3D
This package does plots of vector fields in three dimensions. PlotVectorField3D allows us to specify the functions describing the three components of the field. PlotGradientField3D plots the gradient vector field associated with a scalar function. ListPlotVectorField3D plots a three-dimensional array of vectors.
Needs@"Graphics‘PlotField3D‘"D
DisplayTogetherArrayA9PlotVectorField3DA
8y, -x, 0<þþþþþþþþþþþþþþþþþþþþþþþþþ
z, 8x, -1, 1<, 8y, -1, 1<, 8z, 1, 3<E,
PlotGradientField3D@x y z, 8x, -1, 1<, 8y, -1, 1<, 8z, -1, 1<D,ListPlotVectorField3D@
Flatten@Table@88i, j, k<,8Random@Real, 8-1, 1<D, Random@Real,
8-1, 1<D, Random@Real, 8-1, 1<D<<, 8i, 7<, 8j, 7<, 8k, 7<D, 2DD=E;
4.1.14Å Shapes
This package provides lists of polygons for some common three-dimensional shapes. Functions for translating, rotating, and affine transformations of Graphics3D objects and converting a Graphics3D object to its wire frame representation are also included.
Needs@"Graphics‘Shapes‘"D
120 Mathematica Graphics
DisplayTogetherArrayA98Show@Graphics3D@[email protected], 0.5DD, Boxed � FalseD,Show@Graphics3D@Torus@2, 0.7, 15, 14DD, Boxed � FalseD,Show@Graphics3D@Sphere@DD, Boxed � FalseD,Show@Graphics3D@MoebiusStrip@2, 1, 80DD, Boxed � FalseD,Show@Graphics3D@Helix@DD, Boxed � FalseD<,9Show@Graphics3D@DoubleHelix@DD, Boxed � FalseD,ShowARotateShapeA
Graphics3D@MoebiusStrip@DD, pþþþþ4,
pþþþþ3,
pþþþþ2E, Boxed � FalseE,
ShowATranslateShapeARotateShapeAGraphics3D@MoebiusStrip@DD, p
þþþþ4,
pþþþþ3,
pþþþþ2E, 81, 2, 3<E,
RotateShapeAGraphics3D@MoebiusStrip@DD, pþþþþ4,
pþþþþ3,
pþþþþ2E,
Boxed � FalseE,Show@AffineShape@Graphics3D@Cone@DD, 81, 2, 3<D, Boxed � FalseD,Show@WireFrame@Graphics3D@Cone@DDD, Boxed � FalseD==E;
4.1.15Å Spline
This package introduces a Spline graphics primitive and provides utilities for rendering splines.
Needs@"Graphics‘Spline‘"D
Mathematica Graphics 121
pts = 880, 0<, 81, 2<, 8-1, 3<, 80, 1<, 83, 0<<;DisplayTogetherArray@8
Show@Graphics@8Hue@0D, Line@ptsD, GrayLevel@0D, Spline@pts, CubicD<D,PlotRange � AllD,
Show@Graphics@8Hue@0D, Line@ptsD, GrayLevel@0D,Spline@pts, BezierD<D, PlotRange � AllD,Show@Graphics@Spline@pts, CompositeBezier,
SplineDots� AutomaticDDD<D;
4.1.16Å SurfaceOfRevolution
This package provides functions for plotting surfaces generated by revolving a curve about an axis. The curve may be expressed as a function, parametrically, or as a list of points. Rotation can be about an arbitrary axis.
Needs@"Graphics‘SurfaceOfRevolution‘"D
DisplayTogetherArrayA99SurfaceOfRevolution@Sin@xD, 8x, 0, 2 p<D,
SurfaceOfRevolutionA81.1 Sin@uD, u2<, 9u, 0,
3 pþþþþþþþþ2
=, BoxRatios -> 81, 1, 2<E=,9SurfaceOfRevolution@x2, 8x, 0, 1<, RevolutionAxis-> 81, 1, 1<D,
ListSurfaceOfRevolutionATable@8n, n3<, 8n, 0, 1, 0.1<D, 9t, 0,
pþþþþ2=,
RevolutionAxis-> 81, -1, 1<, PlotRange -> AllE==E;
00.5
1
00.5
1
00.5
1
00.5
1
00.5
1
00.250.50.751
-0.6-0.4-0.200
0.5
1
00.250.50.751
0
0.5
1
-50
5-5
0
5-1-0.500.51
-50
5
-1-0.500.51-1-0.500.51
05
101520-1-0.500.51
122 Mathematica Graphics
4.1.17Å ThreeScript
This package provides a utility for converting three-dimensional graphical objects from Mathematica to the 3-Script file format and writing them into files (or suitable operating system pipes).
Needs@"Graphics‘ThreeScript‘"D
obj = Graphics3D@Polygon@880, 0, 0<, 80, 1, 0<, 80, 1, 1<<DD
ú Graphics3D ú
ThreeScript@"object.ts", objD
object.ts
! ! object.ts
4.2 DiscreteMath
4.2.1Å Combinatorica
This package defines over 230 combinatoric and graph theory functions. It includes functions for constructing graphs and other combinatorial objects, computing invariants of these objects, and finally displaying them.
The package contains all the programs from the book, Implementing Discrete Mathematics: Combinatorics and Graph Theory with Mathematica, by Steven S. Skiena, Addison-Wesley Publishing Co., Advanced Book Program, 350 Bridge Parkway, Redwood City CA 94065. ISBN 0-201-50943-1. For ordering information, call 1-800-447-2226.
The programs from the book, Implementing Discrete Mathematics: Combinatorics and Graph Theory with Mathematica, are available by anonymous ftp.cs.sunysb.edu in the pub/Combinatorica directory.
Any comments, bug reports, or requests to get on the Combinatorica mailing list should be forwarded to:
Steven SkienaDepartment of Computer ScienceState University of New YorkStony Brook, NY 11794
(516) 632-9026 / 8470
Mathematica Graphics 123
Needs@"DiscreteMath‘Combinatorica‘"D
Star::shdw : Symbol Star appears in multiple contexts
8DiscreteMath‘Combinatorica‘, Graphics‘MultipleListPlot‘<;definitions in context DiscreteMath‘Combinatorica‘ may
shadow or be shadowed by other definitions.
Vertices::shdw : Symbol Vertices appears in multiple
contexts 8DiscreteMath‘Combinatorica‘, Geometry‘Polytopes‘<;definitions in context DiscreteMath‘Combinatorica‘ may
shadow or be shadowed by other definitions.
c1 = FerrersDiagram@RandomPartition@100DD;
124 Mathematica Graphics
c2 = ShowGraph@CompleteGraph@5DD;
c3 = ShowGraph@RankedEmbedding@GridGraph@5, 5D, 813<DD;
Mathematica Graphics 125
c4 = ShowGraph@LineGraph@CompleteGraph@5DDD;
c5 = ShowGraph@CirculantGraph@21, RandomKSubset@Range@10D, 3DDD;
126 Mathematica Graphics
c6 = ShowGraph@OrientGraph@Wheel@10DD, DirectedD;
c7 = ShowLabeledGraph@MakeGraph@Range@8D, HMod@#1, #2D == 0L ⅅ
1
2
3
4
5
6
7
8
Mathematica Graphics 127
c8 = ShowGraph@MinimumSpanningTree@CompleteGraph@6, 6, 6DDD;
Show@GraphicsArray@88c1, c2, c3, c4<, 8c5, c6, c7, c8<<DD;
12
3
4
56
7
8
4.2.2Å ComputationalGeometry
This package implements selected functions from computational geometry relevent to nearest neighbor point location, including triangulation.
Needs@"DiscreteMath‘ComputationalGeometry‘"D
128 Mathematica Graphics
input2D = 884.4, 14<, 86.7, 15.25<, 86.9, 12.8<, 82.1, 11.1<,89.5, 14.9<,
813.2, 11.9<, 810.3, 12.3<, 86.8, 9.5<,83.3, 7.7<,
8.6, 5.1<, 85.3, 2.4<, 88.45, 4.7<,811.5, 9.6<,
813.8, 7.3<, 812.9, 3.1<, 811, 1.1<<;input3D = MapA9#1P1T, #1P2T, "###########################################################################
64 - HH#1P1TL - 8L2 - HH#1P2TL - 8L2 = &,
input2DE;
DisplayTogetherArray@88PlanarGraphPlot@input2D, ConvexHull@input2DDD,
PlanarGraphPlot@input2DD<,8DiagramPlot@input2DD,
TriangularSurfacePlot@input3DD<<D;
123
4
5
67
89
10
1112
1314
1516
10
4
9
1
11
2
8
3
12
5
7
16
13
15
6
14
10
4
9
1
11
2
8
3
12
5
7
16
13
15
6
14
4.2.3Å Tree
This package introduces functions for creating, searching and displaying trees represented as nested lists.
Needs@"DiscreteMath‘Tree‘"D
Mathematica Graphics 129
TreePlot@MakeTree@Range@15DDD;
ExprPlot@f@g@a, b, c, dD, g@x, y, h@x, yDD, g@x, yDDD;
4.3 Geometry
4.3.1Å Polytopes
This package contains functions that give geometric characteristics of regular polygons and polyhedra.
Needs@"Geometry‘Polytopes‘"D
130 Mathematica Graphics
? Geometry‘Polytopes‘*
Area InscribedRadiusCircumscribed NonagonCircumscribedRadius NumberOfEdgesCoords NumberOfFacesCube NumberOfVerticesDecagon OctagonDigon OctahedronDodecagon PentagonDodecahedron SchlafliDual SquareFaces TetrahedronHeptagon Geometry‘Polytopes‘TriangleHexagon UndecagonHexahedron Geometry‘Polytopes‘VerticesIcosahedron VolumeInscribed
Area@OctagonD
2 CotA pþþþþ8E
vert = Vertices@OctagonD
Vertices@OctagonD
Mathematica Graphics 131
Show@Graphics@[email protected], Point �ë vert<, AspectRatio� 1DD;
Graphics::gprim : Vertices@Point@OctagonDD was encountered
where a Graphics primitive or directive was expected.
Volume@TetrahedronD
1þþþþþþþþþþþþþþ6 �!!!!2
vert = Vertices@TetrahedronD
Vertices@TetrahedronD
132 Mathematica Graphics
Show@Graphics3D@[email protected], Point �ë vert<, AspectRatio� 1DD;
Graphics3D::gprim :
Vertices@Point@TetrahedronDD was encountered where a
Graphics3D primitive or directive was expected.
TriangularSurfacePlot@vertD;
4.4 Miscellaneous
4.4.1Å WorldPlot
This package allows us to plot maps as graphic objects, where positions are expressed in terms of latitude and longitude. The standard map projections Albers, Equirectangular, LambertAzimuthal, LambertCylindrical, Mercator, Mollweide, and Sinusoidal are included in the package. In addition, WorldPlot also loads data describing the names and outlines of countries of the world.
Needs@"Miscellaneous‘WorldPlot‘"D
Mathematica Graphics 133
w1 = WorldPlot@NorthAmericaD;
w2 = WorldPlot@8World, RandomGrays<D;
134 Mathematica Graphics
w3 = WorldPlot@8World, RandomGrays<,WorldRotation-> 890, 0, 0<, WorldRange -> 880, 90<, 8-180, 180<<,WorldProjection-> LambertAzimuthalD;
Mathematica Graphics 135
w4 = WorldPlot@8Africa, RandomGrays<, WorldProjection-> SinusoidalD;
136 Mathematica Graphics
w5 = WorldPlot@Africa,WorldBackground-> [email protected],WorldGrid -> None,
WorldFrame -> [email protected],WorldProjection-> LambertCylindricalD;
Mathematica Graphics 137
8capetown, cairo< =
ToMinutes@888-33, -56<, 818, 22<<, 8830, 3<, 831, 15<<<D;w6 = Show@8w5,
WorldGraphics@[email protected], 0.03<D,
[email protected], Line@8capetown, cairo<D<D<D;
Show@GraphicsArray@Partition@Map@Graphics, 8w1, w2, w3, w4, w5, w6<D, 3DDD;
138 Mathematica Graphics
4.5 NumericalMath
4.5.1Å Butcher
This package gives the order conditions that a Runge-Kutta method must satisfy to be of a particular order. It works for both implicit and explicit methods. The package also calculates and plots Butcher trees and implements the functions of trees defined in John Butcher’s book, The Numerical Analysis of Ordinary Differential Equations: Runge-Kutta and General Linear Methods. Butcher’s row and column simplifying conditions assist in the derivation of high-order methods. A more compact and efficient stage-independent tensor notation has also been implemented.
Needs@"NumericalMath‘Butcher‘"D
a::shdw :
Symbol a appears in multiple contexts 8NumericalMath‘Butcher‘,Global‘<; definitions in context NumericalMath‘Butcher‘ may
shadow or be shadowed by other definitions.
b::shdw :
Symbol b appears in multiple contexts 8NumericalMath‘Butcher‘,Global‘<; definitions in context NumericalMath‘Butcher‘ may
shadow or be shadowed by other definitions.
c::shdw :
Symbol c appears in multiple contexts 8NumericalMath‘Butcher‘,Global‘<; definitions in context NumericalMath‘Butcher‘ may
shadow or be shadowed by other definitions.
f::shdw :
Symbol f appears in multiple contexts 8NumericalMath‘Butcher‘,Global‘<; definitions in context NumericalMath‘Butcher‘ may
shadow or be shadowed by other definitions.
Mathematica Graphics 139
ButcherPlot@#,ButcherPlotLabel-> HInputForm �ë #LD & ë
Flatten@ButcherTrees@4DD;
her‘f@NumericalMath‘Butcher‘f@NumericalMath‘Butcher‘fer‘f@NumericalMath‘Butcher‘f@NumericalMath‘Butcher‘fher‘f@NumericalMath‘Butcher‘f* NumericalMath‘Butcher‘fNumericalMath‘Butcher‘f@NumericalMath‘Butcher‘f
icalMath‘Butcher‘ficalMath‘Butcher‘f@NumericalMath‘Butcher‘fDcalMath‘Butcher‘f@NumericalMath‘Butcher‘f@NumericalMath‘Butcher‘fNumericalMath‘Butcher‘f@NumericalMath‘Butcher‘f
4.5.2Å Microscope
This package allows functions to be plotted on a microscopic scale to exhibit the granularity of machine arithmetic. Alternatively the actual error (measured in ulps (unit in the last place, i.e., one digit in the last place)) can be plotted.
Needs@"NumericalMath‘Microscope‘"D
Microscope[Sqrt[x], {x, 5, 20}, PlotJoined -> Real];
5.
2.23607
140 Mathematica Graphics
MicroscopicError[Sqrt[x], {x, 5, 20}, PlotJoined -> Real];
5.
-0.6
-0.4
-0.2
0.2
0.4
0.6
4.5.3Å OrderStar
This package plots the order star of an approximating function, to an essentially analytic function. It is common to consider rational approximants to functions such as Padé approximants. Various information about a numerical scheme (such as order and stability) may be ascertained from its order star. For example, Runge-Kutta methods may be considered as rational approximants to the exponential, where relative and absolute stability regions are considered in terms of the linear scalar test problem of Dahlquist. The zeros, poles, and interpolation points convey important additional information and may also be displayed.
Needs@"NumericalMath‘OrderStar‘"D
Mathematica Graphics 141
DisplayTogetherArray@88OrderStar@Pade@Exp@zD, 8z, 0, 1, 0<D, EzD,
OrderStar@Pade@Exp@zD, 8z, 0, 1, 0<D, 1D,OrderStar@Pade@Exp@Cos@zD + I Cosh@zDD, 8z, 0, 2, 3<D,
Exp@Cos@zD + I Cosh@zDD,PlotRange -> 88-5, 5<, 8-5, 5<<, PlotPoints-> 40D<,
8OrderStar@Pade@Log@1 + zD, 8z, 1, 3, 2<D, Log@1 + zD,OrderStarKind -> Second,
PlotPoints -> 50, PlotRange -> 88-0.5, 2<, 8-1, 1<<,OrderStarSubPlots->
88PlotRange -> 880.9, 1.1<, 8-0.1, 0.1<<,PlotPoints -> 820, 20<<<D,
OrderStar@Pade@Exp@zD, 8z, 0, 2, 3<D, Exp@zD,OrderStarPoles-> 8True, False<,OrderStarZeros-> 8True, False<D,
OrderStar@Pade@Sinh@z - 1D, 8z, 0, 3, 3<D, Sinh@z - 1D,OrderStarLegend-> 880.6, 0.6<, 80.98, 0.98<<,TextStyle -> 8FontFamily -> "Times", FontSize -> 4<,OrderStarSymbolSize-> 0.02D<<D;
OrderStar::sols :
Warning: No finite zeros of function found using NSolve.
Either inverse functions or transcendental dependencies
were involved. Try specifying omitted points using options.
OrderStar::sols :
Warning: No poles of function found using NSolve. Either
inverse functions or transcendental dependencies were
involved. Try specifying omitted points using options.
OrderStar::sols :
Warning: No zeros of function found using NSolve. Either
inverse functions or transcendental dependencies were
involved. Try specifying omitted points using options.
General::stop : Further output of
OrderStar::sols will be suppressed during this calculation.
Poles of approximantZeros of approximantPoles of functionZeros of functionInterpolationpoints
142 Mathematica Graphics
4.6 Statistics
4.6.1Å LinearRegression
This package provides least squares or weighted least squares linear regression for data whose errors are assumed to be normally and independently distributed. It supports a number of commonly used statistics such as RSquared, EstimatedVariance, and an ANOVATable. It also provides diagnostics for leverage, influence, collinearity, and correlation.
Needs@"Statistics‘LinearRegression‘"D
This example demonstrates how to generate a ParameterConfidenceRegion of some data.
data = 880.055, 90<, 80.091, 97<, 80.138, 107<,80.167, 124<, 80.182, 142<, 80.211, 150<,80.232, 172<, 80.248, 189<, 80.284, 209<,80.351, 253<<;
regressdata = Regress@data, 81, x2<, x,
RegressionReport-> ParameterConfidenceRegionD
8ParameterConfidenceRegion� [email protected], 1430.68<,8220.303, 7.6006<, 880.045514, -0.998964<, 8-0.998964, -0.045514<<D<
Show@Graphics@ParameterConfidenceRegion�. regressdataD,Axes -> True, AxesLabel -> 8"Constant", "x2"<D;
80 85 90 95 100Constant
1300
1400
1500
1600
x2
4.6.2Å MultiDescriptiveStatistics
This package computes descriptive statistics (location, dispersion, shape, and association statistics) for a sample represented as a data matrix. The data matrix is a list of independent identically distributed vector-valued or multivariate observations.
Needs@"Statistics‘MultiDescriptiveStatistics‘"D
Mathematica Graphics 143
MultiDescriptiveStatistics defines two new graphics functions, Ellipsoid and Polytope. These functions are used in other statistics functions, including EllipsoidQuartiles and PolytopeQuartiles.
data = N@881232, 4175<, 81115, 6652<, 82205, 7612<, 81897, 10914<,81932, 10850<, 81612, 7627<, 81598, 6954<, 81804, 8365<, 81752, 9469<,82067, 6410<, 82365, 10327<, 81646, 7320<, 81579, 8196<,81880, 9709<, 81773, 10370<, 81712, 7749<, 81932, 6818<,81820, 9307<, 81900, 6457<, 82426, 10102<, 81558, 7414<,81470, 7556<, 81858, 7833<, 81587, 8309<, 82208, 9559<, 81487, 6255<,82206, 10723<, 82332, 5430<, 82540, 12090<, 82322, 10072<<D;
8stiffness, strength< = Transpose@dataD;88minx, maxx<, 8miny, maxy<< =
Map@8Min@#1D, Max@#1D< &, 8stiffness, strength<D;
General::spell1 : Possible spelling error: new
symbol name "miny" is similar to existing symbol "minx".
General::spell1 : Possible spelling error: new
symbol name "maxy" is similar to existing symbol "maxx".
q1 = Graphics@EllipsoidQuartiles@dataD,Frame � True, AspectRatio� 1,
PlotRange� 88minx, maxx<, 8miny, maxy<<,FrameTicks� 881200, 1600, 2000, 2400<, Automatic<D;
q2 = Graphics@PolytopeQuartiles@dataD,Frame � True, AspectRatio� 1,
PlotRange� 88minx, maxx<, 8miny, maxy<<,FrameTicks� 881200, 1600, 2000, 2400<, Automatic<D;
Show@GraphicsArray@8q1, q2<DD;
1200 1600 2000 2400
50006000700080009000100001100012000
1200 1600 2000 2400
1200 1600 2000 2400
50006000700080009000100001100012000
1200 1600 2000 2400
4.7 Utilities
4.7.1Å DXF
This package allows the export of Graphics3D objects to a file in the AutoCAD DXF format.
144 Mathematica Graphics
Needs@"Utilities‘DXF‘"D
gr = Plot3DABesselJA0, �!!!!!!!!!!!!!!x2 + y2 E, 8x, -6, 6<, 8y, -6, 6<, PlotPoints� 20E;
-5
-2.5
0
2.55
-5
-2.50
2.55
0
0.5
0
0.5
gr = Graphics3D@grD;
WriteDXF@"bessel.dxf", grD
bessel.dxf
Some of the graphics primitives that can be transferred are Cuboid, Polygon, Line, and Point.
Mathematica Graphics 145
146 Mathematica Graphics
5Colors
5.1 Built-in Color Functions
Colors can be specified in any of several color systems. RGBColor[r, g, b], where 0 ≤ r, g, b ≤ 1 specifies a
color with the specified proportions of red, green, and blue. GrayLevel[level], where 0 ≤ level ≤ 1, specifies a shade of gray. CMYKColor[cyan, magenta, yellow, black] accepts arguments that are each between zero specifies colors in the CMYK color system. Hue[hue, saturation, brightness] allows color specification by hue, saturation and brightness levels, all of which must be between 0 and 1. The simplified form Hue[hue], which corresponds to Hue[h, 1, 1], allows color specification with one argument. As h varies from 0 to 1, Hue[hue] runs through red, yellow, green, cyan, blue, magenta, and back to red again. Values of h outside this range are taken mod 1.
Show[ Graphics[ {PointSize[.15],
RGBColor[0,0,1],Point[{0,0}], CMYKColor[.2,.9,.6,.3], Point[{1,0}], GrayLevel[.7], Point[{2,0}],Hue[.5], Point[{3,0}],Hue[.5, .4, .6], Point[{4,0}]}],PlotRange -> {{-1,5},Automatic}];
A color wheel can be created by mapping Hue over the sectors of a disk.
ColorWheel@n_D :=
Show@Graphics@Map@8Hue@#�H2 p - nLD, Disk@80, 0<, 1, 8#, # + n<D< &,
Range@0, 2 p - n, nDD, AspectRatio -> AutomaticDD
Mathematica Graphics 147
wheel = ColorWheel@p�128D;
General::spell1 : Possible spelling error: new
symbol name "wheel" is similar to existing symbol "Wheel".
Another way of specifying colors in Mathematica is by using the Color Selector built into the Mathematica front end. The menu item can be found, on the Macintosh, in the Input menu. Manipulate the controls until the shade you want appears in the New box. Clicking the OK button pastes the correct RGBColor specification at the current insertion point in the current notebook.
148 Mathematica Graphics
5.2 External Packages
Loading the package Graphics‘Colors‘ provides definitions for three alternate color designation functions and 194 English names for colors and shades of gray. Once loaded, we can use these color names in place of the function calls defined earlier. AllColors is a list of all the defined colors.
Needs["Graphics‘Colors‘"]
Length@AllColorsD
193
Short[AllColors, 10]
÷1ø
Here is the RGBColor value of the color VanDykeBrown.
VanDykeBrown
[email protected], 0.149998, 0.020005D
CMYColor[c, m, y] represents a color in the CMY (cyan-magenta-yellow) system, HLSColor[h, l, s] represents a color in the HLS (hue-lightness-saturation) system, and YIQColor[y, i, q] represents a color in the YIQ (NTSC video form) system. These functions simply return the matching RGBColor specification.
Mathematica Graphics 149
Show@ Graphics@[email protected],
[email protected], 0.7, 0.23D, Point@80, 0<D,[email protected], 0.3, 0.6D, Point@81, 0<D,[email protected], -0.1, -0.05D, Point@82, 0<D<D,PlotRange -> 88-1, 3<, Automatic<D;
The package Graphics‘ArgColors‘ defines three more color utilities; ArgColor, ArgShade, and ColorCircle. ArgColor[z] gives a color value whose hue is proportional to the argument of the complex number z, ArgShade[z] gives a gray level proportional to the argument of the complex number z, and ColorCircle[r, (light:1)] gives a color value whose hue is proportional to r (mod 2 p) with lightness light.
Needs@"Graphics‘ArgColors‘"D
ArgColor@4 + 12 ID
[email protected], 1, 1D
Remember the definition of the color wheel from above. ColorCircle performs the same tranformation and allows a simpler definition.
ColorWheel@n_D :=
ShowAGraphicsAJ9HueA #1þþþþþþþþþþþþþþþ2 p - n
E, Disk@80, 0<, 1, 8#1, #1 + n<D= &N �ë
Range@0, 2 p - n, nD, AspectRatio� AutomaticEE
ColorWheel2@n_D :=
Show@Graphics@H8ColorCircle@#1D, Disk@80, 0<, 1, 8#1, #1 + n<D< &L �ë
Range@0, 2 p - n, nD, AspectRatio� AutomaticDD
150 Mathematica Graphics
wheel2 = ColorWheel2@p� 128D;
Mathematica Graphics 151
152 Mathematica Graphics
6Graphics Primitives and Directives
6.1 Overview
All Mathematica graphics are built from a small number of graphics primitives and directives. The user may build arbitrary graphics in two and three dimensions from these basic building blocks of points, lines, circles, and so on. Graphics primitives define shapes, while graphics directives control the way the shape is drawn.
The following syntaxes are used to render a series of two- and thee-dimensional graphics, respectively.
Show@ Graphics@ 8 series of graphics primitives and directives < D D
General::spell1 : Possible spelling error: new symbol
name "series" is similar to existing symbol "Series".
General::spell1 : Possible spelling error: new symbol
name "graphics" is similar to existing symbol "Graphics".
General::spell1 : Possible spelling error: newsymbol name "and" is similar to existing symbol "And".
General::stop : Further output of
General::spell1 will be suppressed during this calculation.
Graphics::gprim :
and directivesgraphicsof primitivesseries was encountered
where a Graphics primitive or directive was expected.
ú Graphics ú
Mathematica Graphics 153
Show@ Graphics3D@ 8 series of graphics primitives and directives < D D
Graphics3D::gprim :
and directivesgraphicsof primitivesseries was encountered
where a Graphics3D primitive or directive was expected.
ú Graphics3D ú
6.2 Two-Dimensional Graphics Primitives
The primitives Point, Line, Rectangle, Polygon, Circle, Disk, Raster, RasterArray, and Text form the basis for all two-dimensional graphics.
Circle[ {x, y}, r ] is a two-dimensional graphics primitive that represents a circle of radius r centered at the point {x, y}. Circle[ {x, y}, {rx, ry}] yields an ellipse with semi-axes rx and ry. Circle[ {x, y}, r, {theta1, theta2}] represents a circular arc. Line[ {pt1, pt2, …} ] is a graphics primitive that represents a line joining a sequence of points. Text[expr, coords] is a graphics primitive that represents text corresponding to the printed form of expr, centered at the point specified by coords. Other primitives behave similarly.
154 Mathematica Graphics
ShowAGraphicsA9Polygon@880, 0<, 81, 1<, 80, 2<, 8-1, 1<, 80, 0<<D,Line@880, 0<, 81, 1<, 80, 2<, 8-1, 1<, 80, 0<<D,Line@880, 0<, 80, 2<<D,Circle@80, 0<, 1D, CircleA80, 0<, �!!!2 E,Line@88-2, -2<, 82, 2<<D, Line@88-2, 2<, 82, -2<<D,Line@88-2, 0<, 82, 4<<D, Line@88-2, 4<, 82, 0<<D,Line@880, 0<, 81, 0<, 81, 1<, 80, 1<, 80, 0<<D,Text@"Duplicating", 8-1, 1.85<D,Text@"the Square", 8-1, 1.7<D=E,
Axes -> True, AxesOrigin -> 80, 0<,AspectRatio-> Automatic, PlotRange -> 8Automatic, 8-0.2, 2.2<<E;
-2 -1 1 2
0.5
1
1.5
2Duplicatingthe Square
Raster and RasterArray provide a mechanism for representing a rectangular array of gray or colored cells, respectively.
rasterarrayexample= Table@[email protected] Mod@i, jDD, 8i, 2, 10<, 8j, 2, 10<D;
Show@Graphics@RasterArray@rasterarrayexampleDDD;
Mathematica Graphics 155
6.3 Graphics Directives
The directives Thickness, PointSize, and Dashing, and any valid color specification are the basic means of changing the way the graphics primitives are drawn. Interspersing these with the graphics primitives allows us to create any configuration necessary.
Thickness[ r ] is a graphics directive that specifies that lines that follow are to be drawn with a thickness r, where r is given as a fraction of the total width of the graph. Dashing[ {r1, r2, …} ] is a two-dimensional graphics directive which specifies that lines which follow are to be drawn dashed, with successive segments of lengths r1, r2, … (repeated cyclically). The ri are given as a fraction of the total width of the graph. For each directive that scales sizes relative to the size of the graphic, there is a corresponding directive, for example, AbsoluteThickness, allowing us to exactly specify the size or thickness in printer’s points, approximately equal to 1
þþþþþþþ72 inch.
Polygon@880, 0<, 81, 1<, 80, 2<, 8-1, 1<, 80, 0<<D<,8Hue@0D, [email protected],
Line@880, 0<, 81, 1<, 80, 2<, 8-1, 1<, 80, 0<<D<,Line@880, 0<, 80, 2<<D,
[email protected]<D,Circle@80, 0<, 1D, CircleA80, 0<, �!!!2 E,Line@88-2, -2<, 82, 2<<D, Line@88-2, 2<, 82, -2<<D,Line@88-2, 0<, 82, 4<<D, Line@88-2, 4<, 82, 0<<D=,
[email protected],Line@880, 0<, 81, 0<, 81, 1<, 80, 1<, 80, 0<<D<,
[email protected],Text@"Duplicating", 8-1, 1.85<D,Text@"the Square", 8-1, 1.7<D<=E,
Axes -> True, AxesOrigin -> 80, 0<,AspectRatio-> Automatic, PlotRange -> 8Automatic, 8-0.2, 2.2<<E;
-2 -1 1 2
0.5
1
1.5
2Duplicatingthe Square
156 Mathematica Graphics
6.4 Additional Primitives for Three Dimensions
When creating three-dimensional graphics most of the same primitives apply providing we specify points in the appropriate way. Cuboid[{xmin, ymin, zmin}] is a three-dimensional graphics primitive that represents a unit cuboid, oriented parallel to the axes. Cuboid[{xmin, ymin, zmin}, {xmax, ymax, zmax}] specifies a cuboid by giving the coordinates of opposite corners.
Show@Graphics3D@8Cuboid@80, 0, 0<D,
Cuboid@85, 0, 0<D, Cuboid@80, 5, 0<D, Cuboid@80, 0, 5<D,Cuboid@85, 0, 5<D, Cuboid@85, 5, 0<D, Cuboid@80, 5, 5<D,Cuboid@85, 5, 5<D, [email protected], 0.5, .5<, 85.5, 5.5, 5.5<<D,[email protected], 0.5, .5<, 8.5, 5.5, 5.5<<D,[email protected], 0.5, 5.5<, 85.5, 5.5, 0.5<<D,[email protected], 0.5, 5.5<, 8.5, 5.5, 0.5<<D,Polygon@881, 1, 0<, 85, 1, 0<, 85, 5, 0<, 81, 5, 0<, 81, 1, 0<<D,[email protected], Point@83, 3, 3<D<<D,
Boxed -> FalseD;
6.5 Additional Directives for Three Dimensions
SurfaceColor[col] is a three-dimensional graphics directive specifying that the polygons that follow should act as diffuse reflectors of light with a color given by col.
Mathematica Graphics 157
Show@Graphics3D@Table@8SurfaceColor@Hue@Random@DDD,
Cuboid@8Random@Integer, 81, 15<D,
Random@Integer, 81, 15<D,Random@Integer, 81, 15<D<D<,
850<DDD;
EdgeForm[g] is a three-dimensional graphics directive that specifies that edges of polygons are to be drawn using the graphics directive or list of graphics directives g.
158 Mathematica Graphics
Show@Graphics3D@8
EdgeForm@[email protected],Polygon@880, 0, 0<, 80, 1, 1<, 81, 1, 1<<D,EdgeForm@[email protected], Hue@0D<D,Polygon@880, 0, 0<, 81, 1, 0<, 81, 1, 1<<D<DD;
FaceForm[gf, gb] is a three-dimensional graphics directive that specifies that the front faces of all polygons are to be drawn with the graphics primitive gf, and the back faces with gb.
view1 = Graphics3D@8FaceForm@Hue@0D, RGBColor@0, 1, 0DD,Polygon@880, 0, 0<, 80, 1, 1<, 81, 0, 1<<D<,Lighting -> False,
Axes -> True, Ticks -> None,
AxesLabel -> 8"x", "y", "z"<,ViewPoint -> 81.300, -2.400, 2.000<,D;
view2 = Graphics3D@8FaceForm@Hue@0D, RGBColor@0, 1, 0DD,Polygon@880, 0, 0<, 80, 1, 1<, 81, 0, 1<<D<,Lighting -> False,
Axes -> True, Ticks -> None,
AxesLabel -> 8"x", "y", "z"<,ViewPoint -> 81.300, 2.400, 2.000<D;
Mathematica Graphics 159
Show@GraphicsArray@8view1, view2<DD;
Graphics3D::nonopt :
Options expected Hinstead of NullL beyond position 1 in
ú Graphics3D ú. An option must be a rule or a list of rules.
Graphics3D::nonopt :
Options expected Hinstead of NullL beyond position 1 in
ú Graphics3D ú. An option must be a rule or a list of rules.
Set::shape :
Lists 88System‘Dump‘xmin$40418, System‘Dump‘xmax$40418<, ÷1ø, 8÷22ø,
÷22ø<< and PlotRange@ú Graphics3D úD are not the same shape.
Graphics3D::nonopt :
Options expected Hinstead of NullL beyond position 1 in
ú Graphics3D ú. An option must be a rule or a list of rules.
General::stop : Further output of
Graphics3D::nonopt will be suppressed during this calculation.
xyzxy
6.6 More about Text
The form Text[expr, coords] will produce text corresponding to the printed form of expr, centered at the point specified by coords, but there are several more options available. Third and fourth arguments to Text control the orientation alignment and orientation of the text with the form Text[expr, coords, align, orient]. Align is of the form {a, b} where a controls the left-right alignment and b controls the above-below alignment. orient is of the form {a, b} where a controls the rightside-up and upside-down alignment and b controls the horizontal-vertical alignment. Note that rotated text does not display correctly but does print correctly.
160 Mathematica Graphics
offset Relation tocoords
orient Orientation of text
80, 0< Centered 81, 0< Horizontal Hrightside–upL
8-1, 0< Left Justified 80, 1< Vertical HupL
81, 0< Right Justified 80, -1< Vertical HdownL
80, -1< Centered Above8-1, 0< Horizontal Hupside–downL
80, 1< Centered Below
General::spell1 : Possible spelling error: new symbol
name "offset" is similar to existing symbol "Offset".
General::spell1 : Possible spelling error: new symbol
name "coords" is similar to existing symbol "Coords".
General::spell1 : Possible spelling error: new
symbol name "text" is similar to existing symbol "Text".
General::stop : Further output of
General::spell1 will be suppressed during this calculation.
88offset, coords Relationto, orient, of Orientationtext<,880, 0<, Centered, 81, 0<, Horizontal rightside–up <,
88-1, 0 <, Justified Left, 80, 1 <, up Vertical <,
881, 0 <, Justified Right, 80, -1<, down Vertical <,
880, -1<, Above Centered, 8-1, 0 <, Horizontal upside–down <,
880, 1 <, Below Centered, Null, Null <<
Mathematica Graphics 161
Show@Graphics@Table@Text@"Text", 8i, j<,
8Random@Integer, 8-1, 1<D,Random@Integer, 8-1, 1<D<,
8Random@Integer, 8-1, 1<D, Random@Integer, 8-1, 1<D<D,8i, 1, 5<, 8j, 1, 5<DD, PlotRange -> 880, 7<, 8-1, 7<<D;
Text::textz : Vector for text rotation cannot have value 80,0<.
Text::textz : Vector for text rotation cannot have value 80,0<.
Text::textz : Vector for text rotation cannot have value 80,0<.
General::stop : Further output of
Text::textz will be suppressed during this calculation.
txeT
txeT
txeT
txeT
txeT
txeTText
txeT
Text
txeT
Text
Text
txeT tx
eT
txeT
txeT
txeT
txeT
Text
txeT
txeT
txeT
txeT
txeT
txeT
TextStyle is an option for graphics functions and for Text that specifies the default style and font options with which text should be rendered. Options such as FontSize, FontSlant, FontFamily, FontColor, and Background are recognized.
162 Mathematica Graphics
Show@Graphics@Text@"Hello World", 810, 10<, TextStyle� 8FontFamily� "Times",
FontWeight� "Bold", FontSize� 20, FontColor� [email protected],FontSlant� "Italic", Background� [email protected]<DDD;
Hello World
The same effect can be achieved using StyleForm. StyleForm[expr, options] prints using the specified style options. StyleForm[expr, "style"] prints using the specified cell style in the current notebook.
Table@StyleForm@"text", FontFamily� "Times", FontSize� sD, 8s, 10, 20<D
9text, text, text, text, text, text, text, text, text, text, text=
StyleForm@ "Hello World", "Title"D
Hello World
Among the aspects of Mathematica we will not discuss in this course is its typesetting capabilities. Display allows us to take typeset expressions and display them as graphics. stdout directs the output to the front end.
Display@$Output, ToBoxes@Nest@1 + 1� # &, x, 7DD, "MPS"D;
1 +
1þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþ
1 +1
þþþþþþþþþþþþþþþþþþþþþþþþ1+ 1
þþþþþþþþþþþþþþþþþþþþþþþþþ
1+ 1þþþþþþþþþþþþþþþþþþþþþþþþþþþþ
1+ 1þþþþþþþþþþþþþþþþþþþþþþþþ
1+ 1þþþþþþþþþþþþþþ
1+ 1þþþþþx
Mathematica Graphics 163
Exercises: Graphics Primitives and Directives
Í Below is the Mathematica code to draw a face. Use other two-dimensional graphics commands (e.g., Polygon) to add other features (nose, beard, hat, etc.) and make changes to the face (e.g., eye color). Experiment and have fun.
[email protected], Circle@80, 0<, 1D,[email protected], [email protected], .3<D, [email protected], .3<D,CircleA80, -0.1<, .5, 9p +
pþþþþ4, 2 p -
pþþþþ4=E=E, AspectRatio� AutomaticE;
164 Mathematica Graphics
[email protected], Circle@80, 0<, 1D, [email protected],Red, [email protected], 0.3<, 0.04D, [email protected], 0.3<, 0.04D,Blue, CircleA80, -0.1<, 0.5, 9p +
pþþþþ4, 2 p -
pþþþþ4=E, Pink,
[email protected], -0.9<, 80.25, -0.9<, 80, -1.25<<D, Green,
[email protected], 0.9<, 80.6, 0.9<, 80.6, 1.1<, 8-0.6, 1.1<<D,[email protected], 1.1<, 80.4, 1.1<, 8.4, 1.5<, 8-0.4, 1.5<<D=E,
AspectRatio� AutomaticE;
Mathematica Graphics 165
7Displaying and Combining Graphics
7.1 Show
Perhaps the most versatile tool Mathematica provides for working with graphics is the Show command. Show, in its simplest form, allows us to rerender a graphic that has already been generated. Show also allows us to change some of the options associated with a graphic while redrawing the graphic. In general, we can change those options that don’t affect the curve itself, such as the PlotRange.
firstplot = Plot@x Sin@xD2, 8x, 0, 3 p<D;
2 4 6 8
2
4
6
8
Show@firstplot, PlotRange � 88-5, 15<, 8-5, 10<<D;
-5 -2.5 2.5 5 7.5 10 12.5 15
-4
-2
2
4
6
8
10
166 Mathematica Graphics
Show will render two or more graphics together on the same set of axes, scaling them appropriately.
secondplot = Plot@x Cos@xD2, 8x, 0, 3 p<,PlotStyle -> [email protected]<D, [email protected]<D;
2 4 6 8
2
4
6
8
Show@firstplot, secondplotD;
2 4 6 8
2
4
6
8
Show works the same with three-dimensional graphics as it does with two-dimensional plots.
Mathematica Graphics 167
first3d = Plot3D@H1 - Sin@xDL H2 - Cos@2 yDL, 8x, -2, 2<, 8y, -2, 2<D;
-2
-1
0
1
2
-2
-1
0
12
0
2
4
6
0
2
4
6
second3d = Plot3D@H2 + Sin@xDL H1 + Cos@2 yDL, 8x, -2, 2<, 8y, -2, 2<D;
-2
-1
0
1
2
-2
-1
0
12
0
2
4
6
0
2
4
6
168 Mathematica Graphics
Show@first3d, second3dD;
-2
-1
0
1
2
-2
-1
01
2
0
2
4
6
0
2
4
6
Sometimes when generating three-dimensional graphics with Show, it may be easier to visualize the different plots by giving each plot a separate color instead of the the default shading scheme.
Show@Plot3D@8H1 - Sin@xDL H2 - Cos@2 yDL, [email protected]<,
8x, -2, 2<, 8y, -2, 2<, DisplayFunction � IdentityD,Plot3D@8H2 + Sin@xDL H1 + Cos@2 yDL, [email protected]<,8x, -2, 2<, 8y, -2, 2<, DisplayFunction � IdentityD,DisplayFunction � $DisplayFunctionD;
-2
-1
0
1
2
-2
-1
01
2
0
2
4
6
0
2
4
6
Mathematica Graphics 169
7.2 GraphicsArray
Another useful function is GraphicsArray, which allows us to create tables of graphics. It is important to know the difference between those functions that create graphics objects, those that render them, and those that do both. For example, Plot will both create and render a graphics object, Show will render a previously computed graphics object, and GraphicsArray will create a new graphics object from other graphics objects.
Show@GraphicsArray@8firstplot, secondplot<DD;
2 4 6 8
2
4
6
8
2 4 6 8
2
4
6
8
Using GraphicsArray, we are able to create a two-dimensional array of any shape and fill it with Mathematica graphics.
Show@GraphicsArray@88firstplot, secondplot<,8secondplot, firstplot, secondplot<<DD;
2 4 6 8
2468
2 4 6 8
2468
2 4 6 8
2468
2 4 6 8
2468
2 4 6 8
2468
7.3 Rectangle
You have seen how to constuct regular arrays of graphics using GraphicsArray, but you can also combine and superimpose plots in any way with the Rectangle function.
Rectangle allows you to render a graphics object within a specified rectangle and then to display that rectangle with other rectangles or individually in another plot.
First we generate a graphics object.
170 Mathematica Graphics
rectplot =
Plot3D@Sin@xD Exp@yD, 8x, -5, 5<, 8y, -2, 2<,Axes -> FalseD;
Now we can use Rectangle and the Show command to place two copies of a graphic wherever we wish.
Show@Graphics@8Rectangle@80, 0<, 81, 1<, rectplotD,[email protected], 0.8<, 81.2, 1.4<, rectplotD<DD;
7.4 DisplayFunction
DisplayFunction is an option for every graphics function in Mathematica. With it, we specify the output medium for the image. By default, this option is set to the global variable $DisplayFunction, which in the notebook front end, will render the graphic inside the current notebook. To send the PostScript output to a file use the setting DisplayFunction � Display["file", #]&.
Mathematica Graphics 171
For our purposes the setting DisplayFunction � Identity will be the most useful. This is because the graphic is computed, but not displayed. When you wish to display the graphic, you need to set DisplayFunction back to the default value, $DisplayFunction.
gr1 = [email protected] x3 - 2 x, 8x, -10, 10<,PlotStyle� 88RGBColor@1, 0, 0D, [email protected]<<,DisplayFunction� IdentityD;
gr2 = Plot@x Sin@xD2, 8x, -10, 10<,PlotStyle� [email protected]<<,DisplayFunction� IdentityD;
Show@8gr1, gr2<,DisplayFunction� $DisplayFunctionD;
-10 -5 5 10
-10
-5
5
10
When we are combining plots, usually we are only interested in the final graphic. If we had not set DisplayFunction to Identity in the previous example, Mathematica would have printed each component graphic also.
7.5 DisplayTogether and DisplayTogetherArray
The package Graphics‘Graphics‘ which defines the functions DisplayTogether and DisplayTogetherArray, allows us to combine graphs on the same set of axes or in an array without rendering each graph beforehand. To use the functions we first load the package.
Needs@"Graphics‘Graphics‘"D
172 Mathematica Graphics
DisplayTogether@Plot@Sin@xD, 8x, -3, 3<, PlotStyle -> [email protected],
Plot@Sin@3 xD, 8x, -3, 3<, PlotStyle -> [email protected]<DDD;
-3 -2 -1 1 2 3
-1
-0.5
0.5
1
DisplayTogetherArray@Plot@Sin@xD, 8x, -3, 3<, PlotStyle -> [email protected],Plot@Sin@3 xD, 8x, -3, 3<, PlotStyle -> [email protected]<DDD;
-3 -2 -1 1 2 3
-1
-0.5
0.5
1
-3 -2 -1 1 2 3
-1
-0.5
0.5
1
It is important to note that DisplayTogether and DisplayTogetherArray do not work with graphics that we have named. If we have graphics that have already been assigned to a symbol name, we should use the Show function.
7.6 Graphics Formats
Mathematica generates graphics in PostScript form. In most Mathematica implementations you can see and edit the PostScript code directly. This form is transportable among all platforms and contains enough information to allow viewing and printing at any resolution or size.
We can save the PostScript to a file that can be read by many of the highest-quality graphics processors. For example, if we wanted to save a graphic as an Encapsulated PostScript file (EPS) for use in another graphics program, all we would have to do is select the graphic and choose the Save Selection As... command in the Edit menu of the front end and then choose which format we want from the submenu that appears.
The user has the ability to convert the graphic to any one of several local formats, depending on the platform that Mathematica runs on. Usually the results are a loss of generality but savings in speed (rendering) and space.
Mathematica Graphics 173
7.6.1Å Image Conversions
Display[channel, graphics] writes graphics or sound to the specified output channel in Mathematica PostScript format. Display[channel, graphics, "format"] writes graphics or sound in the specified format. Display[channel, expr, "format"] writes boxes, cells, or notebook expressions in the specified format. channel specifies where the image is to be written. Using a filename, such as graph1.eps, will create a new file. The following formats are available.
EPS Encapsulated PostScript
GIF GIF
Illustrator AdobeIllustrator format
Metafile MicrosoftWindowsmetafile
MGF Mathematica system–independent raster graphics format
MPS Mathematica abbreviatedPostScript
PCL Hewlett–Packard printer control language
PDF AdobeAcrobatportable document format
PICT Macintosh PICT
PBM portable bitmap
PSImage PostScript image format
TIFF TIFF
XBitmap X Windows bitmap
General::spell1 : Possible spelling error: new symbol
name "format" is similar to existing symbol "Format".
General::spell1 : Possible spelling error: new symbol
name "metafile" is similar to existing symbol "Metafile".
General::spell1 : Possible spelling error: new symbol
name "raster" is similar to existing symbol "Raster".
General::stop : Further output of
General::spell1 will be suppressed during this calculation.
88EPS, EncapsulatedPostScript<,
8GIF, GIF<, 8Illustrator, Adobe format Illustrator<,
8Metafile, metafileMicrosoftWindows<,
8MGF, format graphicsMathematicaraster system–independent <,
8MPS, abbreviatedMathematicaPostScript <,
8PCL, control Hewlett–Packard language printer <,
8PDF, Acrobat Adobe document format portable <, 8PICT, Macintosh PICT <,
8PBM, bitmap portable <, 8PSImage, format image PostScript <,
8TIFF, TIFF <, 8XBitmap, bitmap Windows X <<
Several different formats specific to other programs are also supported. In addition, on all platforms we can export a graphics as a DXF file, AutoCad’s standard. This functionality is in a Mathematica standard add-on package.
Enter Needs["Utilities‘DXF‘"] to load the package.
174 Mathematica Graphics
8Using Options
8.1 Plot Options
8.1.1Å Basics
Many aspects of every Mathematica graphic are controlled by the values of the options associated with it. Striking changes can be made by using these judiciously. Options[command ] lists all options for command and their defaults.
Options@ PlotD �� TableForm
AspectRatio� 1þþþþþþþþþþþþþþþþþþþþþþþþGoldenRatio
Axes � AutomaticAxesLabel� NoneAxesOrigin� AutomaticAxesStyle� AutomaticBackground� AutomaticColorOutput� AutomaticCompiled � TrueDefaultColor� AutomaticEpilog � 8<
Frame � FalseFrameLabel� NoneFrameStyle� AutomaticFrameTicks� AutomaticGridLines� NoneImageSize� AutomaticMaxBend � 10.PlotDivision� 30.PlotLabel� NonePlotPoints� 25PlotRange� AutomaticPlotRegion� AutomaticPlotStyle� AutomaticProlog � 8<
RotateLabel� TrueTicks � Automatic
DefaultFont� $DefaultFontDisplayFunction� $DisplayFunctionFormatType� $FormatTypeTextStyle� $TextStyle
Mathematica Graphics 175
8.1.2Å PlotRange and Domain
Besides the function we wish to plot, the only required parameter to the function Plot is the domain over which to plot the function. Given that information, Mathematica will choose a range to display in the final graphic. The x-range displayed by default will be exactly the specified domain, while the y-range displayed will be one chosen to display what Mathematica considers to be the interesting parts of the graph. Sometimes, though, extreme values will be cut off so that the rest of the graphic may be scaled normally.
Plot@x Sin@xD3, 8x, -10, 10<D;
-10 -5 5 10
-4
-2
2
4
The option PlotRange allows us to specify the area of the graph to show. We may set this option to All to ensure that the entire graphic is displayed, or we may choose arbitrary coordinates. We may also specify the x range to display by giving coordinates in the form {{xmin, xmax}, {ymin, ymax}}. Note that the curve is computed only for the specified domain even if we display a larger one using PlotRange.
range1 = Plot@x Sin@xD3, 8x, -10, 10<,PlotRange� All, DisplayFunction� IdentityD;
range2 = Plot@x Sin@xD3, 8x, -10, 10<,PlotRange� 8-10, 10<, DisplayFunction� IdentityD;
range3 = Plot@x Sin@xD3, 8x, -10, 10<,PlotRange� 88-15, 15<, 8-15, 15<<, DisplayFunction� IdentityD;
Show@GraphicsArray@8range1, range2, range3<DD;
-10 -5 5 10-4-2
2468
-10-5 5 10
-10-7.5-5-2.52.557.510
-15-10-5 5 1015
-15-10-5
51015
176 Mathematica Graphics
8.1.3Å Adaptive Sampling Algorithm, PlotDivision, and PlotPoints
Mathematica uses an adaptive sampling algorithm to choose the points sampled in a two-dimensional plot. Beginning with 25 equally spaced points dividing the domain to be plotted, Mathematica looks at each set of three consecutive points and computes the angle between the line segment joining the first and second points and the line segment joining the second and third points. If this angle is close to 180 degrees, then Mathematica connects the points with lines. If not, Mathematica subdivides that interval and tries again. This allows Mathematica to sample more points in a “curvy” section of the function than in a flat section.
This process can be controlled by the options PlotDivision, which is the upper limit on the number of times an interval will be divided, and PlotPoints, which sets the initial number of points to be sampled.
Although this is a very robust algorithm, which produces accurate results in most cases, any scheme using a finite number of points is prone to miss sometimes. Here is an example of a function that is misplotted using the default number of plot points, but which can be accurately plotted by raising the initial number of plot points used.
Plot@x + Sin@2 p xD, 8x, 0, 25<D;
5 10 15 20 25
5
10
15
20
25
Plot@x + Sin@2 p xD, 8x, 0, 24<, PlotPoints� 50D;
5 10 15 20
5
10
15
20
Mathematica Graphics 177
8.1.4Å AspectRatio
The ratio of height to width of the plot is controlled with AspectRatio. The default value is the inverse of the golden ratio, or approximately 0.618. We may set this to an arbitrary real number for explicit control, or use the keyword Automatic to allow Mathematica to compute a realistic AspectRatio from the coordinate values in the plot. With this setting, circles and other geometric figures will appear proportionally correct. It is the default setting, historically the choice of artists and architects for their work, and Automatic that will be most often used.
aspect1 = ParametricPlot@8Sin@tD, Cos@tD<, 8t, 0, 2 p<, DisplayFunction� IdentityD;
aspect2 = ParametricPlot@8Sin@tD, Cos@tD<, 8t, 0, 2 p<,AspectRatio� Automatic, DisplayFunction� IdentityD;
Show@GraphicsArray@8aspect1, aspect2<DD;
-1 -0.5 0.5 1
-1
-0.5
0.5
1
-1-0.5 0.5 1
-1
-0.5
0.5
1
8.1.5Å PlotStyle
This option allows the user control over the way the curve itself is drawn. Since Plot allows us to graph an arbitrary number of curves simultaneously, PlotStyle takes a list of lists of directives, pairing each list of directives with one curve. If there are more curves than lists of directives, the existing lists of directives are applied cyclically.
The color of the curve can be specified in any of the standard ways. Thickness of the line is controlled by Thickness[r], where r is given as a fraction of the graphic’s total width or by AbsoluteThickness[d], where d is specified in points (where 1 point is 1
þþþþþþþ72 inch). We can create a dashed line with the directive Dashing[{r1, r2, … }], where {r1, r2, …} denote lengths of successive segments as a fraction of the total width of the graphic. The lengths are repeated cyclically. AbsoluteDashing[{d1, d2, … }] behaves identically but takes lengths in points. When plotting points with a function such as ListPlot, PointSize[r] specifies the radius of individual points, where r is given as a fraction of the graphic’s total width. AbsolutePointSize[r] allows specification of the radius r in points.
178 Mathematica Graphics
PlotA92 x2 E-x2, 2 x2 E-2 x2, 2 x2 E-3 x
2=, 8x, -2, 2<,PlotStyle�
88Red<, [email protected], 0.02<D, Magenta<, [email protected]<<,PlotRange� 8-1, 1<, PlotPoints� 50E;
-2 -1 1 2
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
8.1.6Å Frames, Axes, Labels, and Grids
By default, Mathematica will draw coordinate axes, but no frame and no labels, on a two-dimensional plot.
Plot@x Ex Sin@8 p xD, 8x, 0, 1<D;
0.2 0.4 0.6 0.8 1
-2
-1
1
If we ask for a frame, the ticks are automatically drawn on the frame rather than through the graphic, so often we will want to turn off the axes entirely.
frame1 = Plot@x Ex Sin@8 p xD, 8x, 0, 1<,Frame � True, DisplayFunction� IdentityD;
frame2 = Plot@x Ex Sin@8 p xD, 8x, 0, 1<,Frame � True, Axes � False, DisplayFunction� IdentityD;
Mathematica Graphics 179
Show@GraphicsArray@8frame1, frame2<DD;
0 0.2 0.4 0.6 0.8 1
-2
-1
0
1
0 0.2 0.4 0.6 0.8 1
-2
-1
0
1
Axes allows is to control which, if any, axes are drawn, while AxesStyle controls how the axes are drawn. Axes can bet set to True or False, or to a list of values allowing us to control each axis independently. AxesStyle can take a list of lists of graphics directives and control the style of each axis independently. Analogous to AxesStyle, FrameStyle gives us control over the way the frame is drawn.
axes1 = Plot@x Ex Sin@8 p xD, 8x, 0, 1<,Axes � 8True, False<, DisplayFunction� IdentityD;
axes2 = Plot@x Ex Sin@8 p xD, 8x, 0, 1<,AxesStyle�
88RGBColor@0, 1, 0D, [email protected]<,8RGBColor@1, 0, 0D, [email protected]<<, DisplayFunction� IdentityD;
axes3 = Plot@x Ex Sin@8 p xD, 8x, 0, 1<,Frame � True,
Axes � False,
FrameStyle� [email protected], RGBColor@0, 0, 1D<,DisplayFunction� IdentityD;
Show@GraphicsArray@8axes1, axes2, axes3<DD;
0 0.20.40.60.8 1 0.20.40.60.81
-2-1
1
00.20.40.60.81-2-101
PlotLabel allows us to specify a title for the entire graphic, while FrameLabel allows labeling of each edge of the frame. (Note that while rotated text may not display correctly on the screen, it will print correctly.)
180 Mathematica Graphics
Plot@x Ex Sin@8 p xD, 8x, 0, 1<,Frame � True,
Axes � False,
PlotLabel� "Exponential & Sine",
FrameLabel� 8"Time", "Response", None, "Right Label"<D;
0 0.2 0.4 0.6 0.8 1Time
-2
-1
0
1
esnopseR
thgiR
lebaL
Exponential & Sine
TextStyle will set the font for all text in the graphic. We may also use StyleForm to choose fonts individually.
font1 = Plot@x Ex Sin@8 p xD, 8x, 0, 1<,Frame � True,
Axes � False,
PlotLabel� "Sin@xD�x",FrameLabel� 8"Time", "Response"<,TextStyle� 8FontFamily� "Helvetica", FontSize� 10<,DisplayFunction� IdentityD;
font2 = Plot@x Ex Sin@8 p xD, 8x, 0, 1<,Frame � True,
Axes � False,
PlotLabel� StyleForm@Sin@xD� x,FontFamily� "Times",
FontWeight� "Bold", FontSize � 14D,FrameLabel� 8"Time", "Response"<,TextStyle� 8FontFamily� "Helvetica", FontSize� 10<,DisplayFunction� IdentityD;
Mathematica Graphics 181
Show@GraphicsArray@8font1, font2<DD;
0 0.20.40.60.8 1Time
-2-1
01
esnopseR
Sin@xD�x
00.20.40.60.81Time
-2-101
esnopseR
Sin@xDþþþþþþþþþþþþþþþþþþþ
x
The RotateLabel option defaults to True allowing labels on vertical axes to be written parallel to the axis.
AxesOrigin allows us to specify the point in a two-dimensional graphic where the coordinate axes meet. If set outside the range of the plot, the axes will not cross at all.
origin1 = Plot@x Ex Sin@8 p xD, 8x, 0, 1<,AxesOrigin� 80.5, 0.5<,Ticks � 8Automatic, None<,DisplayFunction� IdentityD;
origin2 = Plot@x Ex Sin@8 p xD, 8x, 0, 1<,AxesOrigin� 80.2, 1<,DisplayFunction� IdentityD;
Show@GraphicsArray@8origin1, origin2<DD;
0 0.2 0.4 0.6 0.8 10 0.4 0.6 0.8 1
-2
-1
0
GridLines allows us to specify the placement of a grid under the graphic. The default setting is None. A setting of Automatic will generate uniformly spaced grid lines in both directions, or we can specify the actual positions by giving a list of values. We may specify different grid lines in each direction with the format {xgrid, ygrid}.
grid1 = PlotA Sin@xDþþþþþþþþþþþþþþþþþ
x, 8x, -10, 10<,
GridLines� Automatic, DisplayFunction� IdentityE;
grid2 = PlotA Sin@xDþþþþþþþþþþþþþþþþþ
x, 8x, -10, 10<, GridLines�
88-10, -5, -2.5, 2.5, 5, 10<, None<, DisplayFunction� IdentityE;
grid3 = PlotA Sin@xDþþþþþþþþþþþþþþþþþ
x, 8x, -10, 10<,
GridLines� 8Range@-10, 10D, Automatic<, DisplayFunction� IdentityE;
182 Mathematica Graphics
Show@GraphicsArray@8grid1, grid2, grid3<DD;
-10 -5 5 10-0.2
0.20.40.60.81
-10-5 5 10-0.2
0.20.40.60.81
-10 -5 5 10-0.2
0.20.40.60.81
8.1.7Å Colors
With Background, DefaultColor, and PlotStyle we control the colors of various aspects of the graphic. Note that we explicitly load the Graphics‘Colors‘ package so that we may use names instead of RGB specifications. DefaultColor, the color to use for lines, points, tick marks, etc., can be set to any valid color specification. When set to Automatic, a color complementary to the color of the background, which can be set with Background, is used.
Needs@"Graphics‘Colors‘"D
Plot@2 x5 - 3 x4 + 10 x2 - 2 x - 1, 8x, -2, 2<,Background� SkyBlue,
DefaultColor� RosyBrown,
PlotStyle� [email protected], Maroon<<D;
-2 -1 1 2
-10
-5
5
10
15
8.1.8Å Spacing
PlotRegion allows us to specify how much blank space to leave around the graphic. A setting of the form {{sxmin, sxmax}, {symin, symax}} specifies the region, in scaled coordinates, that the plot should fill in the final display area. When set to Automatic, the plot will fill the final display area.
defaultregion=
Plot@2 x5 - 3 x4 + 10 x2 - 2 x - 1, 8x, -2, 2<, Background� SkyBlue,
PlotLabel� StyleForm@"Quintic", FontFamily� "Times",
FontWeight� "Bold", FontSize � 14D, DisplayFunction� IdentityD;
Mathematica Graphics 183
largerregion= Plot@2 x5 - 3 x4 + 10 x2 - 2 x - 1, 8x, -2, 2<,Background� SkyBlue, PlotLabel� StyleForm@"Quintic",FontFamily� "Times", FontWeight� "Bold", FontSize � 14D,
PlotRegion� 880.1, 0.9<, 80.1, 0.9<<, DisplayFunction� IdentityD;
Show@GraphicsArray@8defaultregion, largerregion<DD;
-2 -1 1 2
-10-5
51015
Quintic
-2 -1 1 2-10-5
51015
Quintic
8.2 Plot3D Options
8.2.1Å Basics
Many of the options controlling three-dimensional graphics are identical to those used in the two-dimensional case.
AspectRatio, Background, DefaultFont, PlotLabel, DefaultColor, and AxesStyle are options that are used identically with Plot3D and Plot, while AxesLabel, AxesStyle, PlotRange, and PlotRegion are called identically but extended to three dimensions via the addition of a third coordinate.
8.2.2Å Point Sampling
Unlike Mathematica’s two-dimensional plotting algrorithm, three-dimensional plots have no built-in method for sampling more points where necessary to achieve an accurate plot. Plot3D will choose uniformly distributed points along the x-range and the y-range and plot only these values. The option PlotPoints, with default value 15, sets the number of divisions to use. PlotPoints can also be set to an ordered pair, denoting the number of points to use in each direction.
184 Mathematica Graphics
Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<, PlotPoints� 810, 35<D;
-2
0
2
-2
0
2
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
8.2.3Å Lighting, Color, and Other Drawing Options
When plotting in three dimensions, several more color options are available.
AmbientLight and LightSources allow us to shade the three-dimensional graphic with external light. AmbientLight is by default white but can be set to any gray level or color specification. By setting a higher gray level, the graphics appears light and washed-out.
Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<, AmbientLight� SlateGrayD;
-2
0
2
-2
0
2
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
LightSources allows us to create an arbitrary number of lights in arbitrary colors and shine them from arbitrary locations. By default, one red, one green, and one blue light shine from the points {1., 0., 1.}, {1., 1., 1.}, and {0., 1., 1.}, respectively. Here we plot a surface with two light sources shining on it, one green and one blue.
Mathematica Graphics 185
Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<,LightSources� 888-1, 0, 1<, Green<, 881, 0, 1<, Blue<<D;
-2
0
2
-2
0
2
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
A more direct way of applying color to a surface is via the ColorFunction option. ColorFunction specifies a function to apply to function z values to determine the color of a particular x-y region. Since Hue, when called with only one argument, will run cyclically through the entire color wheel, it is suitable for use with ColorFunction.
cf1 = Plot3D@Sin@x yD, 8x, -3, 3<, 8y, -3, 3<,ColorFunction� Hue, PlotPoints� 20, DisplayFunction� IdentityD;
mycolor@z_D := RGBColor@Random@D, Random@D, Random@DD;
cf2 = Plot3D@Sin@x yD, 8x, -3, 3<, 8y, -3, 3<,ColorFunction� mycolor, PlotPoints� 20, DisplayFunction� IdentityD;
We may also turn off the lighting altogether.
cf3 = Plot3D@Sin@x yD, 8x, -3, 3<, 8y, -3, 3<,Lighting� False, PlotPoints� 20, DisplayFunction� IdentityD;
Show@GraphicsArray@8cf1, cf2, cf3<DD;
-202
-202-1-0.50
0.51
-1-0.500.51
-202
-202-1-0.50
0.51
-1-0.500.51
-202
-202-1-0.50
0.51
-1-0.500.51
We may also specify a function to apply to each x-y pair to indicate the color of each region. Use the format Plot3D[{f, s}, {x, xmin, xmax}, {y, ymin, ymax}], where s is the shading function. s can be any expression that yields a valid color specification.
186 Mathematica Graphics
shade1 = Plot3DA9SinA�!!!!!!!!!!!!!!x2 + y2 E, Blue=, 8x, -3, 3<,8y, -3, 3<, PlotPoints� 20, DisplayFunction� IdentityE;
shade2 = Plot3DA9SinA�!!!!!!!!!!!!!!x2 + y2 E, If@Sin@x - Cos@yDD > 0.5, Blue, RedD=,8x, -3, 3<, 8y, -3, 3<, PlotPoints� 20, DisplayFunction� IdentityE;
shade3 = Plot3DA9SinA�!!!!!!!!!!!!!!x2 + y2 E, RGBColorAAbsA xþþþþ3E, AbsA y
þþþþ3E, AbsA x + y
þþþþþþþþþþþ6
EE=,8x, -3, 3<, 8y, -3, 3<, PlotPoints� 20, DisplayFunction� IdentityE;
Show@GraphicsArray@8shade1, shade2, shade3<DD;
-202
-202
-0.500.51-0.500.51
-202
-202
-0.500.51-0.500.51
-202
-202
-0.500.51-0.500.51
A related option is ClipFill, which determines how to render areas of the surface that extend beyond the bounding box. By default, these areas are drawn as the rest of the surface. Setting ClipFill to None will show holes in these areas. We may specify one color for all clipped region or a pair of colors, one for the top and one for the bottom.
cf1 = Plot3D@2 Sin@x - Cos@yDD, 8x, -3, 3<,8y, -3, 3<, PlotRange� 8-1, 1<, DisplayFunction� IdentityD;
cf2 = Plot3D@2 Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<,ClipFill� None, PlotRange� 8-1, 1<, DisplayFunction� IdentityD;
cf3 = Plot3D@2 Sin@x - Cos@yDD, 8x, -3, 3<,8y, -3, 3<, ClipFill � [email protected], RGBColor@0, 1, 0D<,PlotRange� 8-1, 1<, DisplayFunction� IdentityD;
Show@GraphicsArray@8cf1, cf2, cf3<DD;
-202
-202-1-0.50
0.51
-1-0.500.51
-202
-202-1-0.50
0.51
-1-0.500.51
-202
-202-1-0.50
0.51
-1-0.500.51
By setting the option HiddenSurface to False, we can see the graphic rendered with transparent polygons.
Mathematica Graphics 187
Plot3D@2 Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<, HiddenSurface� FalseD;
-2
0
2
-2
0
2
-2
-1
0
1
2
-2
-1
0
1
2
8.2.4Å Box, Mesh, and Other Extrinsic Details
Mesh and Boxed may be set only to True or False, and do the obvious things. Since the mesh on the surface simply connects the points plotted by Mathematica, its fineness is controlled by the value of PlotPoints.
Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<, Boxed � False, Mesh � FalseD;
-2
0
2
-2
0
2
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
MeshStyle and BoxStyle allow control of the way these lines are drawn. They take graphics directives as arguments.
188 Mathematica Graphics
Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<,BoxStyle� 8Red, [email protected]<, MeshStyle� [email protected]<D;
-2
0
2
-2
0
2
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
FaceGrids allows us to specify whether to draw grids on the faces of the bounding box and to specify the arrangement of the grid. All and None are the simplest values for this option, but we may choose individual faces and arrange the grid lines. A face is specified by {dirx, diry, dirz}, where two of the elements are 0 and the third is either +1 or –1. The form { face1, face2, … } is used to specify individual faces, and the form {{ face1, {xgrid, ygrid}}, … } is used to specify the grid for individual faces. Grid specifications follow the same format as that of GridLines, so complex styled grids can be drawn.
fg1 = Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<,8y, -3, 3<, FaceGrids� All, DisplayFunction� IdentityD;
fg2 = Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<,FaceGrids� 880, -1, 0<, 80, 0, 1<<, DisplayFunction� IdentityD;
fg3 = Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<, FaceGrids�
8880, -1, 0<, 8Table@8i, RGBColor@1, 0, 0D<, 8i, -3, 3, .25<D,Table@8i, RGBColor@0, 0, 1D<, 8i, -1, 1, .4<D<<<,
DisplayFunction� IdentityD;
Show@GraphicsArray@8fg1, fg2, fg3<DD;
-202
-202-1-0.50
0.51
-1-0.500.51
-202
-202-1-0.50
0.51
-1-0.500.51
-202
-202-1-0.50
0.51
-1-0.500.51
Mathematica Graphics 189
AxesEdge allows control over which three edges of the bounding box the axes are drawn, with specifications given in the form {{xdir, ydir}, {xdir, zdir}, {xdir, ydir}}, with each element being either 1 or –1, indicating whether to draw the axes on the edge of the box with the larger or smaller coordinate — the default being to draw toward the smaller coordinate. Any pair can be replaced by Automatic or None.
Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<,8y, -3, 3<, AxesEdge � 8Automatic, 8-1, 1<, None<D;
-2
0
2
-2
0
2
-2
0
2
8.2.5Å ViewPoint and Related Options
The point in space from which a three-dimensional object is viewed, the ViewPoint, is by default {1.3, –2.4, 2.0} but can be set to any triple. Notebook front ends to Mathematica all have a graphical viewpoint selector, which allows the user to select an orientation visually instead of numerically.
Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<, ViewPoint� 810, 2, 1<D;
-202
-2 0 2
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
ViewCenter allows us to specify where the center of the bounding box is, while ViewVertical specifies the direction in the image that should be vertical. In the following example, the positive y axis is vertical.
v1 = Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<,ViewCenter� 81, 0, 0<, DisplayFunction� IdentityD;
190 Mathematica Graphics
v2 = Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<,ViewVertical� 80, 1, 0<, DisplayFunction� IdentityD;
Show@GraphicsArray@8v1, v2<DD;
-202
-202-1-0.50
0.51-1-0.50
0.51
-2
02
-2
0
2
-1-0.500.5
1-1-0.5
00.51
BoxRatios is similar to AspectRatio but allows control over the ratios of the lengths of all three sides of the bounding box.
Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<, BoxRatios� 89, 4, 1<D;
-2
0
2
-2
02
-1-0.5
00.51
-1-0.5
00.51
By default, the option SphericalRegion is False, allowing the graphic to be drawn as large as possible. By setting it to True, we require that the image be scaled so that a sphere drawn around the bounding box will still fit in the display area. This control is useful when creating animations. Since Mathematica will try to make each graphic as large as possible, multiple graphics generated to create a movie may be drawn to different scales, ruining the effect of the animation. Setting SphericalRegion to True guarantees that all frames will be drawn the same size.
Mathematica Graphics 191
8.3 ContourPlot and DensityPlot Options
ContourPlot and DensityPlot generally take options available in Plot and Plot3D. The only exceptions are several options dealing specifically with ContourPlot. ContourLines controls the drawing of explicit contour lines. By default, this option is True. Contours allows us to choose the specific contours plotted. This option may be set to an integer n, which will produce n equally spaced contours between the minimum and maximum z values, or to an explicit list of z values. The style in which contour lines are drawn is specified in ContourStyle.
ContourPlot@Sin@x - Cos@yDD, 8x, -3, 3<,8y, -3, 3<, ContourLines� False, Contours� 20D;
-3 -2 -1 0 1 2 3
-3
-2
-1
0
1
2
3
ContourShading defaults to True. Setting it to False will leave the areas between contour lines blank. ContourShading also lets us specify the color between contour lines by using a list of color specifications.
192 Mathematica Graphics
ContourPlot@ Sin@xD - Cos@yD, 8x, -2, 2<, 8y, -2, 2<,ContourShading -> [email protected], [email protected]<D;
-2 -1 0 1 2
-2
-1
0
1
2
8.4 ParametricPlot and ParametricPlot3D Options
ParametricPlot has the same list of options as Plot.
Mathematica Graphics 193
ParametricPlot@8t Cos@tD, t Sin@tD<, 8t, 0, 4 p<,PlotStyle� 88RGBColor@0, 1, 0D, [email protected]<D<<,Frame � True, Axes � False, AspectRatio� Automatic,
FrameStyle� [email protected], [email protected], 0.2, 0.1D<D;
-10 -5 0 5 10
-10
-7.5
-5
-2.5
0
2.5
5
7.5
ParametricPlot3D has a few minor differences from Plot3D. ParametricPlot3D lacks the options Mesh, MeshStyle, and HiddenSurface, and has one new option, RenderAll.
Large graphics can take a large amount of RAM to display— sometimes more memory than is available, especially if we are trying to create an animation in which each frame is a complicated Mathematica graphic.
RenderAll, an option for Graphics3D and ParametricPlot3D, which is True by default, allows the option of generating PostScript only for those polygons that will be visible in the final image. The resulting image contains significantly less PostScript code and therefore takes less memory to display and renders faster, but requires considerably more time to create.
8.5 Ticks
Although Mathematica will usually supply useful tick marks on graphics, much customization is possible. Ticks is an option for all graphics functions. With Ticks -> None, no tick marks are drawn on the axes. With Ticks -> Automatic, tick marks are placed automatically. Ticks -> {xticks, yticks, … } specifies tick mark options separately for each axis.
194 Mathematica Graphics
Plot@BesselJ@1, xD, 8x, 0, 10<, Ticks � 8None, Automatic<D;
-0.2
0.2
0.4
0.6
There are several levels of detail at which we may specify the properties of tick marks. The simplest is to supply a list of positions. Note that the list of positions can be generated by a Range or Table command, though this method may be limiting should we change the domain used in the plot.
Plot@BesselJ@1, xD, 8x, 0, 10<,Ticks � 880, 2, 4, 6, 7, 8, 9, 10<, [email protected], 0.5, 0.1D<D;
2 4 6 7 8 9 10
-0.2
-0.1
0.1
0.2
0.3
0.4
0.5
We may also specify the label associated with each tick mark, the length of the mark (both how far above and below the axes it extends), and the style in which to draw the mark. The following is a list of allowable configurations for a tick mark. Note that you may vary among these throughout one list of tick marks.
Mathematica Graphics 195
position
8position, label<
8position, label, length<
8position, label, length, 8style<<
8position, label, 8positive length, negative length<<
8position, label, 8positive length, negative length<, 8style<<
General::spell1 : Possible spelling error: new symbol
name "position" is similar to existing symbol "Position".
General::spell1 : Possible spelling error: new
symbol name "label" is similar to existing symbol "Label".
General::spell1 : Possible spelling error: new symbol
name "length" is similar to existing symbol "Length".
General::stop : Further output of
General::spell1 will be suppressed during this calculation.
88position<, 88position, label<<,
88position, label, length<<, 88position, label, length, 8style<<<,
88position, label, 8length positive, length negative<<<,
88position, label, 8length positive, length negative<, 8style<<<<
Plot[ BesselJ[1, x], {x, 0, 10}, Ticks �{{{1.8,"max", 0.05,{Thickness[0.01],Red}}, {5.3,"min", 0.05,{Thickness[0.01],Red}}, {8.5,"max", 0.05,{Thickness[0.01],Red}}}, {{-0.35,"mark ",{0,0.02},{Thickness[0.01],Blue}}, {0.58,"mark ",{0,0.02},{Thickness[0.01],Blue}}, {0.28,"mark ",{0,0.02},{Thickness[0.01],Blue}} } }];
max min max
mark
mark
mark
In this way we can add explicitly known information to a graphic, but Ticks is also designed to work in a more general fashion. Ticks can take as its arguments functions that take as arguments the minimum and maximum range for an axis and return a valid tick specification. Using this form, the specifications are not tied to explicit
196 Mathematica Graphics
ranges. Here we define two tick specifications. Each function draws unlabeled blue ticks every unit and larger, labeled blue ticks every 5 or 10 units, respectively.
myticks5[min_, max_] :=Join[
Table[{x, x, 0.05, {RGBColor[0,0,1]}}, {x, 0, Ceiling[max],5}],
Table[{x, x, 0.05, {RGBColor[0,0,1]}}, {x,0, Floor[min],-5}],
Table[{x, "", 0.02, {RGBColor[0,0,1]}}, {x, Floor[min], Ceiling[max]}]
];
myticks10[min_, max_] :=Join[
Table[{x, x, 0.05, {RGBColor[0,0,1]}}, {x, 0, Ceiling[max],10}],
Table[{x, x, 0.05, {RGBColor[0,0,1]}}, {x,0, Floor[min],-10}],
Table[{x, "", 0.02, {RGBColor[0,0,1]}}, {x, Floor[min], Ceiling[max]}]
];
Plot@x [email protected] p xD2, 8x, -20, 20<,Ticks � 8myticks10, myticks5<,PlotPoints� 75D;
10 20-10-20
5
10
15
-5
-10
-15
Mathematica Graphics 197
Show[%, PlotRange -> {{-30, 30}, {-25, 25}}];
10 20 30-10-20-30
5
10
15
20
25
-5
-10
-15
-20
-25
When a frame is drawn around the plot, FrameTicks is used to specify ticks to be drawn on the frame. FrameTicks takes the same arguments as Ticks.
Plot@x [email protected] p xD2, 8x, -20, 20<,Frame � True,
FrameTicks� 8myticks10, myticks5, None, None<D;
0 10 200-10-20
0
5
10
15
0
-5
-10
-15
We use exactly the same specifications for three-dimensional plots, contour plots, and density plots.
198 Mathematica Graphics
[email protected] [email protected] x yD, 8x, -30, 30<, 8y, -30, 30<,PlotPoints� 20, Ticks � 8myticks10, myticks10, Automatic<D;
010
2030
0-10
-20-30
010
2030
0-10
-20-30
-0.2
0
0.2
-0.2
0
0.2
8.6 Prolog, Epilog, and Rectangle
Epilog is an option to every graphics function that allows arbitrary graphics to be rendered after the main graphic has been rendered. Prolog specifies arbitrary graphics primitives and directives to be rendered before the main graphics is rendered. For simplicity, we will look only at Epilog.
Any two-dimensional graphic can be superimposed on any two-dimensional plot. Arrow, Text, and several graphics directives can be combined to add information to this plot. We can specify the font by using StyleForm within a Text primitive.
Mathematica Graphics 199
Plot@4 x2 + 12 x - 10, 8x, -10, 10<,Epilog � 8
Text@StyleForm@"W", FontSize � 18, FontWeight� "Bold"D, 80, 600<D,Text@StyleForm@"D", FontSize � 18, FontWeight� "Bold"D, 812, 0<D<,PlotRange� AllD;
-10 -5 5 10
100
200
300
400
500
W
D
200 Mathematica Graphics
Plot@x [email protected] xD2, 8x, -10, 10<, Epilog � 8RGBColor@1, 0.5, 0.6D,Text@StyleForm@"Minima",
FontFamily� "Helvetica", FontWeight� "Bold",
FontSize� 12D, 86, -8.5<D, Text@StyleForm@"Maxima",FontFamily� "Helvetica",
FontWeight� "Bold", FontSize� 12D, 8-6, 8.5<D,RGBColor@0, 0, 1D,Arrow@8-6, 7<, 8-7, 0<D, Arrow@8-6, 7<, 8-3.5, 0<D,
Arrow@8-6, 7<, 82, 2<D, Arrow@8-6, 7<, 85.25, 5.45<D,Arrow@8-6, 7<, 88.75, 8.9<D, Arrow@86, -7<, 8-8.8, -8.75<D,Arrow@86, -7<, 8-5.35, -5.25<D, Arrow@86, -7<, 8-2.1, -1.85<D,Arrow@86, -7<, 83.5, 0<D, Arrow@86, -7<, 87, 0<D<D;
-10 -5 5 10
-7.5
-5
-2.5
2.5
5
7.5
Minima
Maxima
More generally, we can include any graphic in any other graphic using Rectangle. The form Rectangle[{xmin, ymin}, {xmax, ymax}] represents a filled rectangle oriented parallel to the axes. The more general form Rectangle[{xmin, ymin}, {xmax, ymax}, graphics] represents a rectangle filled with the arbitrary graphics.
inset = PlotA Sin@xDþþþþþþþþþþþþþþþþþ
x, 8x, -10, 10<,
Frame � True,
Ticks � None,
FrameTicks� None,
FrameStyle� [email protected], [email protected]<,PlotStyle� [email protected]<D<<,DisplayFunction� IdentityE;
Mathematica Graphics 201
Plot@x Sin@xD2, 8x, -10, 10<,PlotStyle� RGBColor@0, 0, 1D,
Epilog � Rectangle@8-9, 3<, 8-2, 8<, insetDD;
-10 -5 5 10
-7.5
-5
-2.5
2.5
5
7.5
We can create functions which return graphics objects. Below is a function called CircledDigit, which creates labels we can place on any graphic.
CircledDigit@d_String, 8x_, y_<D :=
8Hue@0D, Disk@8x, y<, 0.9D, GrayLevel@1D, Text@d, 8x, y<D<
Plot@x Sin@xD2, 8x, -10, 10<,Epilog � 8
CircledDigit@"1", 84.9, 5<D,CircledDigit@"2", 8-9.4, 0.5<D<,
AspectRatio� AutomaticD;
-10 -5 5 10
-7.5
-5
-2.5
2.5
5
7.5
1
2
202 Mathematica Graphics
9Animations
9.1 Creating Animations in Notebooks
All versions of Mathematica can create animations. Animation results when a series of Mathematica graphics are displayed quickly in succession to create the illusion of smooth movement.
Mathematica provides many features to aid this process. Here is a simple example. The command Table creates an array of results by iterating commands. Here we will create ten different plots of Sin[a x ], letting a vary. Notice that we explicitly set a PlotRange because the default for Mathematica is to pick a new PlotRange for each frame of the animation, which could ruin the animation effect.
Table@Plot@Sin@a xD, 8x, 0, 10<, PlotRange� 880, 10<, 8-1, 1<<D,
8a, 1, 5, 0.5<D;
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
Mathematica Graphics 203
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
204 Mathematica Graphics
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
Mathematica Graphics 205
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
(The cells of the animation have been deleted to save space. Enter the code into Mathematica to see the animation.)
If you have three-dimensional graphics with different viewpoints, you should similarly set SphericalRegion to be True to ensure that the scaling of different plots are the same.
Table@Plot3D@H2 + Sin@xDL Cos@2 yD,8x, -2, 2<, 8y, -3, 3<, BoxRatios� 81, 5, 1<, Boxed � False,
Axes � False, ViewPoint� 8a, 1, 1<D, 8a, 0, 1, 0.1<D;
206 Mathematica Graphics
Mathematica Graphics 207
208 Mathematica Graphics
Mathematica Graphics 209
210 Mathematica Graphics
Mathematica Graphics 211
(The cells of the animation have been deleted to save space. Enter the code into Mathematica to see the animation.)
Although all that is necessary to create an animation in Mathematica is a series of graphics, a number of animation functions are defined in the package Graphics‘Animation‘.
Needs["Graphics‘Animation‘"]
? Graphics‘Animation‘*
Animate MovieParametricPlot SpinDistanceAnimation MoviePlot SpinOriginAnimationFunction MoviePlot3D SpinRangeFrames RasterFunction SpinShowMovieContourPlot RotateLights SpinTiltMovieDensityPlot ShowAnimation
MoviePlot[ f[x, t], {x, x0, x1}, {t, t0, t1} ] will animate plots of f[x, t] regarded as a function of x, with t serving as the animation (or time) variable.
MoviePlot@Sin@a xD, 8x, 0, 10<, 8a, 1, 5, 0.5<D
2 4 6 8 10
-1
-0.5
0.5
1
212 Mathematica Graphics
2 4 6 8 10
-1
-0.5
0.5
1
2 4 6 8 10
-1
-0.5
0.5
1
2 4 6 8 10
-1
-0.5
0.5
1
Mathematica Graphics 213
2 4 6 8 10
-1
-0.5
0.5
1
2 4 6 8 10
-1
-0.5
0.5
1
2 4 6 8 10
-1
-0.5
0.5
1
214 Mathematica Graphics
2 4 6 8 10
-1
-0.5
0.5
1
2 4 6 8 10
-1
-0.5
0.5
1
(The cells of the animation have been deleted to save space. Enter the code into Mathematica to see the animation.)
Notice that MoviePlot is essentially a shortcut for using the Table command. One difference is that MoviePlot automatically uses the same PlotRange for each frame.
Another interesting animation results by varying the viewpoint, thereby creating a revolution or a flyby of an object. SpinShow automates this process.
SpinShow@ Graphics3D@ Stellate@ Icosahedron@D D D,Boxed � FalseD;
Mathematica Graphics 215
216 Mathematica Graphics
Mathematica Graphics 217
218 Mathematica Graphics
Mathematica Graphics 219
220 Mathematica Graphics
Mathematica Graphics 221
222 Mathematica Graphics
Mathematica Graphics 223
224 Mathematica Graphics
Mathematica Graphics 225
226 Mathematica Graphics
(The cells of the animation have been deleted to save space. Enter the code into Mathematica to see the animation.)
Other effects can be achieved by varying colors, options, ranges, and so on.
Mathematica Graphics 227
9.2 MathLive
MathLive is a real-time visualization environment that can be used with graphics generated within Mathematica.
As we have seen, Mathematica animations are a collection of frames that are viewed in succession to give the impression of motion. MathLive allows us to load a graphics object created in Mathematica from a file or directly through a MathLink connection and manipulate the object in real time.
9.2.1Å Files
You can use MathLive with files in various ways. One is the 3-Script file interface to Mathematica. Using the ThreeScript package, you can write a representation of three-dimensional graphics to a standard text file format. This may be read directly into MathLive. However, because of the limitations of the 3-Script format, you will normally find that the use of MathLink is preferable, since considerably more information about the surface can be passed directly via MathLink. Another type of file is the texture file, which allows you to add textures to your graphics. MathLive also has its own file type called an ASCII mesh file. MathLive scenes can be saved in this format and reloaded later in new sessions of MathLive.
Needs@"Graphics‘ThreeScript‘"D
bell = Plot3D@Exp@-x2 - y2D, 8x, -2, 2<, 8y, -2, 2<,PlotPoints� 25, Boxed � False, Axes � FalseD;
ThreeScript@"bell.ts", bellD
bell.ts
Once we have a 3-Script file, we can launch MathLive and open the file.
228 Mathematica Graphics
9.2.2Å Using MathLive with MathLink
MathLive has been developed to handle the MathLink communications standard for direct communication to Mathematica. By using MathLink, graphics may be sent directly from Mathematica to MathLive. This method is faster and easier than using 3-Script files.
First we need to tell Mathematica where MathLive resides.
$LivePath =
"Red:Mathematica Files:Application Libraries:MathLive:MathLive"
Red:Mathematica Files:Application Libraries:MathLive:MathLive
Then we can open the MathLink connection in one of two ways. The first is to have Mathematica launch MathLive for us.
live = LinkOpen@$LivePath, LinkMode � LaunchD;
General::spell1 : Possible spelling error: new
symbol name "live" is similar to existing symbol "Olive".
LinkOpen::linke : MathLink error:
the launch failed because the program could not be found
Or we can connect to MathLive directly, if we have already launched it manually.
live = LinkOpen@"MathLive", LinkMode � ConnectD;
Once the MathLink connection is open, we can send three-dimensional graphics to MathLive to be manipulated.
Mathematica Graphics 229
lumpy = Plot3D@Sin@x2D Sin@y2D, 8x, -p, p<, 8y, -p, p<,PlotPoints� 30, Boxed � False, Axes � FalseD;
LinkWrite@live, lumpyD;
Now the graphic we called lumpy has been sent to MathLive.
230 Mathematica Graphics
9.2.3Å Capabilities
Once we have a graphic in MathLive there are a number of ways we can view and manipulate it. First, just by clicking on the graphic and moving the mouse, we can impart rotation in the direction that we moved the mouse with the speed of the rotation relative to how fast we moved the mouse.
We can also manipulate how the graphic is visualized and how we view the graphic with commands built into MathLive. For instance, we can change the surface lighting, make the surface solid or wire frame, or map a texture onto the graphic.
Some of the different animations available include rotating or tilting the camera, flying past the graphic or doing corkscrews around the graphic, and varying the lighting around the graphic.
Animations can also be exported into QuickTime files.
Mathematica Graphics 231
Exercises: Animation
Í Using Table, Do, or MoviePlot, create a two-dimensional animation of a function that changes over time. Be sure that the domain and range remain the same throughout the animation.
myanim = Table@Plot@Sin@k xD, 8x, 0, 3 p<,PlotRange� 880, 10<, 8-1, 1<<, PlotPoints� 50D, 8k, 1, 12<D;
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
232 Mathematica Graphics
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
Mathematica Graphics 233
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
234 Mathematica Graphics
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
Mathematica Graphics 235
2 4 6 8 10
-1
-0.75
-0.5
-0.25
0.25
0.5
0.75
1
The line above will generate the animation, but for better viewing on the printed page, we use GraphicsArray to view all the frames at once.
Show@GraphicsArray@Partition@myanim, 3DDD;
2 4 6 810-1-0.75-0.5-0.25
0.250.50.751
2 4 6 810-1-0.75-0.5-0.25
0.250.50.751
2 4 6 810-1-0.75-0.5-0.25
0.250.50.751
2 4 6 810-1-0.75-0.5-0.25
0.250.50.751
2 4 6 810-1-0.75-0.5-0.25
0.250.50.751
2 4 6 810-1-0.75-0.5-0.25
0.250.50.751
2 4 6 810-1-0.75-0.5-0.25
0.250.50.751
2 4 6 810-1-0.75-0.5-0.25
0.250.50.751
2 4 6 810-1-0.75-0.5-0.25
0.250.50.751
2 4 6 810-1-0.75-0.5-0.25
0.250.50.751
2 4 6 810-1-0.75-0.5-0.25
0.250.50.751
2 4 6 810-1-0.75-0.5-0.25
0.250.50.751
Í Choose your favorite three-dimensional graphic from this set of exercises (or make a new one) and create an animation using SpinShow. (Remember to load the Graphics‘Animation‘ package first!)
Needs@"Graphics‘Animation‘"D
236 Mathematica Graphics
simpleplot3d = Plot3D@Sin@x - Cos@yDD, 8x, -3, 3<, 8y, -3, 3<,Boxed -> False, Axes -> FalseD;
my3danim = SpinShow@simpleplot3d, Frames � 12D
Mathematica Graphics 237
238 Mathematica Graphics
Mathematica Graphics 239
240 Mathematica Graphics
Mathematica Graphics 241
242 Mathematica Graphics
(The cells of the animation have been deleted to save space. Enter the code into Mathematica to see the animation.)
Mathematica Graphics 243
244 Mathematica Graphics
10Creating Graphics Functions
There are times when we will want to create a specific type of plot that Mathematica does not directly support. We can eventually make up any kind of graphics we want within Mathematica. Sometimes we’ll be pleasantly surprised to find out that what we want is just a variant of a type of plot Mathematica already knows, other times we will have to start from the basic building blocks of points and lines.
10.1 InequalityPlot
10.1.1Å Basis
As an exercise, we will use Mathematica to create a flexible function that will draw inequality plots; that is, plots where the area above or below the curve are shaded.
To create an inequality plot seems dificult until we realize that we can already achieve the desired results fairly easily using ContourPlot. ContourPlot[f, {x, xmin, xmax}, {y, ymin, ymax}]generates a contour plot of f as a function of x and y. Several options allow us to change ContourPlot into what we need. Contours is an option that lists the specific contours to plot. For example, the following example specifically draws contours that are the solutions to 4 x2 - y = 1, 4 x2 - y = 2, and 4 x2 - y = 3. Note that by default Mathematica shades the areas between the contour lines.
Mathematica Graphics 245
ContourPlot@4 x2 - y, 8x, -3, 3<, 8y, -3, 3<,Contours � 81, 2, 3<D;
-3 -2 -1 0 1 2 3
-3
-2
-1
0
1
2
3
When we tell Mathematica to draw only the contour at 0, the resulting graph represents 4 x2 - y < 0, which can be rewritten 4 x2 < y.
246 Mathematica Graphics
ContourPlot@4 x2 - y, 8x, -3, 3<, 8y, -3, 3<,Contours � 80.<, PlotPoints� 50D;
-3 -2 -1 0 1 2 3
-3
-2
-1
0
1
2
3
What remains is to define a new function that accepts inequalities as input and translates that format into a standard ContourPlot command, and then to add appropriate options to our new function. We’ll call our new function InequalityPlot, and we will want it to transform something that looks like a < b into a - b. The prefix form of a < b is Less[a,b], so we tell InequalityPlot to expect input in this form. We also require the user to give an x- and y-range to the function, which is passed on to ContourPlot. To give the user control over all of the ContourPlot options, we include an argument for arbitrary options.
InequalityPlot@a_ < b_, xrange_List, yrange_List, opts___D :=
ContourPlot@a - b, xrange, yrange,
PlotPoints� 45, Contours � 80.<, optsD;
Mathematica Graphics 247
InequalityPlot@x2 < y, 8x, -3, 3<, 8y, -3, 3<D;
-3 -2 -1 0 1 2 3
-3
-2
-1
0
1
2
3
Creating our own coloring function that will return gray inside the area bounded by the curve and white outside will make this start to look like a “finished” graphic.
Clear@InequalityPlot, mycolorfunctionD;mycolorfunction@x_D := If@x == 0, [email protected], GrayLevel@1DD;
InequalityPlot@a_ < b_, xrange_List, yrange_List, opts___D :=
ContourPlot@a - b, xrange, yrange,
PlotPoints� 45, Contours � 80.<,ColorFunction� mycolorfunction, optsD;
248 Mathematica Graphics
InequalityPlot@x2 < y, 8x, -3, 3<, 8y, -3, 3<D;
-3 -2 -1 0 1 2 3
-3
-2
-1
0
1
2
3
To make this look more like the inequalities we graphed in school, we should move the axes to the center of the graph and remove the frame.
Clear@InequalityPlot, mycolorfunctionD;mycolorfunction@x_D := If@x == 0, [email protected], GrayLevel@1DD;
InequalityPlot@a_ < b_, xrange_List, yrange_List, opts___D :=
ContourPlot@a - b, xrange, yrange,
PlotPoints� 45, Contours � 80.<,ColorFunction� mycolorfunction, Frame � False,
Axes � True, AxesOrigin� 80, 0<, optsD;
Mathematica Graphics 249
InequalityPlot@x2 < y, 8x, -3, 3<, 8y, -3, 3<D;
-3 -2 -1 1 2 3
-3
-2
-1
1
2
3
This didn’t quite work as expected, since the axes are now hidden behind the graph. Recall that FilledPlot has an option AxesFront that takes care of this problem. Looking at the package we see that what the author did was define the option AxesFront for Graphics. If we load this package, we can make use of this solution for our function. Since AxesFront is an option for graphics functions and not plotting functions, we must rearrange our function to create a graphics object and then use Show to display it with the AxesFront option set to True.
Needs@"Graphics‘FilledPlot‘"D
Clear@InequalityPlot, mycolorfunctionD;mycolorfunction@x_D := If@x == 0, [email protected], GrayLevel@1DD;
InequalityPlot@a_ < b_, xrange_List, yrange_List, opts___D :=
Show@Graphics@ContourPlot@a - b, xrange, yrange,
PlotPoints� 45, Contours � 80.<,ColorFunction� mycolorfunction, Frame � False,
Axes � True, AxesOrigin� 80, 0<,DisplayFunction� Identity, optsDD,
AxesFront� True, DisplayFunction� $DisplayFunctionD;
250 Mathematica Graphics
InequalityPlot@x2 < y, 8x, -3, 3<, 8y, -3, 3<D;
-3 -2 -1 1 2 3
-3
-2
-1
1
2
3
-3 -2 -1 1 2 3
-3
-2
-1
1
2
3
Note that we haven’t given the function a way to accept other forms, such as a > b, a � b, etc. Of course, the user can insert a minus sign in the appropriate place, but we can easily automate this feature by providing several cases in our definition. Using patterns so that Mathematica treats various inputs differently is one of the most powerful techniques available within Mathematica. We also customize the way the curve is drawn using ContourStyle, drawing a dashed line when “<” is used and solid line when “�” is used.
10.1.2Å Final InequalityPlot Code
Needs@"Graphics‘Master‘"D
Clear@InequalityPlot, mycolorfunctionD;mycolorfunction@x_D := If@x == 0, [email protected], GrayLevel@1DD;
SetOptions@ContourPlot,PlotPoints� 45,
Contours � 80.<,Frame � False,
Axes � True,
AxesOrigin� 80, 0<,DisplayFunction� IdentityD;
InequalityPlot@a_ < b_, xrange_List, yrange_List, opts___D :=
Show@Graphics@ContourPlot@a - b, xrange, yrange, opts,
ColorFunction� mycolorfunction,
ContourStyle�
Mathematica Graphics 251
[email protected], RGBColor@1, 0, 0D, [email protected]<D<DD,opts, AxesFront� True, DisplayFunction� $DisplayFunctionD;
InequalityPlot@a_ > b_, xrange_List, yrange_List, opts___D :=
Show@Graphics@ContourPlot@-Ha - bL, xrange, yrange, opts,
ColorFunction� mycolorfunction,
ContourStyle�
[email protected], RGBColor@1, 0, 0D, [email protected]<D<DD,opts, AxesFront� True, DisplayFunction� $DisplayFunctionD;
InequalityPlot@a_ � b_, xrange_List, yrange_List, opts___D :=
Show@Graphics@ContourPlot@-Ha - bL, xrange, yrange, opts,
ColorFunction� mycolorfunction,
ContourStyle� [email protected], RGBColor@1, 0, 0D<DD,opts, AxesFront� True, DisplayFunction� $DisplayFunctionD;
InequalityPlot@a_ � b_, xrange_List, yrange_List, opts___D :=
Show@Graphics@ContourPlot@a - b, xrange, yrange, opts,
ColorFunction� mycolorfunction,
ContourStyle� [email protected], RGBColor@1, 0, 0D<DD,opts, AxesFront� True, DisplayFunction� $DisplayFunctionD;
InequalityPlot@a_ == b_, xrange_List, yrange_List, opts___D :=
Show@Graphics@ContourPlot@a - b, xrange, yrange, opts,
ContourStyle� [email protected], RGBColor@1, 0, 0D<DD,opts, AxesFront� True, DisplayFunction� $DisplayFunctionD;
equalcolorfunction@x_D := GrayLevel@1D;
InequalityPlot@a_ == b_, xrange_List, yrange_List, opts___D :=
Show@Graphics@ContourPlot@a - b, xrange, yrange, opts,
ContourStyle� [email protected], RGBColor@1, 0, 0D<,ColorFunction� equalcolorfunctionDD, opts,
AxesFront� True, DisplayFunction� $DisplayFunctionD;
notequalcolorfunction@x_D := [email protected];
InequalityPlot@a_ � b_, xrange_List, yrange_List, opts___D :=
Show@Graphics@ContourPlot@a - b, xrange, yrange, opts,
ColorFunction� notequalcolorfunction,
ContourStyle� [email protected], GrayLevel@1D<DD,opts, AxesFront� True, DisplayFunction� $DisplayFunctionD;
10.1.3Å Examples
InequalityPlot@x � 2 y, 8x, -3, 3<, 8y, -3, 3<D;
252 Mathematica Graphics
-3 -2 -1 1 2 3
-3
-2
-1
1
2
3
-3 -2 -1 1 2 3
-3
-2
-1
1
2
3
InequalityPlot@x2 � -y2 + 25, 8x, -10, 10<, 8y, -10, 10<D;
-10 -5 5 10
-10
-5
5
10
-10 -5 5 10
-10
-5
5
10
Mathematica Graphics 253
variations =
88InequalityPlot@x2 � y2 - 20, 8x, -10, 10<, 8y, -10, 10<,DisplayFunction� IdentityD,
InequalityPlot@x2 < y2 - 20, 8x, -10, 10<, 8y, -10, 10<,DisplayFunction� IdentityD,
InequalityPlot@x2 > y2 - 20, 8x, -10, 10<, 8y, -10, 10<,DisplayFunction� IdentityD<,
8InequalityPlot@x2 � y2 - 20, 8x, -10, 10<, 8y, -10, 10<,DisplayFunction� IdentityD,
InequalityPlot@x2 � y2 - 20, 8x, -10, 10<, 8y, -10, 10<,DisplayFunction� IdentityD,
InequalityPlot@x2 == y2 - 20, 8x, -10, 10<, 8y, -10, 10<,DisplayFunction� IdentityD<<;
Show@GraphicsArray@variationsDD;
-10 -5 5 10
-10
-5
5
10
-10 -5 5 10
-10
-5
5
10
-10 -5 5 10
-10
-5
5
10
-10 -5 5 10
-10
-5
5
10
-10 -5 5 10
-10
-5
5
10
-10 -5 5 10
-10
-5
5
10
-10 -5 5 10
-10
-5
5
10
-10 -5 5 10
-10
-5
5
10
-10 -5 5 10
-10
-5
5
10
-10 -5 5 10
-10
-5
5
10
-10 -5 5 10
-10
-5
5
10
-10 -5 5 10
-10
-5
5
10
10.2 RiemannPlot
10.2.1Å Code
Needs@"Utilities‘FilterOptions‘"D
RiemannPlot::usage =
"RiemannPlot@f, 8x, xmin, xmax<D generates a plot of
f as a function of x from xmin to xmax, with the area
under the curve approximated by rectangles. ";
254 Mathematica Graphics
Options@RiemannPlotD =
8Subdivisions -> 16,
ShowCurve -> True,
FillColor -> [email protected], Method -> Left,
BorderStyle -> GrayLevel@0D<~Join~Options@PlotD;
SetOptions@RiemannPlot, PlotRange� All,
TextStyle�
8FontFamily� "Helvetica", FontSlant� "Oblique", FontSize � 10<D;
Clear@RiemannPlotD; RiemannPlot@func_, 8var_, min_, max_<,opts___?OptionQD :=
Module@8rects, inc, subs, sc, fillc, outlines, n, meth, disp, area = 0<,
8subs, sc, fillc, outlines, meth, disp< =8Subdivisions, ShowCurve, FillColor, BorderStyle, Method,
DisplayFunction< �. Flatten@8opts<D �. Options@RiemannPlotD;inc = Hmax - minL�subs;
adj = Switch@meth, Left, 0, Right, inc, Midpoint, inc�2D;rects = Flatten@Table@
Harea = area+ # inc;
8fillc, Rectangle@8n, 0<, 8n + inc, #<D, outlines,
Line@88n, 0<, 8n + inc, 0<, 8n + inc, #<, 8n, #<, 8n, 0<<D<L &@func �. var � Hn + adjLD, 8n, min, max - inc, inc<D D;
If@TrueQ@scD,Show@8Graphics@rectsD,
Plot@func, 8var, min, max<, DisplayFunction -> Identity,
Evaluate@FilterOptions@Plot, ##D & ëë
Flatten@8opts, Options@RiemannPlotD<DDD<,FilterOptions@Graphics, ##D & ëë
Flatten@8DisplayFunction � disp, opts,
PlotLabel � "Approximate Area = " <>
ToString@areaD <> "\n Calculated Area = " <> ToString@NIntegrate@func, 8var, min, max<DD, Options@RiemannPlotD<DD,
Show@Graphics@rectsD,FilterOptions@Graphics, ##D & ëë Flatten@8opts,
PlotLabel � "Approximate Area = " <>
ToString@areaD <> "\n Calculated Area = " <> ToString@NIntegrate@func, 8var, min, max<DD, Options@RiemannPlotD<D
DD
D
General::spell1 : Possible spelling error: new
symbol name "area" is similar to existing symbol "Area".
Mathematica Graphics 255
10.2.2Å Examples
Needs@"Graphics‘Master‘"D
? RiemannPlot
RiemannPlot@f, 8x, xmin, xmax<D generates a
plot of f as a function of x from xmin to xmax, with
the area under the curve approximated by rectangles.
Options@ RiemannPlotD
9Subdivisions� 16, ShowCurve� True, FillColor� [email protected],
Method � Left, BorderStyle� GrayLevel@0D, AspectRatio�1
þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþGoldenRatio
,
Axes � Automatic, AxesLabel� None, AxesOrigin� Automatic,
AxesStyle� Automatic, Background� Automatic, ColorOutput� Automatic,
Compiled� True, DefaultColor� Automatic, Epilog � 8<, Frame � False,
FrameLabel� None, FrameStyle� Automatic, FrameTicks� Automatic,
GridLines� None, ImageSize� Automatic, MaxBend � 10.,
PlotDivision� 30., PlotLabel � None, PlotPoints� 25, PlotRange � All,
PlotRegion� Automatic, PlotStyle� Automatic, Prolog� 8<,RotateLabel� True, Ticks � Automatic, DefaultFont� $DefaultFont,
DisplayFunction� $DisplayFunction, FormatType� $FormatType,
TextStyle� 8FontFamily� Helvetica, FontSlant� Oblique, FontSize� 10<=
Method can be Midpoint, Left, or Right. It defaults to Left.
RiemannPlotA Sin@xDþþþþþþþþþþþþþþþþþ
x, 8x, 0.001, p<,
FillColor -> Red,
BorderStyle-> 8Blue<,PlotStyle -> [email protected], Magenta<,Method -> Right,
Subdivisions-> 40E;
0.5 1 1.5 2 2.5 3
0.2
0.4
0.6
0.8
1
ApproximateArea = 1.81152Calculated Area = 1.85094
256 Mathematica Graphics
10.3 Building Primitives
Using similar techniques we can easily build our own graphics primitives. As a simple example, we can create a function that returns an n-sided polygon.
poly@n_IntegerD :=
PolygonATableA9CosA i 2 pþþþþþþþþþþþþn
E, SinA i 2 pþþþþþþþþþþþþn
E=, 8i, 1, n<EE
Show@ Graphics@ poly@5D D, AspectRatio -> Automatic D;
We can easily rewrite poly to accept the position of the center of the polygon.
poly@8x_, y_<, n_IntegerD :=
PolygonATableA9CosA i 2 pþþþþþþþþþþþþn
E + x, SinA i 2 pþþþþþþþþþþþþn
E + y=, 8i, 1, n<EE
Mathematica Graphics 257
Show@ Graphics@8Hue@0D, poly@8-1, 1<, 3D, [email protected], poly@82, 3<, 5D< D,AspectRatio -> Automatic D;
258 Mathematica Graphics