Graphics Directives and Options

When you set up a graphics object in the Wolfram Language, 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 rendered.

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.

The first sublist contains the graphics directive (RefLink[GrayLevel, paclet:ref/GrayLevel]):

{{GrayLevel[0.5],Rectangle[{0,0},{1,1}]},Rectangle[{1,1},{2,2}]}

Only the rectangle in the first sublist is affected by the (RefLink[GrayLevel, paclet:ref/GrayLevel]) directive:

Graphics[%]

Basic Wolfram Language color specifications.

The Wolfram Language accepts the names of many colors directly as color specifications. These color names, such as (RefLink[Red, paclet:ref/Red]), (RefLink[Gray, paclet:ref/Gray]), (RefLink[LightGreen, paclet:ref/LightGreen]), and (RefLink[Purple, paclet:ref/Purple]), are implemented as variables which evaluate to an (RefLink[RGBColor, paclet:ref/RGBColor]) specification. The color names can be used interchangeably with color directives.

The first plot is colored with a color name, while the second one has a fine-tuned (RefLink[RGBColor, paclet:ref/RGBColor]) specification:

Plot[{BesselI[1,x],BesselI[2,x]},{x,0,5},PlotStyle->{{Red},{RGBColor[0.3,0.7,0.1]}}]

The function (RefLink[Hue, paclet:ref/Hue])[h] provides a convenient way to specify a range of colors using just one parameter. As h varies from 0 to 1, (RefLink[Hue, paclet:ref/Hue])[h] runs through red, yellow, green, cyan, blue, magenta, and back to red again. (RefLink[Hue, paclet:ref/Hue])[h,s,b] allows you to specify not only the "hue", but also the "saturation" and "brightness" of a color. Taking the saturation to be equal to one gives the deepest colors; decreasing the saturation toward zero leads to progressively more "washed out" colors.

When you give a graphics directive such as (RefLink[RGBColor, paclet:ref/RGBColor]), it affects all subsequent graphical elements that appear in a particular list. The Wolfram Language also supports various graphics directives which affect only specific types of graphical elements.

The graphics directive (RefLink[PointSize, paclet:ref/PointSize])[d] specifies that all (RefLink[Point, paclet:ref/Point]) elements which appear in a graphics object should be drawn as circles with diameter d. In (RefLink[PointSize, paclet:ref/PointSize]), the diameter d is measured as a fraction of the width of your whole plot.

The Wolfram Language also provides the graphics directive (RefLink[AbsolutePointSize, paclet:ref/AbsolutePointSize])[d], which allows you to specify the "absolute" diameter of points, measured in fixed units. The units are (1/72) of an inch, approximately printer's points.

Graphics directives for points.

Here is a list of points:

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

This makes each point have a diameter equal to one-tenth of the width of the plot:

Graphics[{PointSize[0.1],%},PlotRange->All]

Here each point has size 3 in absolute units:

ListPlot[Table[Prime[n],{n,20}],PlotStyle->AbsolutePointSize[3]]

Graphics directives for lines.

This generates a list of lines with different absolute thicknesses:

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

Here is a picture of the lines:

Graphics[%]

The (RefLink[Dashing, paclet:ref/Dashing]) graphics directive allows you to create lines with various kinds of dashing. The basic idea is to break lines into segments which are alternately drawn and omitted. By changing the lengths of the segments, you can get different line styles. (RefLink[Dashing, paclet:ref/Dashing]) allows you to specify a sequence of segment lengths. This sequence is repeated as many times as necessary in drawing the whole line.

This gives a dashed line with a succession of equal-length segments:

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

This gives a dot-dashed line:

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

(RefLink[Dashing, paclet:ref/Dashing]) can be turned off by specifying an empty list. Here, (RefLink[Dashing, paclet:ref/Dashing]) is turned off for only the second line:

Graphics[{Dashing[{0.05}],Line[{{0,0},{1,1}}],
{Dashing[{}],Line[{{0,0},{2,1}}]},
Line[{{0,0},{3,1}}]}]

Graphics directives which require a numerical size specification can also accept values of (RefLink[Tiny, paclet:ref/Tiny]), (RefLink[Small, paclet:ref/Small]), (RefLink[Medium, paclet:ref/Medium]), and (RefLink[Large, paclet:ref/Large]). For each directive, these values have been fine-tuned to produce an appearance which will seem appropriate to the human eye.

This specifies a large thickness with medium dashing:

Plot[Sin[x], {x, 0, 2Pi}, PlotStyle->{Dashing[{Medium}], Thickness[Large]}]

This specifies that the entire multipoint should use large, green points:

Graphics[{PointSize[Large],Green,Point[{{0,0},{1,0.5},{2,0},{1,-.5}}]}]

The (RefLink[CapForm, paclet:ref/CapForm]) directive lets you specify the shapes of the end caps of lines. End cap shapes can be "Butt", "Square", or "Round".

This shows the different shapes available to (RefLink[CapForm, paclet:ref/CapForm]):

Graphics[{
Thickness[.07],
{CapForm["Butt"],Line[{{0,2},{5,2}}]},
{CapForm["Square"],Line[{{0,1},{5,1}}]},
{CapForm["Round"],Line[{{0,0},{5,0}}]}
}]

(RefLink[CapForm, paclet:ref/CapForm])["Butt"] specifies that lines end exactly at their endpoints. "Square" end caps extend one-half line width beyond the ends of a line. "Round" end caps are half-circles whose diameter is the width of the line.

You can also specify the shapes of the joins between line segments via the (RefLink[JoinForm, paclet:ref/JoinForm]) directive.

Here are the shapes available to (RefLink[JoinForm, paclet:ref/JoinForm]):

Graphics[{
Thickness[.07],
{JoinForm["Bevel"],Line[{{0,0},{1,1},{0,1}}]},
{JoinForm["Miter"],Line[{{2,0},{3,1},{2,1}}]},
{JoinForm["Round"],Line[{{4,0},{5,1},{4,1}}]}
},PlotRangePadding->.3]

Specifying the maximum length of miter joins.

When the angle between adjacent line segments is small, the point at a miter join can be very long. Excessively long points are by default truncated to bevel joins. The length at which this happens, the "miter limit", is by default set so that the points of 5-pointed stars are sharp, but more acute joins are beveled. You can specify an explicit miter limit using (RefLink[JoinForm, paclet:ref/JoinForm]) to control exactly when sharp joins are beveled. The miter limit is the number of line widths that the point at a join is allowed to extend past the vertex of the join before it is beveled.

The points on this 7-pointed star are blunted by the default miter limit:

Graphics[{EdgeForm[{Blue,Thickness[.06]}],
Polygon[Table[{Cos[a],Sin[a]},{a,0,6π,2π 3/7}]]},PlotRangePadding->.5]

Specifying an explicit larger miter limit gives a pointed star:

Graphics[{EdgeForm[{Blue,Thickness[.06],JoinForm[{"Miter",5}]}],Polygon[Table[{Cos[a],Sin[a]},{a,0,6π,2π 3/7}]]},PlotRangePadding->.5]

The (RefLink[RoundingRadius, paclet:ref/RoundingRadius]) option to (RefLink[Rectangle, paclet:ref/Rectangle]).

The corners of (RefLink[Rectangle, paclet:ref/Rectangle]) primitives can be rounded with the (RefLink[RoundingRadius, paclet:ref/RoundingRadius]) option, which specifies the radius of the rectangle's corners. The actual rounding is limited to the half-length of an adjacent side.

Here are rectangles with various amounts of corner rounding:

Graphics[{
Rectangle[{0,2},{2,3},RoundingRadius->0],
Rectangle[{3,2},{5,3},RoundingRadius->.2],
Rectangle[{0,0},{2,1},RoundingRadius->.5],
Rectangle[{3,0},{5,1},RoundingRadius->1]
}]

One way to use Wolfram Language graphics directives is to insert them directly into the lists of graphics primitives used by graphics objects. Sometimes, however, you want the graphics directives to be applied more globally, and for example to determine the overall "style" with which a particular type of graphical element should be rendered. There are typically graphics options which can be set to specify such styles in terms of lists of graphics directives.

Some graphics options for specifying styles.

This generates a plot in which all curves are specified to use the same style:

Plot[{BesselJ[1,x],BesselJ[2,x]},{x,0,10},PlotStyle->{{Thickness[0.02],Gray}}]

A different (RefLink[PlotStyle, paclet:ref/PlotStyle]) expression can be used to give specific styles to each curve:

Plot[{BesselJ[1,x],BesselJ[2,x]},{x,0,10},PlotStyle->{{Thickness[0.02],Gray}, {Red}}]

The various "style options" allow you to specify how particular graphical elements in a plot should be rendered. The Wolfram Language also provides options that affect the rendering of the whole plot.

Graphics options that affect whole plots.

This draws the plot in white on a gray background:

Plot[Sin[Sin[x]],{x,0,10},Background->Gray,PlotStyle->White]

This makes the axes white as well:

Show[%,BaseStyle->White]