This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / The Structure of Graphics and Sound /

2.10.3 Graphics Directives and Options

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 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 GrayLevel.

In[1]:= {{GrayLevel[0.5], Rectangle[{0, 0}, {1, 1}]},
Rectangle[{1, 1}, {2, 2}]}


Only the rectangle in the first sublist is affected by the GrayLevel directive.

In[2]:= Show[Graphics[ % ]]


Mathematica provides various kinds of graphics directives. One important set is those for specifying the colors of graphical elements. Even if you have a black-and-white display device, you can still give color graphics directives. The colors you specify will be converted to gray levels at the last step in the graphics rendering process. Note that you can get gray-level display even on a color device by setting the option ColorOutput -> GrayLevel.

Basic Mathematica color specifications.

On a color display, the two curves are shown in color. In black and white they are shown in gray.

In[3]:= Plot[{BesselI[1, x], BesselI[2, x]}, {x, 0, 5},
PlotStyle ->
{{RGBColor[1, 0, 0]}, {RGBColor[0, 1, 0]}}]


The function Hue[h] provides a convenient way to specify a range of colors using just one parameter. As h varies from 0 to 1, Hue[h] runs through red, yellow, green, cyan, blue, magenta, and back to red again. 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.

For most purposes, you will be able to specify the colors you need simply by giving appropriate RGBColor or Hue directives. However, if you need very precise or repeatable colors, particularly for color printing, there are a number of subtleties which arise, as discussed in Section 2.10.17.

When you give a graphics directive such as RGBColor, it affects all subsequent graphical elements that appear in a particular list. Mathematica also supports various graphics directives which affect only specific types of graphical elements.

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

Mathematica also provides the graphics directive AbsolutePointSize[d], which allows you to specify the "absolute" diameter of points, measured in fixed units. The units are of an inch, approximately printer's points.

Graphics directives for points.

Here is a list of points.

In[4]:= Table[Point[{n, Prime[n]}], {n, 6}]


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

In[5]:= Show[Graphics[{PointSize[0.1], %}], PlotRange -> All]


Here each point has size 3 in absolute units.

In[6]:= ListPlot[Table[Prime[n], {n, 20}],
Prolog -> AbsolutePointSize[3]]


Graphics directives for lines.

This generates a list of lines with different absolute thicknesses.

In[7]:= Table[
{AbsoluteThickness[n], Line[{{0, 0}, {n, 1}}]}, {n, 4}]


Here is a picture of the lines.

In[8]:= Show[Graphics[%]]


The 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. 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.

In[9]:= Show[Graphics[ {Dashing[{0.05, 0.05}],
Line[{{-1, -1}, {1, 1}}]} ]]


This gives a dot-dashed line.

In[10]:= Show[Graphics[{Dashing[{0.01, 0.05, 0.05, 0.05}],
Line[{{-1, -1}, {1, 1}}]}]]


One way to use Mathematica 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 the curve is given in a style specified by graphics directives.

In[11]:= Plot[BesselJ[2, x], {x, 0, 10},
PlotStyle -> {{Thickness[0.02], GrayLevel[0.5]}}]


Some typical styles.

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

Graphics options that affect whole plots.

This draws the whole plot on a gray background.

In[12]:= Plot[Sin[Sin[x]], {x, 0, 10},
Background -> GrayLevel[0.6]]


This makes the default color white.

In[13]:= Show[%, DefaultColor -> GrayLevel[1]]