Graphics and Sound

Basic Plotting
Plot[f,{x,xmin,xmax}]
plot f as a function of x from xmin to xmax
Plot[{f1,f2,},{x,xmin,xmax}]
plot several functions together
Basic plotting functions.
This plots a graph of as a function of from 0 to :
You can plot functions that have singularities. The Wolfram Language will try to choose appropriate scales:
The singularities can be omitted from the plot by using the Exclusions option:
You can give a list of functions to plot. A different color will automatically be used for each function:
This finds the numerical solution to a differential equation, as discussed in "Numerical Differential Equations":
Here is a plot of the solution:
Options for Graphics
When the Wolfram Language plots a graph for you, it has to make many choices. It has to work out what the scales should be, where the function should be sampled, how the axes should be drawn, and so on. Most of the time, the Wolfram Language will probably make pretty good choices. However, if you want to get the very best possible pictures for your particular purposes, you may have to help the Wolfram Language in making some of its choices.
There is a general mechanism for specifying "options" in Wolfram Language functions. Each option has a definite name. As the last arguments to a function like Plot, you can include a sequence of rules of the form name->value, to specify the values for various options. Any option for which you do not give an explicit rule is taken to have its "default" value.
Plot[f,{x,xmin,xmax},option->value]
make a plot, specifying a particular value for an option
Choosing an option for a plot.
A function like Plot has many options that you can set. Usually you will need to use at most a few of them at a time. If you want to optimize a particular plot, you will probably do best to experiment, trying a sequence of different settings for various options.
Each time you produce a plot, you can specify options for it. "Redrawing and Combining Plots" will also discuss how you can change some of the options, even after you have produced the plot.
option name
default value
AspectRatio1/GoldenRatio
the heighttowidth ratio for the plot; Automatic sets it from the absolute and coordinates
AxesTrue
whether to include axes
AxesLabelNone
labels to be put on the axes; ylabel specifies a label for the axis, {xlabel,ylabel} for both axes
AxesOriginAutomatic
the point at which axes cross
BaseStyle{}
the default style to use for the plot
FormatTypeTraditionalForm
the default format type to use for text in the plot
FrameFalse
whether to draw a frame around the plot
FrameLabelNone
labels to be put around the frame; give a list in clockwise order starting with the lower axis
FrameTicksAutomatic
what tick marks to draw if there is a frame; None gives no tick marks
GridLinesNone
what grid lines to include; Automatic includes a grid line for every major tick mark
PlotLabelNone
an expression to be printed as a label for the plot
PlotRangeAutomatic
the range of coordinates to include in the plot; All includes all points
TicksAutomatic
what tick marks to draw if there are axes; None gives no tick marks
Some of the options for Plot. These can also be used in Show.
Here is a plot with all options having their default values:
This draws axes on a frame around the plot:
This specifies labels for the and axes. The expressions you give as labels are printed just as they would be if they appeared as TraditionalForm Wolfram Language output. You can give any piece of text by putting it inside a pair of double quotes:
You can give several options at the same time, in any order:
Setting the AspectRatio option changes the whole shape of your plot. AspectRatio gives the ratio of height to width. Its default value is the inverse of the Golden Ratiosupposedly the most pleasing shape for a rectangle:
Automatic
use internal algorithms
None
do not include this
All
include everything
True
do this
False
do not do this
Some common settings for various options.
When the Wolfram Language makes a plot, it tries to set the and scales to include only the "interesting" parts of the plot. If your function increases very rapidly, or has singularities, the parts where it gets too large will be cut off. By specifying the option PlotRange, you can control exactly what ranges of and coordinates are included in your plot.
Automatic
show at least a large fraction of the points, including the "interesting" region (the default setting)
All
show all points
{ymin,ymax}
show a specific range of values
{xrange,yrange}
show the specified ranges of and values
Settings for the option PlotRange.
The setting for the option PlotRange gives explicit limits for the graph. With the limits specified here, the bottom of the curve is cut off:
The Wolfram Language always tries to plot functions as smooth curves. As a result, in places where your function wiggles a lot, the Wolfram Language will use more points. In general, the Wolfram Language tries to adapt its sampling of your function to the form of the function. There is a limit, however, which you can set, to how finely the Wolfram Language will ever sample a function.
The function wiggles infinitely often when . the Wolfram Language tries to sample more points in the region where the function wiggles a lot, but it can never sample the infinite number that you would need to reproduce the function exactly. As a result, there are slight glitches in the plot:
It is important to realize that since the Wolfram Language can only sample your function at a limited number of points, it can always miss features of the function. The Wolfram Language adaptively samples the functions, increasing the number of samples near interesting features, but it is still possible to miss something. By increasing PlotPoints, you can make the Wolfram Language sample your function at a larger number of points. Of course, the larger you set PlotPoints to be, the longer it will take the Wolfram Language to plot any function, even a smooth one.
option name
default value
PlotStyleAutomatic
a list of lists of graphics primitives to use for each curve (see "Graphics Directives and Options" )
ClippingStyleNone
what to draw when curves are clipped
FillingNone
filling to insert under each curve
FillingStyleAutomatic
style to use for filling
PlotPoints50
the initial number of points at which to sample the function
MaxRecursionAutomatic
the maximum number of recursive subdivisions allowed
More options for Plot. These cannot be used in Show.
This uses PlotStyle to specify a dashed curve:
When plotting multiple functions, PlotStyle settings in a list are used sequentially for each function:
When a PlotStyle contains a sublist, the settings are combined:
By default nothing is indicated when the PlotRange is set, so that it cuts off curves:
Setting ClippingStyle to Automatic draws a dashed line where a curve is cut off:
Setting ClippingStyle to a list defines the style for the parts cut off at the bottom and top:
This specifies filling between the curve and the axis:
The filling can be specified to extend to an arbitrary height, such as the bottom of the graphic. Filling colors are automatically blended where they overlap:
This specifies a specific filling to be used only for the first curve:
This shows a filling from the first curve to the second using a nondefault filling style:
Redrawing and Combining Plots
The Wolfram Language saves information about every plot you produce, so that you can later redraw it. When you redraw plots, you can change some of the options you use.
Show[plot,option->value]
redraw a plot with options changed
Show[plot1,plot2,]
combine several plots
GraphicsGrid[{{plot1,plot2,},}]
draw an array of plots
InputForm[plot]
show the underlying textual description of the plot
Functions for manipulating plots.
Here is a simple plot:
When you redraw the plot, you can change some of the options. This changes the choice of scale:
This takes the plot from the previous line, and changes another option in it:
By using Show with a sequence of different options, you can look at the same plot in many different ways. You may want to do this, for example, if you are trying to find the best possible setting of options.
You can also use Show to combine plots. All of the options for the resulting graphic will be based upon the options of the first graphic in the Show expression.
This sets gj0 to be a plot of from to :
Here is a plot of from to :
Plot specifies an explicit PlotRange for each graphic:
This uses PlotRange to override the explicit value set for gj0:
All Wolfram Language graphics are expressions and can be manipulated in the same way as any other expression. Doing these kinds of manipulations does not require the use of Show.
This replaces all instances of the symbol Line with the symbol Point in the graphics expression represented by gj0:
Using Show[plot1,plot2,] you can combine several plots into one. GraphicsGrid allows you to draw several plots in an array.
GraphicsGrid[{{plot11,plot12,},}]
draw a rectangular array of plots
GraphicsRow[{plot1,plot2,}]
draw several plots side by side
GraphicsColumn[{plot1,plot2,}]
draw a column of plots
GraphicsGrid[plots,Spacings->{h,v}]
put the specified horizontal and vertical spacing between the plots
Drawing arrays of plots.
This shows the previous plots in an array:
If you redisplay an array of plots using Show, any options you specify will be used for the whole array, rather than for individual plots:
GraphicsGrid by default puts a narrow border around each of the plots in the array it gives. You can change the size of this border by setting the option Spacings->{h,v}. The parameters h and v give the horizontal and vertical spacings to be used. The Spacings option uses the width and height of characters in the default font to scale the h and v parameters by default, but it is generally more useful in graphics to use Scaled coordinates. Scaled scales widths and heights so that a value of 1 represents the width and height of one element of the grid.
This increases the horizontal spacing, but decreases the vertical spacing between the plots in the array:
When you make a plot, the Wolfram Language saves the list of points it used, together with some other information. Using what is saved, you can redraw plots in many different ways with Show. However, you should realize that no matter what options you specify, Show still has the same basic set of points to work with. So, for example, if you set the options so that the Wolfram Language displays a small portion of your original plot magnified, you will probably be able to see the individual sample points that Plot used. Options like PlotPoints can only be set in the original Plot command itself. (the Wolfram Language always plots the actual points it has; it avoids using smoothed or splined curves, which can give misleading results in mathematical graphics.)
Here is a simple plot:
This shows a small region of the plot in a magnified form. At this resolution, you can see the individual line segments that were produced by the original Plot command:
ThreeDimensional Surface Plots
Plot3D[f,{x,xmin,xmax},{y,ymin,ymax}]
make a threedimensional plot of f as a function of the variables x and y
Basic 3D plotting function.
This makes a threedimensional plot of the function :
Three-dimensional graphics can be rotated in place by dragging the mouse inside the graphic. Dragging inside the graphic causes the graphic to tumble in a direction that follows the mouse, and dragging around the borders of the graphic causes the graphic to spin in the plane of the screen. Dragging the graphic while holding down the Shift key causes the graphic to pan. Use the Ctrl key to zoom.
There are many options for threedimensional plots in the Wolfram System. Some are discussed here; others are described in "The Structure of Graphics and Sound".
The first set of options for threedimensional plots is largely analogous to those provided in the twodimensional case.
option name
default value
AxesTrue
whether to include axes
AxesLabelNone
labels to be put on the axes: zlabel specifies a label for the axis, {xlabel,ylabel,zlabel} for all axes
BaseStyle{}
the default style to use for the plot
BoxedTrue
whether to draw a three-dimensional box around the surface
FaceGridsNone
how to draw grids on faces of the bounding box; All draws a grid on every face
LabelStyle{}
style specification for labels
LightingAutomatic
simulated light sources to use
MeshAutomatic
whether an mesh should be drawn on the surface
PlotRange{Full,Full,Automatic}
the range of or other values to include
SphericalRegionFalse
whether to make the circumscribing sphere fit in the final display area
ViewAngleAll
angle of the field of view
ViewCenter{1,1,1}/2
point to display at the center
ViewPoint{1.3,-2.4,2}
the point in space from which to look at the surface
ViewVectorAutomatic
position and direction of a simulated camera
ViewVertical{0,0,1}
direction to make vertical
BoundaryStyleAutomatic
how to draw boundary lines for surfaces
ClippingStyleAutomatic
how to draw clipped parts of surfaces
ColorFunctionAutomatic
how to determine the color of the surfaces
FillingNone
filling under each surface
FillingStyleOpacity[.5]
style to use for filling
PlotPoints25
the number of points in each direction at which to sample the function; specifies different numbers in the and directions
PlotStyleAutomatic
graphics directives for the style of each surface
Some options for Plot3D. The first set can also be used in Show.
This redraws the previous plot with options changed. With this setting for PlotRange, only the part of the surface in the range is shown:
The ClippingStyle option of Plot3D can be used to fill in the clipped regions:
When you make the original plot, you can choose to sample more points. The Wolfram System adaptively samples the plot, adding points for large variations, but occasionally you may still need to specify a greater number of points:
Here is the same plot, with labels for the axes, and grids added to each face:
Probably the single most important issue in plotting a threedimensional surface is specifying where you want to look at the surface from. The ViewPoint option for Plot3D and Show allows you to specify the point in space from which you view a surface. The details of how the coordinates for this point are defined are discussed in "Coordinate Systems for Three-Dimensional Graphics". When rotating a graphic using the mouse, you are adjusting the ViewPoint value.
Here is a surface, viewed from the default view point {1.3,-2.4,2}. This view point is chosen to be "generic", so that visually confusing coincidental alignments between different parts of your object are unlikely:
This redraws the picture, with the view point directly in front. Notice the perspective effect that makes the back of the box look much smaller than the front:
The ViewPoint option also accepts various symbolic values that represent common view points:
{1.3,-2.4,2}
default view point
Front
in front, along the negative direction
Back
in back, along the positive direction
Above
above, along the positive direction
Below
below, along the negative direction
Left
left, along the negative direction
Right
right, along the positive direction
Typical choices for the ViewPoint option.
The human visual system is not particularly good at understanding complicated mathematical surfaces. As a result, you need to generate pictures that contain as many clues as possible about the form of the surface.
View points slightly above the surface usually work best. It is generally a good idea to keep the view point close enough to the surface that there is some perspective effect. Having a box explicitly drawn around the surface is helpful in recognizing the orientation of the surface.
Here is a plot with the default settings for surface rendering options:
This shows the surface without the mesh drawn. It is usually much harder to see the form of the surface if the mesh is not there:
To add an extra element of realism to threedimensional graphics, the Wolfram System by default colors threedimensional surfaces using a simulated lighting model. In the default case, the Wolfram System assumes that there are four point light sources plus ambient lighting shining on the object. "Lighting and Surface Properties" describes how you can set up other light sources, and how you can specify the reflection properties of an object.
Lighting can also be specified using a string that represents a collection of lighting properties. For example, the option setting Lighting->"Neutral" uses a set of white lights, and so can be faithfully reproduced on a black and white output device such as a printer.
The ColorFunction option by default uses Lighting->"Neutral" so that the surface colors are not distorted by colored lights:
Plotting Lists of Data
The Wolfram Language can be used to make plots of functions. You give the Wolfram Language a function, and it builds up a curve or surface by evaluating the function at many different points.
Here we describe how you can make plots from lists of data, instead of functions. ("Importing and Exporting Data" discusses how to read data from external files and programs.) The Wolfram Language commands for plotting lists of data are direct analogs of the ones for plotting functions.
ListPlot[{y1,y2,}]
plot y1,y2, at x values 1,2,
ListPlot[{{x1,y1},{x2,y2},}]
plot points (x1,y1),
ListLinePlot[list]
join the points with lines
ListPlot3D[{{z11,z12,},{z21,z22,},}]
make a threedimensional plot of the array of heights zyx
ListPlot3D[{{x1,y1,z1},{x2,y2,z2},}]
make a three-dimensional plot with heights zi at positions {xi,yi}
ListContourPlot[array]
make a contour plot
ListDensityPlot[array]
make a density plot
Functions for plotting lists of data.
Here is a list of values:
This plots the values:
This joins the points with lines:
When plotting multiple datasets, the Wolfram Language chooses a different color for each dataset automatically:
This gives a list of pairs:
This plots the points:
This gives a rectangular array of values. The array is quite large, so we end the input with a semicolon to stop the result from being printed out:
This makes a threedimensional plot of the array of values:
This gives a density plot of the array of values:
Parametric Plots
"Basic Plotting" describes how to plot curves in the Wolfram Language in which you give the coordinate of each point as a function of the coordinate. You can also use the Wolfram Language to make parametric plots. In a parametric plot, you give both the and coordinates of each point as a function of a third parameter, say .
ParametricPlot[{fx,fy},{t,tmin,tmax}]
make a parametric plot
ParametricPlot[{{fx,fy},{gx,gy},},{t,tmin,tmax}]
plot several parametric curves together
Functions for generating parametric plots.
Here is the curve made by taking the coordinate of each point to be Sin[t] and the coordinate to be Sin[2t]:
ParametricPlot3D[{fx,fy,fz},{t,tmin,tmax}]
make a parametric plot of a threedimensional curve
ParametricPlot3D[{fx,fy,fz},{t,tmin,tmax},{u,umin,umax}]
make a parametric plot of a threedimensional surface
ParametricPlot3D[{{fx,fy,fz},{gx,gy,gz},},]
plot several objects together
Threedimensional parametric plots.
ParametricPlot3D[{fx,fy,fz},{t,tmin,tmax}] is the direct analog in three dimensions of ParametricPlot[{fx,fy},{t,tmin,tmax}] in two dimensions. In both cases, the Wolfram Language effectively generates a sequence of points by varying the parameter t, then forms a curve by joining these points. With ParametricPlot, the curve is in two dimensions; with ParametricPlot3D, it is in three dimensions.
This makes a parametric plot of a helical curve. Varying t produces circular motion in the - plane, and linear motion in the direction:
ParametricPlot3D[{fx,fy,fz},{t,tmin,tmax},{u,umin,umax}] creates a surface, rather than a curve. The surface is formed from a collection of quadrilaterals. The corners of the quadrilaterals have coordinates corresponding to the values of the fi when t and u take on values in a regular grid.
Here the and coordinates for the quadrilaterals are given simply by t and u. The result is a surface plot of the kind that can be produced by Plot3D:
This shows the same surface as before, but with the coordinates distorted by a quadratic transformation:
This produces a helicoid surface by taking the helical curve shown above, and at each section of the curve drawing a quadrilateral:
In general, it is possible to construct many complicated surfaces using ParametricPlot3D. In each case, you can think of the surfaces as being formed by "distorting" or "rolling up" the - coordinate grid in a certain way.
This produces a cylinder. Varying the t parameter yields a circle in the - plane, while varying u moves the circles in the direction:
This produces a torus. Varying u yields a circle, while varying t rotates the circle around the axis to form the torus:
This produces a sphere:
You should realize that when you draw surfaces with ParametricPlot3D, the exact choice of parametrization is often crucial. You should be careful, for example, to avoid parametrizations in which all or part of your surface is covered more than once. Such multiple coverings often lead to discontinuities in the mesh drawn on the surface, and may make ParametricPlot3D take much longer to render the surface.
Some Special Plots
As discussed in "The Structure of Graphics and Sound", the Wolfram Language includes a full graphics programming language. In this language, you can set up many different kinds of plots.
LogPlot[f,{x,xmin,xmax}]
generate a linear-log plot
LogLinearPlot[f,{x,xmin,xmax}]
generate a log-linear plot
LogLogPlot[f,{x,xmin,xmax}]
generate a log-log plot
ListLogPlot[list]
generate a linear-log plot from a list of data
ListLogLinearPlot[list]
generate a log-linear plot from a list of data
ListLogLogPlot[list]
generate a log-log plot from a list of data
DateListPlot[list]
generate a plot from a list of data with date coordinates
DateListLogPlot[list]
generate a linear-log plot from a list of data with date coordinates
PolarPlot[r,{t,tmin,tmax}]
generate a polar plot of the radius r as a function of angle t
SphericalPlot3D[r,{theta,min,max},{phi,min,max}]
generate a three-dimensional spherical plot
RevolutionPlot3D[f,{t,tmin,tmax}]
generate a three-dimensional surface of revolution
BarChart[list]
plot a list of data as a bar chart
PieChart[list]
plot a list of data as a pie chart
Some special plotting functions.
Here is an exponential function:
This generates linear-log and log-linear plots of the function:
This gathers some financial time series data:
This plots the financial data on linear and logarithmic scales:
This uses RevolutionPlot3D to revolve a curve around the and axes:
Here is a list of the first 10 primes:
Here is a bar chart of the primes:
This gives a pie chart:
Some of the plotting functions available are defined in the Standard Extra Packages included with the Wolfram System.
ErrorListPlot[{{x1,y1,dy1},}]
generate a plot with error bars
BoxWhiskerPlot[list]
generate a box-and-whisker plot of data
ParetoPlot[list]
generate a Pareto plot of data
PairwiseScatterPlot[data]
plot pairs of coordinates for multivariate data
Some of the plotting functions in the Wolfram System standard extra packages.
This plots a dataset with its associated errors:
This generates a box-and-whisker plot for samples from a normal distribution:
Here is a random dataset consisting of the letters a through f:
This creates a Pareto plot of the dataset:
PairwiseScatterPlot can be used to plot pairs of columns from trivariate data:
Sound

Sound Playback

On most computer systems, the Wolfram System can produce not only graphics but also sound. The Wolfram System treats graphics and sound in a closely analogous way.
For example, just as you can use Plot[f,{x,xmin,xmax}] to plot a function, so also you can use Play[f,{t,0,tmax}] to play a function. Play takes the function to define the waveform for a sound: the values of the function give the amplitude of the sound as a function of time.
Play[f,{t,0,tmax}]
play a sound with amplitude f as a function of time t in seconds
Playing a function.
On a suitable computer system, this plays a pure tone with a frequency of 440 hertz for one second:
Sounds produced by Play can have any waveform. They do not, for example, have to consist of a collection of harmonic pieces. In general, the amplitude function you give to Play specifies the instantaneous signal associated with the sound. This signal is typically converted to a voltage, and ultimately to a displacement. Note that amplitude is sometimes defined to be the peak signal associated with a sound; in the Wolfram System, it is always the instantaneous signal as a function of time.
This plays a more complex sound:
Play is set up so that the time variable that appears in it is always measured in absolute seconds. When a sound is actually played, its amplitude is sampled a certain number of times every second. You can specify the sample rate by setting the option SampleRate.
Play[f,{t,0,tmax},SampleRate->r]
play a sound, sampling it r times a second
Specifying the sample rate for a sound.
In general, the higher the sample rate, the better highfrequency components in the sound will be rendered. A sample rate of r typically allows frequencies up to hertz. The human auditory system can typically perceive sounds in the frequency range 20 to 22000 hertz (depending somewhat on age and sex). The fundamental frequencies for the 88 notes on a piano range from 27.5 to 4186 hertz.
The standard sample rate used for compact disc players is 44100. The effective sample rate in a typical telephone system is around 8000. On most computer systems, the default sample rate used by the Wolfram System is around 8000.
You can use Play[{f1,f2,] to produce stereo sound. In general, the Wolfram System supports any number of sound channels.
ListPlay[{a1,a2,},SampleRate->r]
play a sound with a sequence of amplitude levels
Playing sampled sounds.
The function ListPlay allows you simply to give a list of values which are taken to be sound amplitudes sampled at a certain rate.
Play noise generated from random values:
When sounds are actually rendered by the Wolfram System, only a certain range of amplitudes is allowed. The option PlayRange in Play and ListPlay specifies how the amplitudes you give should be scaled to fit in the allowed range. The settings for this option are analogous to those for the PlotRange graphics option discussed in "Options for Graphics".
PlayRange->Automatic
use an internal procedure to scale amplitudes
PlayRange->All
scale so that all amplitudes fit in the allowed range
PlayRange->{amin,amax}
make amplitudes between amin and amax fit in the allowed range, and clip others
Specifying the scaling of sound amplitudes.
While it is often convenient to use the setting PlayRange->Automatic, you should realize that Play may run significantly faster if you give an explicit PlayRange specification, so it does not have to derive one.
EmitSound[snd]
emit a sound when evaluated
Playing sounds programmatically.
A Sound object in output is typically formatted as a button which contains a visualization of the sound and which plays the sound when pressed. Sounds can be played without the need for user intervention or producing output by using EmitSound. In fact, the internal implementation of Sound buttons uses EmitSound when the button is pressed.
The internal structure of Sound objects is discussed in "The Representation of Sound".