The Structure of Graphics

"Graphics and Sound" discusses how to use functions like Plot and ListPlot to plot graphs of functions and data. This tutorial discusses 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, which represent elements of a graphical image, as well as directives such as RGBColor and Thickness.

This generates a plot of a list of points.
In[1]:=
Click for copyable input
Out[1]=
InputForm shows how Mathematica represents the graphics. Each point is represented as a coordinate in a Point graphics primitive. All the various graphics options used in this case are also given.
In[2]:=
Click for copyable input
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 that identifies its type.

Graphics[list]general two-dimensional graphics
Graphics3D[list]general three-dimensional graphics

Graphics objects in Mathematica.

The functions like Plot and ListPlot discussed in "The Structure of Graphics and Sound" all work by building up Mathematica graphics objects and then displaying them.

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.

Graphics objects are automatically formatted by the Mathematica front end as graphics upon output. Graphics may also be printed as a side effect using the Print command.

The Graphics object is computed by Mathematica, but its output is suppressed by the semicolon.
In[3]:=
Click for copyable input
Out[3]=
A side effect output can be generated using the Print command. It has no Out[] label because it is a side effect.
In[4]:=
Click for copyable input
Out[4]=
Show[g,opts]display a graphics object with new options specified by opts
Show[g1,g2,...]display several graphics objects combined using the options from
Show[g1,g2,...,opts]display several graphics objects with new options specified by opts

Displaying graphics objects.

Show can be used to change the options of an existing graphic or to combine multiple graphics.

This uses Show to adjust the Background option of an existing graphic.
In[5]:=
Click for copyable input
Out[5]=
This uses Show to combine two graphics. The values used for PlotRange and other options are based upon those which were set for the first graphic.
In[6]:=
Click for copyable input
Out[6]=
Here, new options are specified for the entire graphic.
In[7]:=
Click for copyable input
Out[7]=
Graphics directivesExamples: RGBColor, Thickness
Graphics optionsExamples: PlotRange, Ticks, AspectRatio, ViewPoint

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 creates a two-dimensional graphics object that contains the Polygon graphics primitive.
In[8]:=
Click for copyable input
Out[8]=
InputForm shows the complete graphics object.
In[9]:=
Click for copyable input
Out[9]//InputForm=
This takes the graphics primitive created above and adds the graphics directives RGBColor and EdgeForm.
In[10]:=
Click for copyable input
Out[10]=

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[11]:=
Click for copyable input
Out[11]=
InputForm shows that the option was introduced into the resulting Graphics object.
In[12]:=
Click for copyable input
Out[12]//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.

Options[g]give a list of all graphics options for a graphics object
Options[g,opt]give the setting for a particular option

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 Automatic are sometimes resolved into specific values by the visualization functions.

Here is a plot.
In[13]:=
Click for copyable input
Out[13]=
Mathematica uses an internal algorithm to compute an explicit value for PlotRange in the resulting graphic.
In[14]:=
Click for copyable input
Out[14]=
FullGraphics[g]translate objects specified by graphics options into lists of explicit graphics primitives

Finding the complete form of a piece of graphics.

When you use a graphics option such as Axes, the Mathematica 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 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[15]:=
Click for copyable input
Out[15]=
FullGraphics yields a graphics object that includes graphics primitives representing axes and so on.
In[16]:=
Click for copyable input
Out[16]//Short=
New to Mathematica? Find your learning path »
Have a question? Ask support »