Introduction to Mathematica: Two-Dimensional Plots

Mathematica’s flexible plotting system is one of it’s major strengths. In this tutorial, we will cover the basics and some subset of advanced features concerning generating two-dimensional plots. Given the flexibility of the system, however, there will be many more options available than it is reasonable for me to cover in a single tutorial. Once you get the basics down, I recommend exploring the documentation center’s explanations of the various plotting functions.

Page Navigation

Plotting Functions: Plot

The simplest plotting function is, not surprisingly, Plot. At its minimum, it takes two parameters: the function to plot, and a list designating the independent variable name used in that function and the domain to plot (i.e., the x-axis limits). Any real-valued function that has been defined can be plotted this way, including functions you have defined yourself.

Plotting multiple functions on the same graph is also quite easy. Simply place those functions in a list, as shown below.

It is tempting to try to combine the Table statement within the Plot statement in order to make the code more concise, as shown below. Notice, however, that when one does so, all of the plots are now the same color. This is because the Mathematica™ kernel interprets the Table statement as a single thing to plot rather than as a list. Forcing Mathematica™ to evaluate the Table statement first using an Evaluate call fixes that problem.

Improving Presentation

The visual appearance of plots can be improved in a wide variety of ways, usually by simply specifying options to the Plot function call. For example, when you have multiple plots on the same graph, it can become important to label them. There are numerous ways to do this, several of which are shown below.

The plot domain and range can be specified specifically. In nearly all options of this type, where you provide a list of parameters that refer to different axes, the x-axis parameter comes before the y-axis parameter.

The style of the lines can also be specified, either as a group or individually, as shown to the right. And multiple style options can be combined using Directive, as shown below.

You can label axes, or turn on a “Frame,” which consists of axes along all boundaries of the plot, as shown to the right.

You can change background colors, as shown below (this is generally a good idea… while I love the StandardReport stylesheet for reasons I have previously explained, one detail I dislike is the grey background that is the default on output cells).

All of these options and more can be combined to generate high-quality plots.

Plotting Data: ListPlot

When you have specific data points, such as from experimental data, a different function is typically used: ListPlot. As the name suggests, the function plots lists of data. And as you might imagine, generating the lists you will use for these plots will involve familiarity with Mathematica™’s list functionality. To the right you can see a basic example. First, I generated a function that will add some noise to the data (the noise is randomly distributed around zero with a Gaussian distribution and a standard deviation of 0.1). Notice that I used SetDelayed in this case rather than Set because I don’t want the noise to be a single random number that is then added to every data point, I want each data point to have a different random number added to it. Then I generate the data points by taking a function and adding noise to it within a Table construct. This resulting data list is just a one-dimensional list of the points, so ListPlot displays those points with the data point number (the “index” to the list” as the x-axis.

If you have specified x values you want to use, then you need the list to be of the form {{x1,y1},{x2,y2},…}; that is, each list element needs to itself be a (two-element) list that is an ordered pair of x and y values. We can do that by generating the x values that correspond to the y values we already determined, and then using the Transpose functionality.

Many of the same presentation options from Plot above can be used in ListPlot as well, as illustrated below.

Combining Plots: Show

Frequently we would like to combine multiple plots on the same graph. Mathematica™ has an easy-to-use, yet surprisingly sophisticated way to do this: the Show function. Show, very simply, takes multiple graphs and puts them on the same plot. For example:

Here, notice that the individual plots are being assigned to variables, and each of those definitions has a semicolon at the end suppressing their output. Show combines the two. A subtle but important point: where options would affect both plots (such as ImageSize, Background, LabelStyle, and even the plot range), those options are taken from the first entry in Show.

Drawing on a Plot: Graphics

For some plotting applications, it can be useful to draw graphics elements directly based on coordinates. A good example of this that is relevant to chemistry is energy level diagrams. Rather than expressing a line as a function, it would be much more convenient to simply specify the start point and end point of the line. In Mathematica™, we accomplish this by specifying graphics primitives, and then displaying them using the Graphics function. There are very many such primitives, only a few of which are shown to the right. An example using some of these primitives is shown below.

A selection of graphics primitives:

  • Line[{p1,p2,…}]: Straight lines connecting points. Each point specifies the coordinates of the point as an ordered pair (if the graphics are 2-dimensional), or ordered triplet (if the graphics are 3-dimensional).
  • Arrow: Called identically to Line, this primitive puts an arrow-head at the last point.
  • Polygon: A polygon whose corners are specified by the list of points.
  • Text: A graphics element corresponding to specified text positioned at a given coordinate.

Directives to specify display options (e.g., color, line thickness, etc.) can be specified as part of the Graphics call, and affect everything following the given directive, as shown to the right.

If the Graphics command is being executed on its own, the display coordinate range, by default, will be large enough to show all elements, but no larger. If, on the other hand, the Graphics command is part of a Show sequence, then the first element of Show will determine the coordinate range displayed. This allows graphics elements to be placed on plots generated by Plot or ListPlot using the same coordinate system. You will see examples of this in an upcoming tutorial.

Specialty Plots: ParametricPlot, ContourPlot, DensityPlot

Plot, ListPlot, and Graphics are only three of a vast range of ways to plot two-dimensional information in Mathematica™. I will show a handful of other such functions below, but will assume that you can look up their use in the built-in documentation center.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s