Introduction to Plotting

Math 245 - Mathematical Models
February 8, 2010

Plotting Functions

The basic Plot command

When you want to plot a function defined on some interval, you will use the Plot command.  This command has the same type of structure as the Table command:

Plot[x^2,{x,0,5}]

You give the function you want to graph, and then you need to tell Mathematica what variable it is that you are changing, along with its range.  Unlike the Table command the list must always be comprised of three entries, the variable, the lower bound of the range, and the upper bound of the range. 
	[Again, notice that the variable is automatically colored green in the list and in the function.]
You will notice that Mathematica always sets the range of the function automatically.  Compare the following two graphs.

Plot[x^2,{x,-5,5}]

Plot[x^2+100,{x,-5,5}]

Notice that the functions are vertical translates of one another, but unless you look at the range on the axes you would never know that they are not the same function.  
If you want to plot two or more functions on the same axes, simply replace the one function in the input by a list of functions.

Plot[{Sin[x],Cos[x]},{x,-2Pi,2Pi}]

Plotting Data

The basic ListPlot command

When you want to plot a list of x-y coordinates, you will use the ListPlot command.  
It is often a good idea to give the list of points you want to plot a descriptive name first (here I define the variable points).

points={{-1,3},{2,6},{3,10}};

ListPlot[points]

If you'd like to see what happens when you connect the points, replace the ListPlot command with the ListLinePlot command.

ListLinePlot[points]

Sometimes you have lists of the x- and y-coordinates and you would like to merge them.  This is a good time to use the Table command.  Here we specify the x-coordinates with the list x and the y-coordinates with the list y:

x={1,2,3,4,5};
y={3,6,10,18,12};
points2=Table[{x[[i]],y[[i]]},{i,1,Length[x]}]

ListLinePlot[points2]

Just as before, you can display two or more list plots on the same set of axes by placing these lists of points in a list.  Here we plot the values of x^2, x, and (Sqrt[x]) as x ranges from 0 to 1. Notice how breaking this command into pieces makes it more legible.

points3=Table[{x,x^2},{x,0,1,.1}]
points4=Table[{x,x},{x,0,1,.1}]
points5=Table[{x,Sqrt[x]},{x,0,1,.1}]
ListPlot[{points3,points4,points5}]

Combining Plots (and graphics in general)

Each plotting command outputs a graphics object.  When you wish to show two graphics objects at the same time, use the command Show.  
This is the only way to display a plot of a function and a list plot at the same time.

p1=ListPlot[{{1,2},{2,3},{4,6},{8,12}}];
p2=Plot[x,{x,0,10}];
p3=Show[p1,p2]

Plot and ListPlot Options

When you want to change the default look of a graph created by using Plot or ListPlot, you will need to specify certain options.  The ones used most often are  AxesOrigin, PlotRange, AxesLabel, PlotLabel, and PlotStyle.  To set an option, you will use the arrow operator (->), which you insert by typing first minus (-) then greater than (>).

Specifying the Range of the graph

The AxesOrigin option makes sure that your specified origin for the axes is in the graph.  In particular, if you would like the origin (0,0) to be in the graph, set the AxesOrigin option to be {0,0}.

Plot[x^2+100,{x,-5,5},AxesOrigin->{0,0}]

For more control over the range of a graph, use the PlotRange option; you will specify the lower bound and upper bound of the range:

Plot[{Sin[x],Cos[x]},{x,-2Pi,2Pi},PlotRange->{0,1.5}]

Giving the graph labels

If you would like to specify labels for the axes, use AxesLabel, and to give the plot a title, use PlotLabel.  Any text you would like to include should be inserted between matching pairs of double quotes (").

ListPlot[{points3,points4,points5},AxesLabel->{"x axis","y axis"}]

Plot[{Sin[x],Cos[x]},{x,-2Pi,2Pi},PlotLabel->"Graphs of Sin[x] and Cos[x]"]

Specifying the format of the graph

If you want to change color, thickness, and line options to personalize your graph, use the PlotStyle option.  There are many many different possiblities open to you; these examples are taken from the Documentation Center.

threePlots=Table[Table[Sin[k x],{x,0,2Pi,0.1}],{k,{1,2,3}}];
ListPlot[threePlots,PlotStyle->{Red,Green,Blue}]

ListPlot[{4,2,1,2,4},PlotStyle->{Orange,PointSize[Large]}]

Plot[Sin[x],{x,0,2Pi},PlotStyle->{Thick,Dashed}]

Using multiple options.

There is no restriction to the number of options that you can use in a plot at one time.

Plot[{Sqrt[x],x,x^2},{x,0,1},AxesOrigin->{0,0},PlotStyle->{{Thick,Red},{Dashed,Gray},{Thick, Green}},AxesLabel->{"x axis","y axis"},PlotLabel->"Graphs of Sqrt[x], x, and x^2"]

Comprehension Questions:

1. How do you plot three functions on one set of axes?  For example, plot the sine function, the tangent function, and the secant function on the interval [-3Pi,3Pi].

2. Now modify the graph from part 1 to restrict the range to be from 0 to 6, and make the sine function red, the tangent function orange, and the secant function green.

3. Here is a set of data: when x equals 10, 15, 20, 25, 30, and 35, the values of f(x) are 90, 84, 50, 36, 20, and -5.  Plot these points on a graph.  Now determine a line that appears to fit this data, and plot this line.  Lastly, combine these two graphics objects in one to see how well your line fits the data.

4. Explore the Documentation Center files for the Plot and ListPlot commands.  Copy some interesting examples here and modify some of the options to your liking.

5.  Advanced: Mathematica can import a large amount of data from its databases.  For instance, you can import country data like land area or yearly information about population, GDP, etc.  Here is a plot of the GDP for France for the years 1980 through 2005.  Investigate what else you can learn from Mathematica.

ListPlot[Table[CountryData["France",{{"GDP"},i}],{i,1980,2005,1}]]