You are on page 1of 8

Basic Tutorial (part 4 of 8)

4 Plotting and Graphics


In this fourth part of the basic tutorial you will learn about:

Ë Making plots of functions


Ë Making plots of data
Ë Axes and labels
Ë Working with graphical elements

Making plots of functions


A few simple examples can be used to illustrate the basic characteristics of functions such as Plot
and Plot3D that are available in Mathematica for plotting numerical functions.
Here is an example showing the use of the Plot function to plot Sin@xD for x ranging from 0 to 6 Pi.
Plot@Sin@xD, 8x, 0, 6 Pi<D

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.

Making plots of data


2 Basic4.nb

Making plots of data


For every function that is available for plotting numerical functions, there is an analogous function
for plotting data. Here is an example showing the use of the ListPlot function to plot a list of points.
The Joined option is included to cause lines to be drawn between the points in the data.
data = 883, 1.5<, 817, 0.3<, 831, 0.5<, 845, 0.1<,
860, 2.7<, 870, 3.2<, 875, 3.3<, 885, 3.3<, 8100, 4.0<<;

ListPlot@data, Joined Ø TrueD

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;

ListContourPlot@data, DataRange Ø 880, 60<, 80, 80<<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

Axes and labels


The variety of methods for adding labels to plots is of course endless. Many of the more common
ways of adding labels to plots can be handled through options of plotting functions.
Here is an example showing the default behavior of Plot in plotting a pair of functions.
Plot@8Sin@xD, Cos@xD<, 8x, 0, 3 Pi<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

Plot@8Sin@xD, Cos@xD<, 8x, 0, 3 Pi<, PlotLabel Ø "Cos@xD and Sin@xD",


Frame Ø True, FrameLabel Ø 8x, y<, PlotStyle Ø 8Dashing@80.02<D, 8<<,
FrameTicks Ø 8880, 0<, 8Pi, Pi<, 82 Pi, 2 Pi<, 83 Pi, 3 Pi<<, Automatic, None, None<D
The PlotLabel option is used to add a title across the top of the plot.
The setting Frame->True for the Frame option adds a frame to the plot, rather than axes.
The FrameLabel option adds labels to the frame. The setting FrameLabel->{x,y} adds x as the
label for the bottom of the frame and y as the label for the left side of the frame.
The PlotStyle option specifies the style of the lines in the plot. The value {Dashing[{0.02}],{}}
for the PlotStyle option specifies that the first line should be drawn dashed and the second line
should be drawn as a solid line (the default style). If the plot contains many lines, different argu-
ments in the Dashing directive, or other style specifications, such as color and thickness can be
used to distinguish the lines. Graphics directives are discussed in the next section of this tutorial.
The FrameTicks option specifies how tick marks should be arranged and labeled around the frame.
In this example the value of the FrameTicks option is a list with four elements. The first element is a
list that specifies the positions and labels for individual tick marks along the bottom of the frame.
The second element is the symbol Automatic, which indicates that default tick marks and labels
should be used on the left side of the frame. The last two elements are None, which indicates that
no tick marks should be shown along the right side of the frame or along the top of the frame.
You can see all of the options of the Plot function by evaluating Options[Plot]. Many of these
options are used for controlling axes and labels. You can get more information about the use of
these options by looking them up in the Help Browser.

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.

Exercise 4.2 Plotting data


Here is an list of pairs of numbers representing data collecting in an experiment. Use ListPlot to plot
these data as a set of points. Assign the result as the value of a variable dataplot for use in the
next exercise.
881.030, 0.021<, 81.087, 0.026<, 81.176, 0.036<, 81.409, 0.080<, 81.470, 0.096<,
81.549, 0.122<, 81.586, 0.135<, 81.684, 0.177<, 81.926, 0.315<, 81.997, 0.366<,
82.11`, 0.453<, 82.171, 0.503<, 82.183, 0.513<, 82.209, 0.535<, 82.266, 0.584<,
82.366, 0.669<, 82.457, 0.745<, 82.467, 0.753<, 82.547, 0.814<, 82.774, 0.950<,
82.852, 0.978<, 82.896, 0.989<, 83.043, 0.998<, 83.064, 0.996<, 83.223, 0.954<,
83.467, 0.862<, 83.711, 1.037<, 83.718, 1.049<, 83.785, 1.170<, 83.837, 1.264<,
84.006, 1.363<, 84.093, 1.221<, 84.131, 1.119<, 84.227, 0.820<, 84.255, 0.728<,
84.257, 0.724<, 84.402, 0.339<, 84.912, 0.026<, 85.067, 0.014<, 85.104, 0.012<,
85.168, 0.009<, 85.287, 0.005<, 85.453, 0.002<, 85.455, 0.002<, 85.458, 0.002<,
85.597, 0.001<, 85.695, 0.001<, 85.717, 0.001<, 85.723, 0.001<, 85.874, 0.000<<
Basic4.nb 5

Exercise 4.3 Combining plots


Here is a plot representing a theoretical fit to the data in the previous exercise.
fitplot = Plot@1.3 Exp@- Hx - 3.4L ^ 2D, 8x, 0, 6<D

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.

Exercise 4.4 Controlling tick marks on a plot


Recall the example in which the FrameTicks option was used to specify explicit positions and labels
for the tick marks across the bottom of the frame around a plot:
Plot@8Sin@xD, Cos@xD<, 8x, 0, 3 Pi<, PlotLabel Ø "Cos@xD and Sin@xD",
Frame Ø True, FrameLabel Ø 8x, y<, PlotStyle Ø 8Dashing@80.02<D, 8<<,
FrameTicks Ø 8880, 0<, 8Pi, Pi<, 82 Pi, 2 Pi<, 83 Pi, 3 Pi<<, Automatic, None, None<D
An explicit tick specification such as {{0,0},{Pi,Pi},{2Pi,2Pi},{3Pi,3Pi}} provides for
precise control over the tick marks on a plot, but entering such a specification can be a relatively
time-consuming and error-prone task. The tick specification in this and many similar examples can
also be generated using the Range function. Here is an example showing its use to generate tick
marks and tick labels at 0, Pi, 2 Pi, and 3 Pi.
Plot@8Sin@xD, Cos@xD<, 8x, 0, 3 Pi<, PlotLabel Ø "Cos@xD and Sin@xD",
Frame Ø True, FrameLabel Ø 8x, y<, PlotStyle Ø 8Dashing@80.02<D, 8<<,
FrameTicks Ø 8Range@0, 3 Pi, PiD, Automatic, None, None<D
Here is another plot. Add a value for the Ticks option to give tick marks and tick labels at multiples
of 60 along the horizontal axis. The result should be a plot with a tick mark and tick label at 60, 120,
180, 240, 300, 360, 420, and 480. You can do this either using Range[0,480,60], or by includ-
ing a list specifying the position and label for each tick mark in the value of the Ticks option.
Plot@Exp@- HHx - 100L ê 100L ^ 2D, 8x, 0, 500<D

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

Exercise 4.5 Labeling elements in a plot


Here is an example showing the use of the Epilog option of Plot and the Text and Arrow graphics
primitives to label a point on a curve.
Plot@8 - x ^ 3 + 4 x ^ 2 + 3 x, 8x, - 3, 5<,
Epilog Ø 8Arrow@882, 40<, 83, 26<<D, Text@"local maximum", 82, 45<D<D
Replace the Arrow primitive in this example with a corresponding Line primitive so that a line rather
than an arrow is drawn between the text and the curve. The finished plot should look like this:
6 Basic4.nb

60

50
local maximum
40

30

20

10

-2 2 4

Exercise 4.6 Controlling the axes in a plot


Set the value of the AxesOrigin option in this example so the axes cross at the point {2 Pi, -1},
and set the AxesStyle option to RGBColor[0,0,1] so the axes are drawn in blue.
Plot@Cos@xD, 8x, 0, 4 Pi<D

Exercise 4.7 Drawing a histogram


Add an option to the Histogram function in this example so that the plot shows 9 bars (categories),
rather than the default number of categories.
The Histogram function is defined in the kernel. The ChartStyle option is set in this example so that
the histogram is displayed using alternate gray and blue bars.
data = N@Table@Cos@kD, 8k, 500<DD;

Histogram@data, ChartStyle Ø 8GrayLevel@0.5D, RGBColor@0, 0, 1D<D

Exercise 4.8 Combining two surfaces


Use Show to combine the two surfaces shown here.
The first surface is a spiral constructed using ParametricPlot3D.
g1 = ParametricPlot3D@8y Cos@Pi xD, y Sin@Pi xD, x<, 8x, 0, 2<, 8y, - 1, 1<D

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

Exercise 4.9 Plotting several data sets


Here is an example showing the use of MultipleListPlot to plot two data sets on the same plot.
The MultipleListPlot function is defined in the standard Graphics`MultipleListPlot`
package.
data1 = 880.16, 0.18<, 81.20, 0.73<, 81.43, 0.85<, 81.72, 0.96<, 82.57, 0.85<,
83.20, 0.63<, 83.27, 0.65<, 83.83, 1.10<, 83.95, 1.14<, 84.21, 0.96<,
84.28, 0.86<, 84.71, 0.25<, 85.00, 0.06<, 85.31, 0.01<, 85.64, 0.01<<;

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<<;

ListPlot@8data1, data2<, PlotMarkers Ø 8 , <, PlotRange Ø AllD

Here is a third data set, assigned as the value of data3.


data3 = 880.181, 0.497<, 80.385, 0.456<, 80.842, 0.807<,
81.890, 0.955<, 82.646, 1.234<, 83.016, 1.266<, 83.419, 0.897<,
83.454, 0.969<, 84.591, 0.848<, 85.078, 0.526<, 85.599, 0.594<<;
Change the MultipleListPlot input above to include data3 and change the value of the Sym-
bolShape option so that a different plot symbol of your choice is used for the points in data3. Here
are some of the symbols that you might consider:
Point point
PlotSymbol@TriangleD small filled triangle
PlotSymbol@Triangle, 4D larger filled triangle
PlotSymbol@Diamond, 4D filled diamond shape
MakeSymbol@RegularPolygon@4, 4DD empty diamond shape
MakeSymbol@RegularPolygon@4, 4, 0, Pi ê 4DD empty square

Exercise 4.10 Adding a legend to a plot


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.
8 Basic4.nb

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.

You might also like