2-D Graphics

Index

This index gives a list of keywords that are associated with various cells in this Notebook. If you Command-Double-click one of the keywords below,  Mathematica  will find the cell associated with that keyword, select it, and display it in the center of  your window.

ContourPlot`advanced  
ContourPlot`basic  
ContourPlot`options  
DensityPlot`advanced  
DensityPlot`basic  
DensityPlot`options  
Evaluate  
Graphics  
Graphics`directives`2D 
Graphics`elements`2D  
GraphicsArray  
ListPlot`advanced  
ListPlot`basic  
ListPlot`options  
ParametricPlot`advanced  
ParametricPlot`basic  
ParametricPlot`options  
Plot`basic  
Plot`combining  
Plot`options  
Primitives  
Show  
Show`combining  
Show`graphics`primitives

Basic 2-D Graphics

Syntax (see pages 134 and 394)

Plot

Plot[f, {x, xmin, xmax }] plots f  as a function of x  from xmin  to xmax .

Plot[{f1, f2, ...}, {x, xmin, xmax }] plots several functions together.

Plot[Evaluate[y [x ] /. solution ], {x, xmin, xmax }] plots a numerical solution to a differential equation obtained from NDSolve.

ListPlot

ListPlot[{y1, y2, ...}] plots y1, y2 , ... at x  values of 1, 2, ...

ListPlot[{{x1, y1 }, {x2, y2 },...}] plots points (x1, y1), ...

ParametricPlot

ParametricPlot[{fx, fy }, {t, tmin, tmax }] produces a parametric plot with x  and y  coordinates fx  and fy  generated as a function of t .

ContourPlot

ContourPlot[f, {x, xmin, xmax }, {y, ymin, ymax }]  makes a contour plot of f  as a function of x  and y .

DensityPlot

DensityPlot[f, {x, xmin, xmax }, {y, ymin, ymax }] makes a density plot.

Plotting a Function

Plot[Sin[x], {x, 0, 2Pi}];

Plotting Several Functions

Plot[{Sin[x],Sin[2x], Sin[3x]}, {x, 0, 2Pi}];

Plotting the Solution of A Differential Equation

NDSolve[
   {y''[x] + Sin[x]^2 y'[x] + y[x] ==Cos[x]^2,
         y[0] == 1, y'[0] == 0},y, {x,0,10}];

Plot[ Evaluate[ y[x] /. %], {x, 0, 10}];

List Plot

t = Table[i^2, {i, 10}]

ListPlot[t];

List Plot of Pairs

s = Table[ {x, x^3}, {x, 0, 1, 0.1}]

ListPlot[s];

Parametric Plot

ParametricPlot[{Sin[t], Sin[2t]}, {t, 0, 2Pi}];

Contour Plot

ContourPlot[Sin[x] Sin[y], 
                   {x, -2, 2}, {y, -2, 2}];

Density Plot

DensityPlot[Sin[x] Sin[y], 
                   {x, -2, 2}, {y, -2, 2}];

Using Plot with Options

Syntax (see pages 137, 407 and 418)

Plot[f, {x, xmin, xmax }] plots f  as a function of x  from xmin  to xmax .

Plot[f, {x, xmin, xmax }, option -> value] makes a plot, specifying a particular value for an option.

Show

Show[plot ]  redraws a plot.

Show[plot, option ->value ] redraws a plot with options changed.

Options for Plot and Show

The following options can be given in Plot or Show. The default value of the option is given.

AspectRatio -> 1/GoldenRatio  gives the ratio of height to width.

Axes ->  True  determines whether to draw axes.

AxesLabel -> None  gives axes labels.

AxesOrigin  ->  Automatic  determines where axes should cross.

AxesStyle  ->  Automatic  gives graphics directives to specify the style for axes.

Background  ->  Automatic  gives the background color for the plot.

DefaultColor  ->  Automatic  gives the default color for plot elements.

Frame  ->  False  determines whether to put a frame around the plot.

FrameLabel  ->  None  gives frame labels.

FrameStyle  ->  Automatic  gives graphics directives for the style for the frame.

FrameTicks ->  Automatic  gives frame tick marks.

GridLines  ->  None  determines grid lines to draw.

PlotLabel ->  None  gives a label for the plot.

PlotRange ->  Automatic  gives range of values to include.

PlotRegion  ->  Automatic  gives the final display region to be filled.

RotateLabel  ->  True  determines whether to rotate y  labels on the frame.

Ticks  ->  Automatic  gives tick marks.

Options for Plot

The following options can be given for Plot. These cannot be used in Show. The default value of the option is given.

PlotStyle  ->  Automatic gives a list of lists of graphics directives to use for each curve.

PlotPoints  ->  25  gives the minimum number of points at which to sample the function.

Additional Options

For information on additional options, see pages 842 and 802.

Plot with Default Options

Plot[Sin[x], {x, 0, 7}];

Drawing a Frame

Plot[Sin[x], {x, 0, 7}, Frame -> True];

Drawing a Frame Using Show

Note that this gives the same plot as the last example.

Plot[Sin[x], {x, 0, 7}];

Show[ %, Frame -> True];

A Note on Using Show

Mathematica  saves information about every plot you produce, so you can later redraw it using Show. When you redraw plots, you can change some of the options you use. You may want to do this, for example, if you are trying to get the best possible setting of options. With a few exceptions, all the options that can be used in Plot can also be used in Show.

The Basic Plot

bp = Plot[Sin[x], {x, 0, 7}];

Axes Labels

Show[bp, AxesLabel -> {"x value", "Sin[x]"}];

Plot Label

Show[bp, PlotLabel -> "The Sine Function"];

Grid Lines

Show[bp, Frame -> True,
                    GridLines -> Automatic];

Moving the Origin

Show[bp, AxesOrigin -> {Pi, 0}];

Specifying Tick Marks

This gives tick marks at specified positions on the x  axis, and chooses tick marks automatically on the y  axis.

Show[ bp, 
   Ticks -> {{0,  Pi, 2 Pi}, Automatic}];

Tick Marks without Labels

This adds tick marks but no labels at multiples of Pi/2.

Show[ bp, Ticks ->
{{0, {Pi/2, ""}, Pi, {3Pi/2, ""}, 2 Pi},
                                Automatic} ];

Labeling the Frame

Show[ bp, Frame -> True, 
FrameLabel -> 
  {"label1", "label2", "label3", "label4"} ];

Adjusting the Aspect Ratio

Show[ bp, AspectRatio -> 1];

Adjusting the Plot Range

Show[bp,  PlotRange -> {0, 1.2}];

Changing the Gray Level of the Background and Plot Elements

Show[bp, Background -> GrayLevel[0.6],
               DefaultColor -> GrayLevel[1]];

Changing the Thickness of the Graph

Note that this option cannot be used in Show.

bp = Plot[Sin[x], {x, 0, 7}, PlotStyle -> 
        {{Thickness[0.02], GrayLevel[0.5]}}];

Combining Plots

Syntax (see pages 143 and 423)

Plot

Plot[{f1, f2, ...}, {x, xmin, xmax }] plots several functions together.

Plot[Evaluate[Table[f, ...]], {x, xmin, xmax }] generates a list of functions and then plots them together.

Show

Show[plot1, plot2, ...] combines several plots

Show[GraphicsArray[{{plot1 }, {plot2 }, ...}]] draws several plots side by side.

Show[GraphicsArray[{{plot11, plot12,...},  ...}]] draws a rectangular array of plots.

Show[GraphicsArray[plots, GraphicsSpacing -> {h, v }]] puts the specified horizonal and vertical spacing between the plots.

Show[Graphics[{Rectangle[{x1min, y1min },              {x1max, y1max }, plot1 ], Rectangle[{x2min, y2min }, {x2max, y2max }, plot2 ], ...}]]  draws the plots in specified rectangles.

Plotting Several Functions

Plot[{Sin[x], Sin[2x], Sin[3x]}, {x, 0, 2Pi}];

Plotting a Table of Functions

Plot[Evaluate[Table[BesselJ[n, x], {n, 4}]],
                            {x, 0, 10}];

A Note on Using Evaluate in Plot

When you ask Mathematica  to plot a function, say f , as a function of x , it first works out the values of x  that are needed, and only then does it evaluate f  with those values of x . There are, however, some cases in which it is much better to have Mathematica  evaluate f  before it starts to make the plot. A typical case is when f  is actually a command that generates a table of functions.  You want to have Mathematica  first produce the table, and then evaluate the function, rather than trying to produce the table afresh for each value of x . You can do this by typing                                      Plot[Evaluate[f ], {x, xmin, xmax }].

Plotting with Different Line Dashing

Plot[{Sin[x], Sin[2x]}, {x, 0, 2 Pi}, 
  PlotStyle -> 
   {{Thickness[0.01]}, {Dashing[{0.05, 0.05}]}} ];

Plotting with Different Colors

On a color display, the first Bessel function is plotted in red and the second in green.

Plot[ {BesselJ[1, x], BesselJ[2, x]}, {x, 0, 5},
    PlotStyle -> 
     {{RGBColor[1, 0, 0]}, {RGBColor[0, 1, 0]}}];

Combining Plots Using Show

Note that the scale in the combined plot is adjusted appropriately.

gj0 = Plot[BesselJ[0, x], {x, 0, 10}];

gj1 = Plot[BesselJ[1, x], {x, 0, 10}];

gjy = Show[gj0, gj1];

An Array of Plots

ga = Show[
         GraphicsArray[{{gj0, gjy}, {gj1, gjy}}]];

Using an Option on the Whole Array

Note that ga was defined in the previous example.

Show[ga, Frame -> True, FrameTicks -> None];

Changing the Options for All  Plots in the Array

Show[ ga /. 
       (Ticks -> Automatic) -> (Ticks -> None) ];

Changing the Spacing of the Array

Show[ GraphicsArray[{{gj0, gjy}, {gj1, gjy}},
          GraphicsSpacing -> {0, 0}]];

Specifying the Position of Plots

Show[Graphics[ {Rectangle[{0,0}, {1, 1}, gj0],
      Rectangle[{0.8, 0.8}, {1.2, 1.4}, gj1]} ]];

Contour Plots

Syntax (see page 151 and 425)

ContourPlot

ContourPlot[f,{x, xmin, xmax }, {y, ymin, ymax }]  makes a contour plot of f  as a function of x  and y.

ContourPlot[f,{x, xmin, xmax }, {y, ymin, ymax },        option ->value ]  produces a plot specifying a particular value for an option.

Options for ContourPlot and Show

The following options can be given for ContourPlot and Show.
The default value of the option is given.

ColorFunction -> Automatic    gives the  function specifying the color of  regions between contour lines.

ContourLines -> True   determines whether to draw explicit  contour lines.

Contours  -> 10   determines the total number of contours, or the list of z values for contours.

ContourShading ->  True   determines whether to shade the regions  between contours.

ContourSmoothing -> None   determines how to smooth contour lines.

ContourStyle  ->  Automatic   gives the style for contour lines.

Option for ContourPlot

The following option can be used in ContourPlot. It cannot be used in Show. The default value of the option is given.

PlotPoints  ->  15  gives the number of points in each direction at which to sample the function.

Additional options

For information on additional options for ContourPlot, see pages 151, 425, and 770. ContourPlot takes most of the options that specify the layout of the plot such as FrameLabel, etc. See the section above on Using Options with Plot.

Contour Plot with Default Options

Larger values of the function are lighter.

cp = ContourPlot[Sin[x] Sin[y], 
                   {x, -2, 2}, {y, -2, 2}];

Eliminating the Shading

Note that cp was defined in the previous example.

Show[cp, ContourShading -> False];

Increasing the Number of Contours and Smoothing

Show[cp, 
   Contours -> 30, ContourSmoothing -> Automatic,
   ContourShading -> False];

Specifying a Choice of Contours

Show[cp, Contours -> {-.5, .5},
               ContourShading -> False ];

A Note on Contour Plots

If you do not evaluate your function on a fine enough grid, there may be inaccuracies in your contour plot. One point to notice is that whereas a curve generated by Plot may be inaccurate if your function varies too quickly in a particular region, the shape of contours can be inaccurate if your function varies too slowly. A rapidly varying function gives a regular pattern of contours, but a function that is almost flat can give irregular contours. You can often use the ContourSmoothing option to ContourPlot to reduce the visual impact of the contours.

Density Plots

Syntax (see pages 151 and 425)

DensityPlot

DensityPlot[f,{x, xmin, xmax }, {y, ymin, ymax }] makes a density plot.

DensityPlot[f,{x, xmin, xmax }, {y, ymin, ymax },            option ->value ]  produces a plot specifying a particular value for an option.

Options for DensityPlot and Show

The following options can be given for DensityPlot and Show.
The default value of the option is given.

ColorFunction  ->  Automatic   gives a  function specifying the color for each cell .

Mesh  ->  True  determines whether to draw a mesh.

Option for DensityPlot

The folowing option can be used in DensityPlot. It cannot be used in Show. The default value of the option is given.

PlotPoints  ->  15   gives the number of points in each direction at which to sample the function.

Additional options

For information on additional options for DensityPlot, see pages 151, 425, and 777. DensityPlot takes most of the options that specify the layout of the plot such as FrameLabel, etc. See the section above on Using Options with Plot.

Density Plot with Default Options

dp = DensityPlot[Sin[x] Sin[y], 
                   {x, -2, 2}, {y, -2, 2}];

Removing the Mesh

Note that dp was defined in the previous example.

Show[dp, Mesh -> False];

Changing the Color Map

Show[dp, ColorFunction -> (GrayLevel[#^3]&)];

Increasing the Number of Evaluation Points

Note that this option cannot be used in Show.

DensityPlot[ Abs[Gamma[x + I y]] , {x, -3, 3}, 
 {y, -2, 2}, PlotPoints -> 40];

List Plots

Syntax (see page 164)

ListPlot[{y1, y2, ...}]  plots y1, y2, ... at x  values of 1, 2, ....

ListPlot[{{x1, y1 }, {x2, y2 },...}] plots points (x1, y1), ...

ListPlot[list , PlotJoined -> True]  joins the points with lines.

List Plot

t = Table[i^2, {i, 10}]

ListPlot[t];

List Plot of Pairs

s = Table[ {x, x^2}, {x, 0, 1, 0.1}]

ListPlot[s];

Joining the Points

Note that s was defined in the previous example.

ListPlot[s, PlotJoined -> True];

A Note on Using Options with ListPlot

You can use many of the same options with ListPlot as you use with Plot.  Most of these options can be given in Show. For more information, see the section Using Options with Plot above and pages 164 and 820.

Drawing a Frame

fp = ListPlot[Table[Prime[n], {n, 30}], 
                           Frame -> True];

Adding a Label

Note that fp was defined in the previous example.

Show[ fp , PlotLabel -> "Prime Numbers"];

Plotting the Roots of a Polynomial in the Plane

By specifying PointSize in the option PlotStyle you can use larger points for plotting.

ListPlot[{Re[z], Im[z]} /.
   NSolve[ z^5 == 1, z], AspectRatio -> Automatic,
                   PlotStyle -> {PointSize[0.02]}];

Parametric Plots

Syntax (see page 167)

ParametricPlot[{fx, fy }, {t, tmin, tmax }] produces a parametric plot with x  and y  coordinates fx  and fy  generated as a function of t .

ParametricPlot[{fx, fy }, {t, tmin, tmax }, option -> value]  produces a plot specifying a particular value for an option.

Parametric Plot

pp = ParametricPlot[{Sin[t], Sin[2t]}, {t, 0, 2Pi}];

Adjusting the Aspect Ratio

Setting the option AspectRatio to Automatic makes Mathematica  preserve the "true shape" of the curve, as defined by the actual coordinates values it involves. Note that pp was defined in the previous example.

Show[pp, AspectRatio -> Automatic];

A Note on Options for ParametricPlot

The options you can give for ParametricPlot are the same as for Plot. For more information, see the section Using Options with Plot above and pages 839, 842 and 802.

Plotting Several Curves

ParametricPlot[
   {{Sin[t], Sin[2t]},{Sin[t], Cos[t]}},
          {t, 0, 2Pi}, AspectRatio -> Automatic];

Two-Dimensional Graphics Elements

Syntax (see page 407)

Graphics

Graphics[primitives ] represents a two-dimensional graphics image.

Show

Show[Graphics[primitives ], options ] displays the two-dimensional graphics with specified options.

The following graphics primitives can be used:

Point[{x, y }]  gives a point at position {x , y }.

Line[{{x1, y1 }, {x2, y2 }, ... }]  gives a line through the points {x1, y1}, {x2, y2 }, ...

Rectangle[{xmin, ymin }, {xmax, ymax }]  gives a filled rectangle with specified corners.

Polygon[{{x1, y1 },{x2, y2 }, ... }]  gives a filled polygon with specified list of vertices.

Circle[{x, y }, r ]   gives a circle with radius r  centered at          {x , y }.

Circle[{x, y }, {rx, ry }]  gives an ellipse with semi-axes rx  and ry .

Circle[{x, y }, r, {theta1, theta2 }]  gives a circular arc.

Circle[{x, y }, {rx, ry },{theta1, theta2 }]  gives an elliptical arc.

Disk[{x, y }, r ], etc.  gives filled disks.

Text[expr, {x, y }]  gives text centered at the point {x , y }.

Lines

sawline = Line[Table[{n, (-1)^n}, {n, 6}]]

sawgraph = Show[ Graphics[sawline] ];

Using an Option

This adds axes to the plot in the previous example.

Show[ sawgraph, Axes -> True];

Rectangles

Note that when you use Graphics the argument must be a list.

boxes = {Rectangle[{1, -1}, {2, -0.6}],
               Rectangle[{4, 0.3}, {5, 0.8}]}

Show[ Graphics[ boxes ] ];

Combining Plots

This combines the plots of the lines and rectangles given above.

Show[ Graphics[ {boxes, sawline} ] ];

Text

This adds text at a specified position to the plot of lines above.

Show[ Graphics[ 
   {{Text["some text", {2, 0.5}]}, sawline} ] ];

Polygon

With the default choice of aspect ratio, the pentagon looks somewhat squashed. This is adjusted using AspectRatio in the second plot.

pentagon = Table[{Sin[2 Pi n/5], Cos[2 Pi n/5]},
                                    {n, 5}]

Show[ Graphics[ Polygon[pentagon] ] ];

Show[ %, AspectRatio -> Automatic];

Circles

Show[ Graphics[
      {Circle[{0, 0}, 2], Circle[{1, 1}, 2]} ],
                       AspectRatio -> Automatic];

Disks

This shows a sequence of disks with progressively larger semi-axes in the x direction, and progressively smaller ones in the y  direction.

Show[ Graphics[
Table[Disk[{3n, 0}, {n/4., 2-n/4}], {n, 4}] ], 
                        AspectRatio -> Automatic];

Wedge

Show[ Graphics[
          Disk[{0, 0}, 1, {0, 140 Degree}] ],
                        AspectRatio -> Automatic];

Array of Gray Levels

modtab = 
       Table[Mod[i, j]/3, {i, 4}, {j, 4}] //N

Show[ Graphics[ Raster[ modtab ] ] ];

Directives

Syntax (see page 407)

The following directives can be used:

AbsoluteDashing[{d1, d2, ... }] gives an absolute line dashing specification.

AbsolutePointSize[d ] gives an absolute point size specification.

AbsoluteThickness[d ] gives an absolute line thickness specification.

CMYKColor[c, m, y, k ] gives a color specification.

Dashing[{r1, r2, ... }] gives a line dashing specification.

GrayLevel[i ] gives an intensity specification.

Hue[h ] gives a hue specification.

RGBColor[r, g, b ] gives a color specification.

PointSize[r ] gives a point size specification.

Thickness[r ] gives a line thickness specification.

Directive That Affects All Primitives

{GrayLevel[.05], Rectangle[{0, 0}, {1, 1}],
 Rectangle[{1, 1}, {2, 2}]}

Show[ Graphics[ % ] ];

Directive That Affects a Single Primitive

In contrast to the previous example, only the rectangle in the first sublist is affected by the GrayLevel directive.

{{GrayLevel[.05], Rectangle[{0, 0}, {1, 1}]},
 Rectangle[{1, 1}, {2, 2}]}

Show[ Graphics[ % ] ];

A Note on Graphics Directives

When you set up a graphics object in Mathematica , you typically give a list of graphical elements. You can include in that list graphics directives which specify how subsequent elements in the list should be specified. In general, the graphical elements in a particular graphics object can be given in a collection of nested lists. When you insert graphics directives in this kind of structure, the rule is that a particular graphics directive affects all subsequent elements of the list it is in, together with all elements of sublists that may occur.  The graphics directive does not, however, have any effect outside the list it is in.

Adjusting the Point Size

dots = Table[Point[{n, Prime[n]}], {n, 6}]

Show[Graphics[{PointSize[0.1], dots}], 
                   PlotRange -> All];

Adjusting the Line Thickness

Table[
 {AbsoluteThickness[n], Line[{{0, 0}, {n, 1}}]},
                            {n, 4}]

Show[ Graphics[ % ] ];

Dashed Line

Show[Graphics[{Dashing[{0.05, 0.05}],
                  Line[{{-1, -1}, {1, 1}}]} ] ];

Dot-Dashed Line

Show[Graphics[{Dashing[{0.01, 0.05, 0.05, 0.05}],
                  Line[{{-1, -1}, {1, 1}}]} ] ];