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

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

2.10.2 Two-Dimensional Graphics Elements

Basic two-dimensional graphics elements.

Here is a line primitive.

In[1]:= sawline = Line[Table[{n, (-1)^n}, {n, 6}]]

Out[1]=

This shows the line as a two-dimensional graphics object.

In[2]:= sawgraph = Show[ Graphics[sawline] ]

Out[2]=

This redisplays the line, with axes added.

In[3]:= Show[ %, Axes -> True ]

Out[3]=

You can combine graphics objects that you have created explicitly from graphics primitives with ones that are produced by functions like Plot.

This produces an ordinary Mathematica plot.

In[4]:= Plot[Sin[Pi x], {x, 0, 6}]

Out[4]=

This combines the plot with the sawtooth picture made above.

In[5]:= Show[%, sawgraph]

Out[5]=

You can combine different graphical elements simply by giving them in a list. In two-dimensional graphics, Mathematica will render the elements in exactly the order you give them. Later elements are therefore effectively drawn on top of earlier ones.

Here is a list of two Rectangle graphics elements.

In[6]:= {Rectangle[{1, -1}, {2, -0.6}],
Rectangle[{4, .3}, {5, .8}]}

Out[6]=

This draws the rectangles on top of the line that was defined above.

In[7]:= Show[ Graphics[ {sawline, %} ]]

Out[7]=

The Polygon graphics primitive takes a list of , coordinates, corresponding to the corners of a polygon. Mathematica joins the last corner with the first one, and then fills the resulting area.

Here are the coordinates of the corners of a regular pentagon.

In[8]:= pentagon = Table[{Sin[2 Pi n/5], Cos[2 Pi n/5]}, {n, 5}]

Out[8]=

This displays the pentagon. With the default choice of aspect ratio, the pentagon looks somewhat squashed.

In[9]:= Show[ Graphics[ Polygon[pentagon] ] ]

Out[9]=

This chooses the aspect ratio so that the shape of the pentagon is preserved.

In[10]:= Show[%, AspectRatio -> Automatic]

Out[10]=

Mathematica can handle polygons which fold over themselves.

In[11]:= Show[Graphics[
Polygon[ {{-1, -1}, {1, 1}, {1, -1}, {-1, 1}} ] ]]

Out[11]=

Circles and disks.

This shows two circles with radius 2. Setting the option AspectRatio -> Automatic makes the circles come out with their natural aspect ratio.

In[12]:= Show[ Graphics[
{Circle[{0, 0}, 2], Circle[{1, 1}, 2]} ],
AspectRatio -> Automatic ]

Out[12]=

This shows a sequence of disks with progressively larger semi-axes in the direction, and progressively smaller ones in the direction.

In[13]:= Show[ Graphics[
Table[Disk[{3n, 0}, {n/4, 2-n/4}], {n, 4}] ],
AspectRatio -> Automatic ]

Out[13]=

Mathematica allows you to generate arcs of circles, and segments of ellipses. In both cases, the objects are specified by starting and finishing angles. The angles are measured counterclockwise in radians with zero corresponding to the positive direction.

This draws a wedge centered at the origin.

In[14]:= Show[ Graphics[ Disk[{0, 0}, 1, {0, 140 Degree}] ],
AspectRatio -> Automatic ]

Out[14]=

Raster-based graphics elements.

Here is a array of values between 0 and 1.

In[15]:= modtab = Table[Mod[i, j]/3, {i, 4}, {j, 4}] // N

Out[15]=

This uses the array of values as gray levels in a raster.

In[16]:= Show[ Graphics[ Raster[%] ] ]

Out[16]=

This shows two overlapping copies of the raster.

In[17]:= Show[ Graphics[ {Raster[modtab, {{0, 0}, {2, 2}}],
Raster[modtab, {{1.5, 1.5}, {3, 2}}]} ] ]

Out[17]=

In the default case, Raster always generates an array of gray cells. As described in Section 2.10.7, you can use the option ColorFunction to apply a "coloring function" to all the cells.

You can also use the graphics primitive RasterArray. While Raster takes an array of values, RasterArray takes an array of Mathematica graphics directives. The directives associated with each cell are taken to determine the color of that cell. Typically the directives are chosen from the set GrayLevel, RGBColor or Hue. By using RGBColor and Hue directives, you can create color rasters using RasterArray.