* Script

*Hello and welcome to the tutorial on Basic Plotting using Python. 

*The intended audience for this tutorial are Engineering, mathematics and science teachers and students

*In this tutorial, we will cover the basics of the Plotting features available in Python. 
For this we shall use  Ipython and pylab. 
Ipython is An Enhanced Interactive Python interpreter. It provides additional features like tab completion,easier access to help , and many other useful features which are not present in the vanilla Python interpreter.
Pylab is python library which provides plotting functionality. 

I am assuming that both Ipython and Pylab are installed on your system. If not please refer to our tutorial on how to install IPython and pylab. 

*On your terminal type in the command
$ ipython -pylab
press RETURN

We will first start with the customary Hello world program by writing:

In []: print 'hello world'

Voila we have got hello world as the output 

To exit ipython press Ctrl-D.

*Now we will get back to plotting.

type again :
$ ipython -pylab
press RETURN

In order to plot, we need a set of points. Let us create a sequence of equally spaced points starting from 0 to 2*pi. For this we use linspace

Type:
In []: x = lins<Tab> This is an Ipython feature that will auto-suggest the word

In []  x=linspace( RETURN
Ouch! I made a mistake . As you can see I made the mistake of not writing command correctly
and Ipython changed the prompt . To get the old prompt back press crtl-c

In []: x = linspace(0, 2*pi, 100)

*To know more about any function,  in this case say, for 'linspace' you can type ? after it .

In []: linspace?

It shows documentation related to linspace function. 'help' gives details about arguments to be passed, return values and also some examples on usage. You can scroll the help using up, and down arrows keys.
To exit from the help menu type 'q'.

*As you can see linspace can take three parameters the starting point, the ending point and the number of points.

Let us see what x contains now. Type 'x' and hit enter.

We see that x is a sequence of numbers from 0 to 2*pi. We can check the length of x by typing 

In []: len(x)

To obtain the plot we use,
In []: plot(x, sin(x))
***
As you can see a plot has appeared on the screen. 
***

The plot has the default color and line properties. 

In this case we have used two commands 
'pi' and 'sin' these come from 'pylab' library called using -pylab. 

*Now that we have a basic plot, we can label and title the plot. 
In []: xla<TAB>bel('x') will add a label to the x-axis. Note that 'x' is enclosed in quotes. 
Similarly
In []: ylabel('sin(x)') adds a label to the y-axis.
To add a title to plot we simply use 
In []: tit<TAB>le('Sinusoid').

Hmm we also got the axis's nicely labeled and the plot titled but there is still a important detail left.That might leave a teacher unsatisfied, it lacks a legend. 

Add a legend to the plot by typing 
In []: legend(['sin(x)'])

We can modify previous command to specify the location of the legend, by passing an additional argument to the function. 

To go to previous command, we can use 'UP Arrow key' and 'DOWN' will take us back.

Once you start editing a previous command and then you try to use 'Up arrow key ' you can get commands that are only similar to the command you are editing. But if you move your cursor to the beginning of the line you can get all the previous commands using up and down arrow keys.
In []: legend(['sin(x)'], loc = 'center')

Note that once 
other positions which can be tried are
'best' 
'right'

Very often in mathematical plots we have to define certain points and their meaning also called annotating . We next look at how to annotate
In this case, let's add a comment at the point of origin. 
In []: annotate('local max', xy=(1.5, 1))

The first argument is the comment string and second one is the position for it. 

As you can see, the boundary along the x-axis extends after the graph and there is an ugly blank space left on the right. Also along the y-axis, the sine plot in fact is cut by the boundary. We want to make the graph fit better. For this we shall use xlim() and ylim() to set the boundaries on the figure.

In []: xlim(0, 2*pi)

In []: ylim(-1.2, 1.2)

The first value passed is the lower limit and the second is the upper limit. Hence when we do xlim(0, 2*pi) the boundary is set from x-value 0 to x-value 2*pi. Similarly for the y-axis.

Ok, what do I do with all this effort . I obviously have to save it . 

We save the plot by the function savefig
In []: savefig('sin.png') saves the figure with the name 'sin.png' in the current directory. 

other supported formats are: eps, ps, pdf etc.

Let's see what happens when we use plot again 
In []: plot(x, cos(x)) by default plots get overlaid.

we update Y axis label 
In []: ylabel('f(x)')

Now in these situations with overlaid graphs, legend becomes absolutely essential. To add multiple legends, we pass the strings within quotes separated by commas and enclosed within square brackets as shown.

In []: legend( [ 'sin(x)' , 'cos(x)'] )

Please note that the previous legend is overwritten.

In []: clf()
clears the plot area and starts afresh.

# Close the figure manually.

*In case we want to create multiple plots rather than overlaid plots, we use 'figure' function.
The figure command is used to open a plain figure window without any plot.
In []: figure(1)

We will use plot() plot again to plot a sin curve on figure(1)
In []: plot(x, sin(x))

to creates a new plain figure window without any plot type: 
In []: figure(2)
figure() is also used to shift the focus between multiple windows. 

Any command issued henceforth applies to this window only so typing
In []: plot(x, cos(x))
plots cos curve on second window now.
The previous plot window remains unchanged to these commands.

calling function figure using argument 1 shifts the focus back to figure(1).
In []: figure(1)

title() sets the title of figure(1) 
In []: title('sin(x)')

Now we save the plot of figure(1) by typing 
In []: savefig('sine.png')

close() closes figure(1). Now there is just one figure that is open and hence 
the focus is automatically shifted to figure(2).
In []: close()

close() now closes the figure(2).
In []: close()

Now, what if we want to plot in a different color? The plot command can take the additional parameters such as 'g' which generates the plot in green color. 

What if we want a thicker line? Passing the linewidth=2 option to plot, generates the plot with linewidth of two units.

In []: plot(x, sin(x), 'g', linewidth=2) and add arguments

In []: clf()

If we want to  plot points we may pass '.' as a parameter to plot
In []: plot(x, sin(x), '.')

In []: clf()

You may look at more options related to colors and type of lines using plot?(question mark)
There are numerous options and various combinations available.
quit the documentation using 'q'

In []: clf()

and finally to close the plot
In []: close()

In this tutorial we learned 

Some IPython Features
Starting and exiting.
autocompletion.
help functionality.

Regarding plotting we covered:
How to create basic plots.
Adding labels, legends annotation etc.
How to change looks of the plot like colors, linewidth formats etc

****************
This brings us to the end of this tutorial. This is the first tutorial in a series of tutorials on Python for Scientific Computing. This tutorial is created by the FOSSEE team, IIT Bombay. Hope you enjoyed it and found it useful.
****************

************
A slide of review of what has been covered and sequentially/rapidly going through them.
************

Various problems of Ipython, navigation and enter, exit.
What about xlim and ylim?
