Reprint From The Mathematica Conference: June, 1992 Boston, MA
Reprint From The Mathematica Conference: June, 1992 Boston, MA
All rights reserved. No part of this document may be reproduced, stored in a retrieval system, or transmitted, in any form or by
any means, electronic, mechanical, photocopying, recording or otherwise, without the prior written permission of the copyright holder.
873.21.10.1992
Chapter 2 Basic Graphics
Plot ParametricPlot
Plot3D ContourPlot
DensityPlot ParametricPlot3D
Play
0.5
1 2 3 4 5 6
-0.5
-1
Sometimes we know how x; y depends In[2]:= ParametricPlot[ {Cos[t]^3, Cos[t]^2 Sin[t]}, {t,0,2Pi}]
on some parameter. For this use
ParametricPlot .
0.4
0.2
-1 -0.5 0.5 1
-0.2
-0.4
This is a three-dimensional surface where In[3]:= Plot3D[ Sin[x y], {x,0,2Pi}, {y,0,Pi}]
the height is given by Sin[x y].
1
0.5
3
0
-0.5
-1 2
0
2 1
4
6 0
2.1 Graphics Commands 3
Other typical graphical representations such as contour plots and density plots can be generated.
The option PlotPoints has been In[4]:= ContourPlot[ Exp[ -Sqrt[ x^2 + y^2]] Cos[ ArcTan[x,y]],
increased to give the plot a better {x,-1,1}, {y,-1,1}, PlotPoints -> 30]
appearance. Options are described in the
next section.
1
0.5
-0.5
-1
-1 -0.5 0 0.5 1
A density plot shows the value of the In[5]:= DensityPlot[ Tan[x y], {x,-Pi, Pi}, {y,-Pi, Pi},
function at a regular array of points. PlotPoints -> 50]
Lighter regions are higher.
-1
-2
-3
-3 -2 -1 0 1 2 3
1-1
-0.50
0.5
0.5 1
0
-0.5
-1
4
-2
-4
This will produce a 400-Hz sound. In[7]:= Play[ Sin[ 2Pi 400 t], {t,0,2}]
Out[7]=-Sound-
4 2. Basic Graphics
Mathematica treats sounds as an extension to graphics and allows primitives which render sounds to be added to graphics objects.
Plot ListPlot
Plot3D ListPlot3D
ContourPlot ListContourPlot
DensityPlot ListDensityPlot
Play ListPlay
Mathematica commands which plot functions and the corresponding commands to plot lists of data.
However before we look at plotting data we should see how to import data into Mathematica. These data may have been written by
another application such as a spreadsheet or by some custom program which you have written. If the data are written in an ASCII
form, that is, if they can be looked at with a text editor, they can easily be read into Mathematica. For the examples given here to work
the data files which are loaded must exist. To prevent this being a problem I have included the Mathematica commands which will cre-
ate the files used in these examples.
This will write the file so we can use it. In[8]:= (Do[ WriteString[ "file1.dat",
i, " ",
i^3,"\n"], {i,8}];
Close["file1.dat"])
This reads the numbers in the file and In[9]:= mydata = ReadList[ "file1.dat", {Number, Number}]
builds a list. The list is set to be the value
of the symbol mydata. Out[9]= {{1, 1}, {2, 8}, {3, 27}, {4, 64}, {5, 125}, {6, 216},
{7, 343}, {8, 512}}
400
300
200
100
2 3 4 5 6 7 8
To read in a set of data and carry out a command such as ListPlot3D , ListContourPlot or ListDensityPlot is a little more
complex. The data points must be regularly spaced over a grid. In addition you must specify the bounds of the x and y coordinates.
First we shall make the file.
This writes the file we want to use for this In[12]:= (WriteString["file2.dat", -1, " ", 1,"\n"];
example. This file is a little more complex WriteString["file2.dat", -2, " ", 2,"\n"];
than the previous example. I close the file Do[ WriteString[ "file2.dat",
for neatness.
3i," ",2i," ",i," ",
2i," ",3i,"\n"], {i,-2,2}];
Close["file2.dat"])
The rest of the file contains the data. In[16]:= (data1 = ReadList[stm, {Number, Number,
Then we close the file. Number, Number, Number}];
Close[stm])
Out[16]= file2.dat
This makes a contour plot. We could In[17]:= ListContourPlot[ data1, MeshRange -> {{xmin, xmax},
have used ListPlot3D or {ymin, ymax}}]
ListDensityPlot here. Note that the
axes use the values for the mesh.
2
-1
-2
-1 -0.5 0 0.5 1
6 2. Basic Graphics
GraphicsArray
A last type of data plotting can be done with the command GraphicsArray . Here the data which are plotted are graphics objects.
1 1 1
0.5 0.5 0.5
-0.5 1 2 3 4 5 6 -0.5 1 2 3 4 5 6 -0.5 1 2 3 4 5 6
-1 -1 -1
1 1 1
0.5 0.5 0.5
-0.5 1 2 3 4 5 6 -0.5 1 2 3 4 5 6 -0.5 1 2 3 4 5 6
-1 -1 -1
1 1 1
0.5 0.5 0.5
-0.5 1 2 3 4 5 6 -0.5 1 2 3 4 5 6 -0.5 1 2 3 4 5 6
-1 -1 -1
1 1 1
0.5 0.5 0.5
-0.5 1 2 3 4 5 6 -0.5 1 2 3 4 5 6 -0.5 1 2 3 4 5 6
-1 -1 -1
Show[ image1, image2] combine image1 and image2 and display the result
Redisplaying the image is simple. To alter the image we must examine the options which are available for graphics.
Here the background color of the plot is In[20]:= a = Plot[ Sin[x], {x,0,2Pi}, Background -> GrayLevel[0]]
set to be a GrayLevel of 0, which is black.
We save the plot as the symbol a to use it
1
later.
0.5
1 2 3 4 5 6
-0.5
-1
This uses Show to change the option for In[22]:= Show[ a, Background -> GrayLevel[1]]
background color to be white. The
drawing color, which is set by the option
DefaultColor , changes according to the 1
background.
0.5
1 2 3 4 5 6
-0.5
-1
This shows the default setting of the In[23]:= Options[ Plot, AspectRatio]
option AspectRatio.
1
Out[23]= {AspectRatio -> ---------------------}
GoldenRatio
8 2. Basic Graphics
To change a default setting one uses the In[24]:= SetOptions[ Plot, AspectRatio -> 1]
command SetOptions.
Out[24]= {AspectRatio -> 1, Axes -> Automatic,
AxesLabel -> None, AxesOrigin -> Automatic,
AxesStyle -> Automatic, Background -> Automatic,
ColorOutput -> Automatic, Compiled -> True,
DefaultColor -> Automatic, Epilog -> {}, Frame -> False,
FrameLabel -> None, FrameStyle -> Automatic,
FrameTicks -> Automatic, GridLines -> None, MaxBend -> 10.,
PlotDivision -> 20., PlotLabel -> None, PlotPoints -> 25,
PlotRange -> Automatic, PlotRegion -> Automatic,
PlotStyle -> Automatic, Prolog -> {}, RotateLabel -> True,
Ticks -> Automatic, DefaultFont :> $DefaultFont,
DisplayFunction :> $DisplayFunction}
Let’s change it back. The “;” at the end In[25]:= SetOptions[ Plot, AspectRatio -> GoldenRatio^(-1)];
suppresses the result from printing out.
The permissible values for a given option vary according to the actual effect of the option. Some options take numbers to control the
number of points at which a function is evaluated, some options control the appearance of axes and these take style directives and
some options take a simple Boolean True or False to decide whether to do something or not.
True do this
The value is Automatic. This means use In[26]:= Options[ Plot, Background]
an internal algorithm to choose some
good value. Out[26]= {Background -> Automatic}
The value is False. This means do not In[27]:= Options[ Plot, Frame]
draw frame axes.
Out[27]= {Frame -> False}
FullOptions
Often it is useful to use an internal algorithm to set an option, for example, setting PlotRange or AspectRatio to be Automatic .
Sometimes you want to know the value which was actually used. This functionality is provided by FullOptions .
Make a plot and use Automatic for the In[28]:= Plot[ Sin[x], {x,0,2Pi}, AspectRatio -> Automatic]
AspectRatio .
0.5
1 2 3 4 5 6
-0.5
-1
2.2 Altering Pictures 9
This is the default setting, it draws x and In[30]:= Options[ Plot, Axes]
y axes.
Out[30]= {Axes -> Automatic}
This draws only the horizontal axis. In[31]:= Plot[ Sin[x], {x,0,2Pi}, Axes -> {True, False}]
1 2 3 4 5 6
The position of the axes can be controlled In[32]:= Plot[ Sin[x], {x,0,2Pi}, AxesOrigin -> {Pi,-1}]
with the option AxesOrigin.
1
0.5
-0.5
0 1 2 3 4 5 6
Sin[x]
1
0.5
x
1 2 3 4 5 6
-0.5
-1
10 2. Basic Graphics
The option Frame produces different axes In[34]:= Plot[ Sin[x]^2, {x,0,2Pi}, Frame -> True]
which frame the graphic.
1
0.8
0.6
0.4
0.2
0
0 1 2 3 4 5 6
Upper x
1
0.8
Right y
Left y
0.6
0.4
0.2
0
0 1 2 3 4 5 6
Lower x
PlotLabel will give a label to the whole In[36]:= ParametricPlot[ {Sqrt[t] Sin[t], Sqrt[t] Cos[t]},
graphic. {t,0,4Pi},
PlotLabel -> "This is a spiral"]
This is a spiral
-3 -2 -1 1 2
-1
-2
-3
PlotRange
The default value of PlotRange is used In[38]:= Plot[ Sin[x]/x, {x,-14, 14}]
to omit points which are scattered away
from the group of the majority of the
0.6
points.
0.4
0.2
-10 -5 5 10
-0.2
2.2 Altering Pictures 11
To see all the points specified in the In[39]:= Show[%, PlotRange -> All]
primitives use PlotRange -> All.
1
0.8
0.6
0.4
0.2
-10 -5 5 10
-0.2
One can give explicit values to the In[41]:= Plot[ Tan[x], {x,0,2Pi}, PlotRange -> {0,10}]
PlotRange command.
10
0 1 2 3 4 5 6
AspectRatio
AspectRatio sets the shape of the output In[42]:= Plot[ Sin[x]^2, {x,0,Pi}]
image. The default value uses the
GoldenMean.
1
0.8
0.6
0.4
0.2
0.8
0.6
0.4
0.2
This uses the real ratio of x and y. In[44]:= Show[%, AspectRatio -> Automatic]
0.8
0.6
0.4
0.2
PlotStyle
0.5
1 2 3 4 5 6
-0.5
-1
This gives a z axis but no x or y axes. In[46]:= Plot3D[ Sin[x y], {x,0,2Pi}, {y,0,Pi}, Axes ->
{False,False,True}]
1
0.5
0
-0.5
-1
2.2 Altering Pictures 13
This still draws the box around the In[47]:= Show[ %, Axes -> False]
image.
This just draws the image. In[48]:= Show[ %, Boxed -> False]
This shows a more end-on view of this In[50]:= Show[ %, ViewPoint -> {1.3, -2.4, .3}]
surface.
Often when viewing a three-dimensional surface it is necessary to experiment with a number of ways to view it to find the one which
is most pleasing. Many of the Mathematica front ends have specialized tools to allow this to be done.
The Mathematica packages are well described in the Wolfram Research technical report the Guide to Standard Mathematica Packages which
is distributed with Mathematica and is also available from Wolfram Research. This describes each of the packages and gives examples
of its functions. In this section I shall give a brief look at a few of them. One important thing to remember about these packages is that
for the functions to be available to Mathematica the package must be loaded.
By using this notation, with a back quote `, Mathematica ensures that the same command will load the package on every computer
system on which Mathematica runs, even when the file naming conventions for those systems are different.
This plots the two data sets. In[56]:= MultipleListPlot[ data1, data2]
0.5
5 10 15 20
-0.5
-1
This joins the points of each set of data In[57]:= MultipleListPlot[ data1, data2, PlotJoined -> True]
and helps to distinguish them.
1
0.5
5 10 15 20
-0.5
-1
In this package facilities are given to change the plotting symbols and line styles which are used. The Wolfram Research Guide to Stan-
dard Mathematica Packages describes this and all the other Mathematica packages in detail.
2.3.2 PieCharts
These are defined in the package Graphics`Graphics` .
In[59]:= ?PieChart
PieChart[{y1, y2, ...}] generates a pie chart of the values yi.
The values yi need to be positive. Several options
(PieLabels, PieStyle, PieLineStyle, PieExploded) are
available to modify the style of the pie.
This is data I want to plot. If this were in In[59]:= data = {{30, "CDs"}, {30, "Bonds"}, {40, "Stocks"}}
a file I could read it with a command like
ReadList["file", {Number, String}].
Out[59]= {{30, CDs}, {30, Bonds}, {40, Stocks}}
CDs
Bonds
Stocks
16 2. Basic Graphics
One can use the options to alter the In[61]:= PieChart[ data, PieExploded -> {2, .2}]
appearance of the result.
CDs
Bonds
Stocks
2.3.3 BarChart
BarChart is defined in the package Graphics`Graphics` . If this package is already loaded there is no need to load it again.
The Frame option is useful in BarChart In[62]:= BarChart[ {1, -3, 4, 5, 2, 3}, {3, 6, 4, 3}, Frame ->
to keep the bars from obstructing the True]
labeling. If Graphics`Graphics` has not
been loaded you will have to load it first.
6
-2
1 2 3 4 5 6
-4 -2 0 2 4 6 8 10
2.3.4 LogPlot
Sometimes it is useful to make a plot with a scaling other than linear. For example if some value is growing or falling very rapidly
one may wish to plot the logarithm of the value. One could of course do Plot[ Log[ fun[x]], {x,0,t}] . This is unsatisfac-
tory since it does not actually plot fun[x] and the axes will look strange. LogPlot[ fun[x], {x,0,t}] would be much better.
Support for many logarithmically scaled plots is defined in the package Graphics`Graphics` .
0.1
0.01
0.001
0.0001
0 20 40 60 80 100
2.3.5 FilledPlot
A good picture will maximize the amount of ink used to display information. If one is plotting two functions simultaneously one may be
interested in the absolute values of the functions but also in the difference between them. One effective way to do this is shade in the area
between the two plots. This is done with the command FilledPlot which is defined in the package Graphics`FilledPlot` .
In[66]:= <<Graphics`FilledPlot`
Giving one function fills between the In[67]:= FilledPlot[ Sin[x], {x,0,2Pi}]
curve and the axis.
0.5
1 2 3 4 5 6
-0.5
-1
With two functions the area between them In[68]:= FilledPlot[ {x^2,x^3}, {x,1,5}]
is filled and we can see the relationship
between the two functions more clearly.
120
100
80
60
40
20
2 3 4 5
2.3.6 PlotLegend
The various options give a number of methods for labeling a plot. However you may wish to give a more sophisticated label or leg-
end to a graphic. This is provided with the function ShowLegend which is defined in the package Graphics`Legend` .
In[69]:= <<Graphics`Legend`
18 2. Basic Graphics
-1
-2
-2 -1 0 1 2
Now we can redisplay it adding the In[71]:= ShowLegend[ cont, {GrayLevel, 10, "-4", " 4",
legend. LegendPosition -> {1.1,0}}]
2
-4
1
0 4
-1
-2
-2 -1 0 1 2
2.4 Animation
Animation of a sequence of graphical images gives an extra dimension to the data which is represented. Since this extra dimension is
easily associated with time one way to use an animation is to demonstrate and investigate dynamical effects.
In Mathematica there is a number of ways to carry out animations. The simplest is provided by the Notebook front end to Mathematica.
One generates a sequence of graphics, groups them and issues the appropriate command to start the animation. The other way, also
available on front ends which do not support Notebooks, is to use the package Graphics`Animation` . This package defines use-
ful commands which work on Notebook and non-Notebook front ends. Here I demonstrate using the animation package since it is
more general.
Animate[plot, {t, tmin, tmax}] execute the graphics command plot for a sequence of values of t, and animate the
resulting sequence of frames
To demonstrate the effects of an animation in notes like these is quite difficult. One way to do this is to use the package
FlipBookAnimation` which produces an array of images.
In[73]:= <<Graphics`Animation`
2.4 Animation 19
In[74]:= Animate[
Plot[ Sin[ x n], {x,0,2Pi}],
{n, 1,6,1}]
1 1 1
0.5 0.5 0.5
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
-0.5 -0.5 -0.5
-1 -1 -1
1 1 1
0.5 0.5 0.5
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
-0.5 -0.5 -0.5
-1 -1 -1
To make full use of animation commands one needs more understanding of programming ideas and of the use of graphics options
than have been covered so far. For this reason we leave the subject and cover it in more detail in a later chapter.