Chapter 6. Two-Dimensional Graphics and Plots

I’ve been looking so long at these pictures of you that I almost believe that they’re real I’ve been living so long with my pictures of you that I almost believe that the pictures are all I can feel

One of the features that places Mathematica in a class by itself among similar computer-aided mathematics tools is its advanced graphics capabilities. This chapter focuses on two-dimensional graphics. Mathematica provides a variety of plotting functions with a versatile set of options for customizing their display. The most common types of 2D graphic are the plot of a function and list plots of values. 6.1 Plotting Functions in Cartesian Coordinates covers Plot and 6.4 Plotting Data covers ListPlot. Frequently you will want to use other coordinate systems or scales. In two dimensions, PolarPlot and ParametricPlot are often used as demonstrated in 6.1 Plotting Functions in Cartesian Coordinates and 6.2 Plotting in Polar Coordinates.

True to its symbolic nature, Mathematica represents all graphics as collections of graphics primitives and directives. Primitives include objects such as Point and Line; directives provide styling information such as Thickness and Hue. Mathematica allows you to work with the low-level primitives (see 6.8 Displaying 2D Geometric Shapes), but most readers will be interested in the higher-level functions like Plot and ListPlot, which generate graphics from functions and data and display them. However, it is easy to demonstrate that these functions generate primitives by specifying InputForm.

In[1]:=  ListPlot[{0, 1, 2, 3}] // InputForm
Out[1]//InputForm=
             Graphics[{Hue[0.67, 0.6, 0.6],
               Point[{{1., 0.}, {2., 1.}, {3., 2.}, {4., 3.}}]},
              {AspectRatio -> GoldenRatio^(-1), Axes -> True,
               AxesOrigin -> {0, Automatic},
               PlotRange -> Automatic, PlotRangeClipping -> True}]

This uniform representation allows graphics to be manipulated programmatically, just like any Mathematica object, and sometimes can be useful for generating custom effects. However, this representation is not entirely at the lowest level, because graphics constructs like axes are implicitly specified via options. To get to the lowest level you can use the function FullGraphics. Here I use Short to suppress some of the details.

    In[2]:=  Short[InputForm[FullGraphics[ListPlot[{0, 1, 2, 3}]]], 10]
Out[2]//Short=
             Graphics[{{Hue[0.67, 0.6, 0.6], Point[{{1., 0.}, {2., 1.}, {3.,
               2.}, {4., 3.}}]}, {{GrayLevel[0.], AbsoluteThickness[0.25],
               Line[{{0.2, 0.}, {0.2, 0.010112712429686845}}]}, Text[0.2,
               {0.2, -0.02022542485937369}, {0., 1.}], {GrayLevel[0.],
               AbsoluteThickness[0.25], Line[{{0.4, 0.}, {0.4,
               0.010112712429686845}}]}, Text[0.4, {0.4, -0.02022542485937369},
               {0., 1.}], {GrayLevel[0.], AbsoluteThickness[0.25],
               Line[{{0.6000000000000001, 0.}, {0.6000000000000001,
               0.010112712429686845}}]}, Text[0.6000000000000001,
               {0.6000000000000001, -0.02022542485937369}, {0., 1.}],
               {GrayLevel[0.], AbsoluteThickness[0.25], Line[{{0.8, 0.}, {0.8,
               0.010112712429686845}}]}, <<41>>, {GrayLevel [0.], <<2>>},
               {GrayLevel[0.], AbsoluteThickness[0.125], Line[{{0., 0.9}, {0.00375,
               0.9}}]}, {GrayLevel[0.], AbsoluteThickness[0.125], Line[{{0.,
               0.9500000000000001}, {0.00375, 0.9500000000000001}}]}, {GrayLevel[0.],
               AbsoluteThickness[0.25], Line[{{0., 0.}, {0., 1.}}]}}}]

In the recipes that follow, I make frequent use of GraphicsRow, GraphicsColumn, and GraphicsGrid. These are handy for formatting multiple graphics outputs across the page to make maximum use of both horizontal and vertical space. Both GraphicsRow and GraphicsColumn take a list of graphics to format, whereas GraphicsGrid takes a matrix. To help generate these lists and matrices, I sometimes use Table and Partition. These functions are simple enough that I hope they do not detract from the intended lesson of the recipe. 6.6 Displaying Multiple Graphs in a Grid explains the use of these gridlike formatting functions in detail.

Plot has a wide variety of options for controlling the appearance of the plot. Here are the defaults.

Discussion

When plotting two or more functions, you may want to explicitly set the style of each plot’s lines. You can also suppress one or both of the axes using Axes, as I do in the second and fourth plots. You can label one or both of the axes using AxesLabel and control the format using LabelStyle.

Discussion

PlotLabel is a handy option for naming plots, especially when you display several plots at a time.

Discussion

You can add grid lines with an explicitly determined frequency or a frequency determined automatically by Mathematica.

Discussion

Frame, FrameStyle, and FrameLabel let you annotate the graph with a border and label. Note that FrameStyle and FrameLabel only have effect if FrameTrue is also specified.

Discussion

Mesh is an option that allows you to highlight specific points in the plot. Mesh → All will highlight all points sampled while plotting the graph, Mesh → Full will use regularly spaced points. Mesh → n will use n equally spaced points. The behavior of Mesh → Automatic will vary based on the plotting primitive.

Discussion

PlotRange is an important option that controls what coordinates to include in the plot. Automatic lets Mathematica decide on the best choice, All specifies all points actually plotted, and Full specifies the entire range. In addition, you can supply explicit coordinates in the form {{xmin,xmax},{ymin,ymax}}.

Discussion

AspectRatio controls the ratio of height to width of the plot. The default value is 1/GoldenRatio (also known as ϕ). A value of Automatic uses the coordinate values to determine the aspect ratio.

Discussion

Sometimes you want to emphasize an area on one side of the curve or between two different curves. Filling can be set to Top to fill from the curve upward, Bottom to fill from the curve downward, Axis to fill from the axis to the curve, or to a numeric value to fill from the curve to that value in either y direction.

Discussion

FillingStyle allows you to control the color and opacity of the filling. Specifying an opacity is useful where regions of multiple functions overlap.

Discussion

You can also use a special notation to fill the area between two curves. In this notation, you refer to a curve by {i} where i is an integer referring to the ith plot. You can then say something like Filling → {i → {j}} to specify that filling should be between plot i and plot j. You can also override the FillingStyle by including a graphics directive, as in the example here.

Discussion

6.2 Plotting in Polar Coordinates and 6.3 Creating Plots Parametrically demonstrate PolarPlot and ListPlot, which share most of the options of Plot.

One of the keys to getting the most out of the graphics primitives is to learn how to combine them with graphics directives. Some directives are very specific, whereas others are quite general. For example, Arrowheads applies only to Arrow, whereas Red and Opacity apply to all primitives. A directive will apply to all objects that follow it, subject to scoping created by nesting objects within a list. For example, in the following graphic, Red applies to Disk and Rectangle but not Line because the line is given a specific color and thickness within its own scope.

Discussion

Color directives can use named colors: Red, Green, Blue, Black, White, Gray, Cyan, Magenta, Yellow, Brown, Orange, Pink, Purple, LightRed, LightGreen, LightBlue, LightGray, LightCyan, LightMagenta, LightYellow, LightBrown, LightOrange, LightPink, and LightPurple. You can also synthesize colors using RGBColor or Hue, CMYKColor, GrayLevel, and Blend. In Mathematica 6 or later versions, these directives can take opacity values in addition to values that define the color or gray settings. Blend is also new to Mathematica 6.

Discussion

Of course, you’ll need to try the code on your own to view the colors.

Thickness[r] is specified relative to the total width of the graphic and, therefore, scales with size changes. AbsoluteThickness[d] is specified in units of printer points (1/72 inch) and does not scale. Thick and Thin are predefined versions (0.25 and 2, respectively) of AbsoluteThickness. Thickness directives apply to primitives that contain lines such as Line, Polygon, Arrow, and the like.

Discussion

14.12 Visualizing Trees for Interest-Rate Sensitive Instruments applies Mathematica’s graphics primitives to the serious task of visualizing Hull-White trees, which are used in modeling interest-rate-sensitive securities.

13.11 Modeling Truss Structures Using the Finite Element Method shows an application in constructing finite element diagrams used in engineering.