Plotting lists of (x, y) points

We've seen that Mathematica uses parentheses for grouping and square brackets in functions. Mathematica uses curly braces to delimit lists of numbers. For example, a list of the first ten prime numbers would be

   {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}
If you enter this as Mathematica input, you'll get the exact same list as output:
   {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}
This is because we haven't asked Mathematica to do anything to the list. Later on we'll see how we can use lists of numbers in calculations.

The reason we want to discuss lists now is that Mathematica represents (x, y) points as lists of two numbers. For example the point (x = 1, y = 3.7) is represented in Mathematica as

   {1, 3.7}

Suppose we have a list of three (x, y) points: (1, 2), (2, 3), and (3, 5). These points are represented as lists of two numbers. The list of the three points is then a nested list, or a list of lists:

   { {1, 2}, {2, 3}, {3, 5} }
To plot a graph of these three points, we use a new Mathematica command:
   ListPlot[ { {1, 2}, {2, 3}, {3, 5} } ]
This tells Mathematica to plot the list of points inside the square brackets. The spaces are optional and are included here mainly for the sake of clarity.

To plot ten points representing the first ten prime numbers, we would therefore type
   ListPlot[ { {1, 2}, {2, 3}, {3, 5}, {4, 7},
               {5, 11}, {6, 13}, {7, 17}, {8, 19},
               {9, 23}, {10, 27} } ]
Again, the extra space is optional. If you type the input as it is shown here, however, you'll notice that your input cell grows large enough to accomodate all three lines of the command. If you forget a curly brace (or add an extra one), Mathematica will inform you of this. One of the points may be hard to see; look closely at the origin of the x and y axes to convince yourself that there is a point there.

Now let's join the points with straight line segments. Type

   ListPlot[ { {1, 2}, {2, 3}, {3, 5}, {4, 7},
               {5, 11}, {6, 13}, {7, 17}, {8, 19},
               {9, 23}, {10, 27} }, PlotJoined -> True ]
The modifier
   PlotJoined -> True
tells Mathematica to connect the points with lines.

Here's another shortcut that can save you a lot of typing. Suppose we wanted to store the list of the first ten primes in memory, so that we don't have to type it in every time we want to use it. Enter

   primes = { {1, 2}, {2, 3}, {3, 5}, {4, 7},
              {5, 11}, {6, 13}, {7, 17}, {8, 19},
              {9, 23}, {10, 27} }
Mathematica will respond with
   { {1, 2}, {2, 3}, {3, 5}, {4, 7},
     {5, 11}, {6, 13}, {7, 17}, {8, 19},
     {9, 23}, {10, 27} }
This list is now stored in the variable "primes". We can use this variable in a ListPlot command as follows:
   ListPlot[primes, PlotJoined -> True]
It's good stylistic practice to give your own variables names that begin with lowercase letters. This prevents us from trying to use the name of a Mathematica command or function as a variable name. Mathematica commands are all case specific, which means that uppercase and lowercase letters are distinct.

If you just want to see what is stored in the variable primes, type

   primes
and Mathematica will respond with
   { {1, 2}, {2, 3}, {3, 5}, {4, 7},
     {5, 11}, {6, 13}, {7, 17}, {8, 19},
     {9, 23}, {10, 27} }


Other parts of the Mathematica tutorial: