The Structure of Graphics

"Graphics and Sound" discusses how to use functions like (RefLink[Plot, paclet:ref/Plot]) and (RefLink[ListPlot, paclet:ref/ListPlot]) to plot graphs of functions and data. This tutorial discusses how the Wolfram Language represents such graphics, and how you can program the Wolfram Language to create more complicated images.

The basic idea is that the Wolfram Language represents all graphics in terms of a collection of graphics primitives. The primitives are objects like (RefLink[Point, paclet:ref/Point]), (RefLink[Line, paclet:ref/Line]), and (RefLink[Polygon, paclet:ref/Polygon]), which represent elements of a graphical image, as well as directives such as (RefLink[RGBColor, paclet:ref/RGBColor]) and (RefLink[Thickness, paclet:ref/Thickness]).

This generates a plot of a list of points:

ListPlot[Table[Prime[n],{n,20}]]

(RefLink[InputForm, paclet:ref/InputForm]) shows how the Wolfram Language represents the graphics. Each point is represented as a coordinate in a (RefLink[Point, paclet:ref/Point]) graphics primitive. All the various graphics options used in this case are also given:

InputForm[%]

Each complete piece of graphics in the Wolfram Language is represented as a graphics object. There are several different kinds of graphics object, corresponding to different types of graphics. Each kind of graphics object has a definite head that identifies its type.

Graphics objects in the Wolfram Language.

The functions like (RefLink[Plot, paclet:ref/Plot]) and (RefLink[ListPlot, paclet:ref/ListPlot]) discussed in "The Structure of Graphics and Sound" all work by building up Wolfram Language graphics objects and then displaying them.

You can create other kinds of graphical images in the Wolfram Language by building up your own graphics objects. Since graphics objects in the Wolfram Language are just symbolic expressions, you can use all the standard Wolfram Language functions to manipulate them.

Graphics objects are automatically formatted by the Wolfram System front end as graphics upon output. Graphics may also be printed as a side effect using the (RefLink[Print, paclet:ref/Print]) command.

The (RefLink[Graphics, paclet:ref/Graphics]) object is computed by the Wolfram Language, but its output is suppressed by the semicolon:

Graphics[Circle[]];
2+2

A side effect output can be generated using the (RefLink[Print, paclet:ref/Print]) command. It has no (RefLink[Out, paclet:ref/Out])[] label because it is a side effect:

Print[Graphics[Circle[]]];
2+2

Displaying graphics objects.

(RefLink[Show, paclet:ref/Show]) can be used to change the options of an existing graphic or to combine multiple graphics.

This uses (RefLink[Show, paclet:ref/Show]) to adjust the (RefLink[Background, paclet:ref/Background]) option of an existing graphic:

g1=Plot[Sin[x],{x,0,2Pi}];
Show[g1, Background->Pink]

This uses (RefLink[Show, paclet:ref/Show]) to combine two graphics. The values used for (RefLink[PlotRange, paclet:ref/PlotRange]) and other options are based upon those which were set for the first graphic:

Show[{g1,Graphics[Circle[]]}]

Here, new options are specified for the entire graphic:

Show[{g1,Graphics[Circle[]]},PlotRange->All,AspectRatio->Automatic]

Local and global ways to modify graphics.

Given a particular list of graphics primitives, the Wolfram Language provides two basic mechanisms for modifying the final form of graphics you get. First, you can insert into the list of graphics primitives certain graphics directives, such as (RefLink[RGBColor, paclet:ref/RGBColor]), which modify the subsequent graphical elements in the list. In this way, you can specify how a particular set of graphical elements should be rendered.

This creates a two-dimensional graphics object that contains the (RefLink[Polygon, paclet:ref/Polygon]) graphics primitive:

poly=Polygon[Table[N[{Cos[n Pi/5],Sin[n Pi/5]}],{n,0,5}]];
Graphics[poly]

(RefLink[InputForm, paclet:ref/InputForm]) shows the complete graphics object:

InputForm[%]

This takes the graphics primitive created above and adds the graphics directives (RefLink[RGBColor, paclet:ref/RGBColor]) and (RefLink[EdgeForm, paclet:ref/EdgeForm]):

Graphics[{RGBColor[0.3,0.5,1],EdgeForm[Thickness[0.01]],poly}]

By inserting graphics directives, you can specify how particular graphical elements should be rendered. Often, however, you want to make global modifications to the way a whole graphics object is rendered. You can do this using graphics options.

By adding the graphics option (RefLink[Frame, paclet:ref/Frame]) you can modify the overall appearance of the graphics:

Show[%,Frame->True]

(RefLink[InputForm, paclet:ref/InputForm]) shows that the option was introduced into the resulting (RefLink[Graphics, paclet:ref/Graphics]) object:

InputForm[%]

You can specify graphics options in (RefLink[Show, paclet:ref/Show]). As a result, it is straightforward to take a single graphics object and show it with many different choices of graphics options.

Notice however that (RefLink[Show, paclet:ref/Show]) always returns the graphics objects it has displayed. If you specify graphics options in (RefLink[Show, paclet:ref/Show]), then these options are automatically inserted into the graphics objects that (RefLink[Show, paclet:ref/Show]) returns. As a result, if you call (RefLink[Show, paclet:ref/Show]) again on the same objects, the same graphics options will be used, unless you explicitly specify other ones. Note that in all cases new options you specify will overwrite ones already there.

Finding the options for a graphics object.

Some graphics options can be used as options to visualization functions that generate graphics. Options which can take the right-hand side of (RefLink[Automatic, paclet:ref/Automatic]) are sometimes resolved into specific values by the visualization functions.

Here is a plot:

zplot=Plot[Abs[Zeta[1/2+I x]],{x,0,10},PlotRange->Automatic]

The Wolfram Language uses an internal algorithm to compute an explicit value for (RefLink[PlotRange, paclet:ref/PlotRange]) in the resulting graphic:

Options[zplot,PlotRange]

Finding the complete form of a piece of graphics.

When you use a graphics option such as (RefLink[Axes, paclet:ref/Axes]), the Wolfram System front end automatically draws objects such as axes that you have requested. The objects are represented merely by the option values rather than by a specific list of graphics primitives. Sometimes, however, you may find it useful to represent these objects as the equivalent list of graphics primitives. The function (RefLink[FullGraphics, paclet:ref/FullGraphics]) gives the complete list of graphics primitives needed to generate a particular plot, without any options being used.

This plots a list of values:

ListPlot[Table[EulerPhi[n],{n,10}]]

(RefLink[FullGraphics, paclet:ref/FullGraphics]) yields a graphics object that includes graphics primitives representing axes and so on:

Short[InputForm[FullGraphics[%]],6]