Академический Документы
Профессиональный Документы
Культура Документы
The Plot function works by evaluating the first argument at points in the range specified by the
second argument, and connecting those points with straight lines. In most cases the straight line
segments are so short that plot looks like a smooth curve.
The vertical range of the plot is chosen automatically using built-in heuristics. If you would like to
see a vertical range other than the range that is chosen automatically, you can use the PlotRange
option to specify the vertical range explicitly. For example:
Plot@Sin@xD, 8x, 0, 6 Pi<, PlotRange Ø 80, 1<D
Here is an example showing the use of the Plot3D function to plot Sin@xD for x ranging from 0 to
6 Pi.
Just as the Plot function works by evaluating the first argument at a set of points and connecting
those points with straight lines, the Plot3D function works by evaluating the first argument on a grid
of points and drawing plane surfaces between those points. The second and third arguments in
Plot3D give the variables and the ranges of those variables.
Also, as with Plot, the Plot3D function accepts many options that allow you to control various charac-
teristics of the plot. Here is an example in which the AxesLabel option is used to label two of the
axes, the Mesh option is used to omit the grid lines on the surface, and the ViewPoint option is used
to change the point from which the image is viewed.
Plot3D@x Cos@Pi yD, 8x, - 1, 1<, 8y, 0, 2<,
AxesLabel Ø 8"x", "y", None<, Mesh Ø False, ViewPoint Ø 82, - 1, 2<D
In addition to Plot and Plot3D, the other built-in functions for making plots of numerical functions are
ContourPlot, DensityPlot, ParametricPlot, and ParametricPlot3D. All of these functions use similar
syntax, and have many similar characteristics. For information and examples, see the documenta-
tion for each function.
The ListPlot function is typical of functions used for plotting data. The other built-in functions for
plotting data are ListPlot3D, ListContourPlot, ListDensityPlot., all of which are used for plotting data
arrays rather than lists of data points.
As an example, here is a contour plot of a data array generated using Table. The DataRange option
is used here to specify the range of the independent coordinates (the values of x and y). Without
this specification the axes would be labeled to show a range 1 through 9 in the vertical direction and
1 through 5 in the horizontal direction, since the data are given as a 9 by 5 array. To see this effect,
try the same example without the DataRange option.
data = Table@Sqrt@x ^ 2 + y ^ 2D, 8y, 0, 80, 10.0<, 8x, 0, 60, 15.0<D;
It is instructive to compare this plot with the original data. Note that the first row in the data array
corresponds to the bottom of the plot.
MatrixForm@dataD
Additional Plots
Among the additional graphics functionality included in the kernel , there are commands for making
plots on various logarithmic scales. Here is an example showing the use of the LogPlot function to
make a plot in which the vertical scale is logarithmic.
LogPlot@Exp@x ^ 2D ê H1 + x ^ 2L, 8x, 0, 4<D
Another function BarChart, which can be used for making bar charts. Here is an example showing
the use of theBarChart function.
BarChart@82, 1, 4, 4, 6, 7, 9, 11, 10, 7, 3, 2, 2, 3, 2, 1<D
Here is a plot of the same pair of functions but with several non-default option settings to control the
labels on the plot.
Basic4.nb 3
Graphical elements
All of the points, lines, text, color instructions, and other elements that are used to construct graphi-
cal images are represented in Mathematica using expressions. These expressions are used in
results from Plot and other plotting functions. In many situations, however, it is helpful or necessary
to work with these expressions directly.
Expressions that describe geometrical elements such as points and lines are called graphics primi-
tives. For example, the expression Line[{{1,1},{2,2}}] is a two-dimensional graphics primitive
that specifies a line from the point {1,1} to the point {2,2}.
Expressions that specify color, dashing, line thickness, and other characteristics of geometrical
elements are called graphics directives. The graphics directive Dashing[{0.02}], which was
encountered in the previous section, specifies that subsequent lines should be dashed with dash
segments that have length 0.02 of the width of the plot.
Many characteristics of graphical images can be controlled using graphics options. A number of
graphics options have already been encountered in previous sections this tutorial. For example, the
option Frame->True in two-dimensional graphics specifies that a frame should be drawn around
the plot.
Graphics primitives, graphics directives, and graphics options are collected into graphics
expressions.
The first argument in Graphics is a list of graphics primitives and graphics directives. Here is an
example of a graphics expression with a head of Graphics and with a single Line primitive listed in
the first argument. The Line primitive in this example represents a line from the point {1,3} to the
point {2,2} to the point {3,5} to the point {4,4} to the point {5,7}.
g = Graphics@8Line@881, 3<, 82, 2<, 83, 5<, 84, 4<, 85, 7<<D<, ImageSize Ø SmallD
Here is a more complicated graphics expression demonstrating the use of Line, Point, Text, and
Polygon graphics primitives, Thickness, PointSize, and RGBColor graphics directives, and several
graphics options. A useful way to learn about graphics expressions is to try various changes to
examples such as this one and to observe the effect of your changes on the image that is displayed.
4 Basic4.nb
Here is a more complicated graphics expression demonstrating the use of Line, Point, Text, and
Polygon graphics primitives, Thickness, PointSize, and RGBColor graphics directives, and several
graphics options. A useful way to learn about graphics expressions is to try various changes to
examples such as this one and to observe the effect of your changes on the image that is displayed.
Show@Graphics@8Line@881, 1<, 84, 2<<D, Thickness@0.02`D,
Line@881, 4<, 84, 4<<D, RGBColor@1, 0, 0D, PointSize@0.05`D, Point@84, 1<D,
Polygon@881, 2<, 83, 3<, 81, 3<<D, Text@Style@"Red triangle", FontSize Ø 12D,
82, 3.2`<, Background Ø GrayLevel@1DD, Line@882, 1<, 82, 2<, 83, 1<, 83, 2<<D<,
Background Ø GrayLevel@0.8`D, AspectRatio Ø 1, Axes Ø True,
PlotRange Ø 880, 5<, 80, 5<<DD
Exercises
Exercise 4.1 Plotting a table of functions
Correct the error in this input by enclosing the first argument of Plot in Evaluate. The result should
be a plot of Sin[n x] for five integer values of n.
Plot@Table@Sin@n xD, 8n, 1, 5<D, 8x, 0, 1<D
Plot::plnr :
Table@Sin@n xD, 8n, 1, 5<D is not a machine-size real number at x = 4.166666666666666`*^-8.
Plot::plnr :
Table@Sin@n xD, 8n, 1, 5<D is not a machine-size real number at x = 0.04056699157291579`.
Plot::plnr :
Table@Sin@n xD, 8n, 1, 5<D is not a machine-size real number at x = 0.08480879985937367`.
General::stop : Further output of Plot::plnr will be suppressed during this calculation.
The original example fails because the Plot function holds the first argument unevaluated until
numerical values are inserted for the variable. Since the first argument is not explicitly a list, Plot
expects the first argument to evaluate to a number. The calculation fails when the first argument
instead evaluates to a list of numbers.
This failure can be addressed in a variety of ways, one of which is to enclose the first argument of
Plot in Evaluate to force evaluation of the table.
Use Show to combine this plot with the plot from the previous exercise. For information regarding
this use of Show, enter ?Show, or see the documentation for Show.
Although not part of this exercise, it is worth noting that other functions for generating tick specifica-
tions are also available. One of the more frequently used functions is LogScale, which generates
tick specifications for logarithmic scales. Here is an example showing the use of LogScale to add a
logarithmic scale to the right side of the frame around a plot.
Plot@8x, 1 + Sqrt@xD<, 8x, 0, 4<, Frame Ø True,
FrameTicks Ø 88Automatic, Automatic<, 8Automatic, None<<D
60
50
local maximum
40
30
20
10
-2 2 4
The second surface is based on plane generated using Plot3D. The PlotPoints option is used to
specify the number of grid points to use in each direction in generating this surface. Applying the
WireFrame function to the result replaces each polygon in this surface by an outline of the polygon.
The image displayed here is drawn before the WireFrame function is applied, and so shows the
complete polygons. The wireframe image will be displayed when Show is applied to the result. The
wireframe image provides a form of transparency.
The second surface is based on plane generated using Plot3D. The PlotPoints option is Basic4.nb
used to 7
specify the number of grid points to use in each direction in generating this surface. Applying the
WireFrame function to the result replaces each polygon in this surface by an outline of the polygon.
The image displayed here is drawn before the WireFrame function is applied, and so shows the
complete polygons. The wireframe image will be displayed when Show is applied to the result. The
wireframe image provides a form of transparency.
g2 = Plot3D@1 + y, 8x, - 1, 1<, 8y, - 1, 1<, PlotPoints Ø 50D
data2 = 880.26, 0.22<, 80.49, 0.28<, 80.89, 0.41<, 81.66, 0.70<, 81.99, 0.82<,
82.36, 0.92<, 82.81, 0.99<, 83.30, 0.98<, 83.65, 0.92<, 85.27, 0.36<<;
Add a legend identifying the plot symbols in the plot from the previous exercise. See the Documenta-
tion Center for more information about how to add legends to a plot.