**2.9.1 The Structure of Graphics**

Section 1.9 discussed how to use functions like Plot and ListPlot to plot graphs of functions and data. In this section, we discuss how Mathematica represents such graphics, and how you can program Mathematica to create more complicated images.

The basic idea is that Mathematica represents all graphics in terms of a collection of graphics primitives. The primitives are objects like Point, Line and Polygon, that represent elements of a graphical image, as well as directives such as RGBColor, Thickness and SurfaceColor.

This generates a plot of a list of points.
In[1]:= **ListPlot[ Table[Prime[n], {n, 20}] ]**

InputForm shows how Mathematica represents the graphics. Each point is represented as a Point graphics primitive. All the various graphics options used in this case are also given.
In[2]:= **InputForm[%]**

Out[2]//InputForm=

Each complete piece of graphics in Mathematica 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 which identifies its type.

Graphics objects in *Mathematica*.

The functions like Plot and ListPlot discussed in Section 1.9 all work by building up Mathematica graphics objects, and then displaying them.

Generating graphics objects by plotting functions and data.

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

Once you have created a graphics object, you must then display it. The function Show allows you to display any Mathematica graphics object.

Displaying graphics objects.

This uses Table to generate a polygon graphics primitive.
In[3]:= **poly = Polygon[**

Table[N[{Cos[n Pi/5], Sin[n Pi/5]}], {n, 0, 5}] ]

Out[3]=

This creates a two-dimensional graphics object that contains the polygon graphics primitive. In standard output format, the graphics object is given simply as -Graphics-.
In[4]:= **Graphics[ poly ]**

Out[4]=

InputForm shows the complete graphics object.
In[5]:= **InputForm[%]**

Out[5]//InputForm=

This displays the graphics object you have created.
In[6]:= **Show[%]**

Local and global ways to modify graphics.

Given a particular list of graphics primitives, Mathematica 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 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 takes the list of graphics primitives created above, and adds the graphics directive GrayLevel[0.3].
In[7]:= **Graphics[ {GrayLevel[0.3], poly} ]**

Out[7]=

Now the polygon is rendered in gray.
In[8]:= **Show[%]**

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 Frame you can modify the overall appearance of the graphics.
In[9]:= **Show[%, Frame -> True]**

Show returns a graphics object with the options in it.
In[10]:= **InputForm[%]**

Out[10]//InputForm=

You can specify graphics options in 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 Show always returns the graphics objects it has displayed. If you specify graphics options in Show, then these options are automatically inserted into the graphics objects that Show returns. As a result, if you call 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 work by requiring you to specify a particular value for a parameter related to a piece of graphics. Other options allow you to give the setting Automatic, which makes Mathematica use internal algorithms to choose appropriate values for parameters. In such cases, you can find out the values that Mathematica actually used by applying the function FullOptions.

Here is a plot.
In[11]:= **zplot = Plot[Abs[Zeta[1/2 + I x]], {x, 0, 10}]**

The option PlotRange is set to its default value of Automatic, specifying that Mathematica should use internal algorithms to determine the actual plot range.
In[12]:= **Options[zplot, PlotRange]**

Out[12]=

FullOptions gives the actual plot range determined by Mathematica in this case.
In[13]:= **FullOptions[zplot, PlotRange]**

Out[13]=

Finding the complete form of a piece of graphics.

When you use a graphics option such as Axes, Mathematica effectively has to construct a list of graphics elements to represent the objects such as axes that you have requested. Usually Mathematica does not explicitly return the list it constructs in this way. Sometimes, however, you may find it useful to get this list. The function 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.
In[14]:= **ListPlot[ Table[EulerPhi[n], {n, 10}] ]**

FullGraphics yields a graphics object that includes graphics primitives representing axes and so on.
In[15]:= **Short[ InputForm[ FullGraphics[%] ], 6]**

Out[15]//Short=

With their default option settings, functions like Plot and Show actually cause Mathematica to generate graphical output. In general, the actual generation of graphical output is controlled by the graphics option DisplayFunction. The default setting for this option is the value of the global variable $DisplayFunction.

In most cases, $DisplayFunction and the DisplayFunction option are set to use the lower-level rendering function Display to produce output, perhaps after some preprocessing. Sometimes, however, you may want to get a function like Plot to produce a graphics object, but you may not immediately want that graphics object actually rendered as output. You can tell Mathematica to generate the object, but not render it, by setting the option DisplayFunction->Identity. Section 2.9.14 will explain exactly how this works.

Generating and displaying graphics objects.

This generates a graphics object, but does not actually display it.
In[16]:= **Plot[BesselJ[0, x], {x, 0, 10},**

DisplayFunction -> Identity]

Out[16]=

This modifies the graphics object, but still does not actually display it.
In[17]:= **Show[%, Frame -> True]**

Out[17]=

To display the graphic, you explicitly have to tell Mathematica to use the default display function.
In[18]:= **Show[%, DisplayFunction -> $DisplayFunction]**