Simple Plot in Python
This section will begin a very gentle introduction to Python and Matplotlib. The goal is to show how one can draw the plot of a function using Python and the tools from library provided by Matplotlib.
A complete description of the tools provided by Matplotlib is available at the following URL.
http://matplotlib.sourceforge.net/
After trying out the purposely short tutorial on this page, you might have developed enough curiosity to pursue further the combination of Python and Matplotlib. There is a very nice tutorial on using Matplotlib at the following URL.
http://matplotlib.sourceforge.net/tutorial.html
The tutorial is easy to follow and it gives you a nice overview of the capabilities of Python and Matplotlib.
Plotting a Sinusoid
In this first activity, let's try to draw the graph of a sinusoid with the following equation.
y = 2 sin (2π x - π /2)
In a trigonometry class, the first step would be to identify the amplitude, period, and phase shift. To that effort, we factor out a 2π.
y = 2 sin 2π (x - 1/4)
Comparing this equation with the more general form, y = A sin B (x - φ), we see that A = 2, B = 2π, and φ = 1/4. Thus, we have the following facts:
- The amplitude of the sinusoid is |A| = |2| = 2.
- The phase shift of the sinusoid is φ = 1/4.
- The period of the sinusoid is T = 2π /B = 2π /2π = 1.
Thus, when we draw the graph of the sinusoid, we should see it complete one full cycle every 1 second, it should bounce up and down between 2 and -2, and it should be shifted 1/4 units to the right. With these thoughts in mind, let's sketch 2 periods of the sinusoid, which means that we should sketch the graph of the sinusoid over the domain [0, 2].
Coding
We begin. Open the python interpreter by opening a terminal window and typing the following command at the prompt:
>> python
The MathDept $ is the system prompt on our system, which might be quite different on your system. Following this prompt, you need only type the word python, followed by the Enter or Return key on your system. Assuming Python is installed and configured, the Python interpreter will start up with a message similar to what follows.
MathDept $ python Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) [GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
The three >>> is the Python prompt. Python is now waiting for you to enter an instruction. Our first instruction will load everything contained in the Matplotlib library.
>>> from pylab import *
Recall that our plan is to plot the sinusoid y = 2 sin 2π (x - 1/4) over the domain [0,2]. The next step is to create a vector with a lot of points from this interval.
>>> x=arange(0,2,0.01)
This form of the arange command has the syntax arange(start, finish, increment) which creates a vector of values, starting at the number start, finishing at the number finish, and incrementing by the number increment. Consequently, the command x=arange(0,2,0.01) builds a vector of points, starting at 0, finishing at 2, and incrementing by 0.01, then stores the resulting vector in the variable x.
We now need to evaluate the sinusoid y = 2 sin 2π (x - 1/4) at each point of the vector x. This is a simple matter in Python.
>>> y=2*sin(2*pi*(x-1/4))
The above command evaluates the sinusoid at each point in the vector x and stores the result in the vector y. All that remains is to plot the points in the vector y versus the points in the vector x.
>>> plot(x,y)
We need to execute one more command to "show" the plot of the sinusoid.
>>> show()
The result of this last command on our system is shown in Figure 1. Results may differ on different systems depending on the windowing system used by your version of Matplotlib.

Figure 1. A plot of y = 2 sin 2π (x - 1/4) on the interval [0, 2].
You can add axis labels and a title with the following commands. You may have to close the figure window before you can enter the following commands.
>>> plot(x,y)
>>> ylabel('y-axis')
>>> xlabel('x-axis')
>>> title(r'$y=2\sin 2\pi(x-1/4)$')
>>> show()
The code produces the axis labels and title shown in Figure 2. Note that Matplotlib understands a subset of the TeX typesetting language, which is an excellent way to include mathematical text with your plots.

Figure 2. Adding axis labels and a title to the figure..
Script Files
One can quickly grow weary of typing many consecutive commands at the Python prompt, particularly if one is prone to making typing mistakes, with the accompanying need for repeated typing of code lines. Fear not! Python offers a number of nice solutions, the first of which involves gathering all of the lines of code needed to produce the plot in a script file.
Open any text editor on your system. Vi, Emacs, and BBEdit are good choices on the Mac, as are Textedit and TextWrangler. On Windows, Notepad is a good choice. You can even use Microsoft Word, just as long as you save your file in text format. So, open your favorite editor and enter the following lines:
from pylab import *
x=arange(0,2,0.01)
y=2*sin(2*pi*(x-1/4))
plot(x,y)
xlabel('x-axis')
ylabel('y-axis')
title(r'$y=2\sin (2\pi(x-1/4))$')
show()
Save this file as simple.py.
As you can clearly see, we've gathered all the previous commands used to produce the plot in Figure 2 into one file, then saved the file as simple.py. No surprises there. We can now execute the commands in the script file with the following command:
MathDept $ python simple.py
This command should produce the same image as that shown in Figure 2 above.
Alternatively, we can add the following line at the beginning of our script file:
#! /usr/local/bin/python
from pylab import *
x=arange(0,2,0.01)
y=2*sin(2*pi*(x-1/4))
plot(x,y)
xlabel('x-axis')
ylabel('y-axis')
title(r'$y=2\sin (2\pi(x-1/4))$')
show()
The first line of the file is now:
#! /usr/local/bin/python
The character combination #! must be the very first two characters in the file. These characters are immediately followed by the location of the command used to start up python. On our system, the location of the Python interpreter is /usr/local/bin/python/. You can use the command which python to determine the location of Python on your system.
The next step is to make the script file an executable file, a simple matter on unix-type systems like the Mac.
MathDept $ chmod +x simple.py
We can now execute the file by simply typing its name.
MathDept $ ./simple.py
The command ./simple.py tells the system to locate the file simple.py in the current directory and execute it. Because the first line of the file contains a path to the Python interpreter, Python is used to execute the script contained in the file. If all goes well, you should again get a figure identical to that in Figure 2.
You'll quickly discover that script files are one of the most efficient ways to code in Python. Simple mistakes are quickly corrected and the file can be executed again. No need to retype lines of code.
Time to Strike Out on Your Own
We hope you've enjoyed this simple tutorial introduction to plotting using Python and Matplotlib. You should now take some time to investigate further by trying out the tutorial at:
http://matplotlib.sourceforge.net/tutorial.html
Once you've completed this simple tutorial, try plotting some graphs of your own choosing.
