Hello and welcome to the tutorial on Basic Plotting using Python.
The intended audience for this tutorial are students and teachers from Engineering, mathematics and science background.
In this tutorial, we shall cover the basic Plotting features available in Python. For this we shall use Ipython and pylab. Ipython is An Enhanced Interactive Python interpreter. It provides additional features such as tab completion, easier access to help and many other features which are not present in the vanilla Python interpreter. Pylab is the 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. Now lets get into action.
On your terminal type :
$ ipython -pylab
and hit enter. This commands starts the Ipython interpreter.
We shall first start with the customary Hello world program by typing:
print 'hello world!'
and thus we have the first python program.
To exit ipython press Ctrl-D and type y at the prompt and hit enter.
Now that we want to get back to plotting, lets start ipython again
by typing :
$ ipython -pylab
In order to plot, we need a set of points so we shall first create a set of equally spaced points starting from 0 upto 2*pi. For this we use linspace
x = lins<Tab>
As you can see Ipython completes the word for us. This feature is called auto-completion. Let us continue with the plot.
Ouch! I made a mistake . As you can see I wanted to type in the arguments and then close the parenthesis before I hit enter but I accidently hit the enter key and Ipython gave me a different prompt . To get back to the previous prompt press crtl-c. Now type:
x = linspace(0, 2*pi, 100)
To know more about any function, type the function name followed by ? mark and hit enter. It shows documentation related to that function.
In this case the 'linspace' function. It gives details such as arguments to be passed, return values and also example on usage. You can scroll through the help using the up, and down arrows keys. In order to quit the help and get back to the Ipython prompt press 'q'.
linspace function here takes three parameters the starting point, the ending point and the total number of points.
Let us see what x contains now. Type 'x' and hit enter.
As you can see x is a set of values starting from 0 and ending in 2*pi. Let us now check the length of x by typing
It shows that there are 100 points.
To obtain a plot we shall use,
As you can see a plot has appeared on the screen with a default color and line properties.
Here we have used two special terms 'pi' and 'sin', both of these come from 'pylab'.
Now that we have a basic plot, we shall label and title the plot.
labels the x-axis. Similarly for the y-axis we use
After naming the x and y-axis we shall title to figure using the 'title' command
This produces a title on the figure. Now that we have a title we shall give a legend to the plot as l using
As you can see the legend appears at the top right corner. What if I want to place the legend at a different location. For this first get to the previous command using the up arrow key and add another parameter by giving ',' after the square brackets and type
legend(['sin(x)'], loc = 'center')
This places the legend at the center.
The other positions available are
Very often in mathematical plots we need to name certain points and these are called annotations . We shall annotate a particular point in this plot. This can be done using the command 'annotate'.
annotate('origin', xy=(0, 0))
This shall annotate the point 0,0 with a string origin. The first argument is the comment string and second argument is the position for it.
As you can see, there is a blank space at the end of plot along the x-axis also y-axis grazes the plot. In order to make it look better ,I would want the x-axis to end at 2*pi and i would want the y-axis to include the little more of space. For this we use xlim() to define the boundaries of x-axis.
Now the limits of x-axis are set. Similarly
This sets the boundaries of the y-axis and the graph fits more precisely.
Now since we have done so much we would want to save the figure .
We save the figure using
This saves the figure in the current working directory under the name 'sin.png'.
The format of the figure is taken from the extension given in the name. The other supported formats are: eps, ps and pdf .
Let's see what happens when we use plot again
As you can see by default the plots get overlaid.
we can now update Y axis label with
In these kind of situations where plots are overlaid , legend becomes absolutely essential. To specify multiple plots and legends, we pass multiple names within quotes separated by commas and enclosed within square brackets as shown.
legend( ['sin(x)' , 'cos(x)'] )
Please note that the previous legend is overwritten.
Let us now see how to create multiple plots. Type:
This opens a figure window without any plot.
We will plot the sinx plot using
The plot appears on the figure. To create a new plain figure we now type:
Any command issued henceforth shall apply only to figure(2). Type:
As you can see the cos plot has appeared on figure(2) and figure(1) is unchanged.
figure is also used to shift focus between figures. Now calling figure(1) shifts the focus to figure(1).
titles the figure(1) appropriately.
Now we save the plot in figure(1) using
close() closes figure(1). Now that there is just one figure ,the focus is automatically shifted to figure(2).
now closes the figure(2).
if you observe the color of the plot and the width of the plot, they were set by Ipython itself and we did not choose them. What if we want a plot, a colour of our interest and we want to choose other attributes of plot as well. This is possible by passing additional attributes to the plot command.
plot(x, sin(x), 'g')
generates the plot in green colour.
Let us now clear the figure
is used to clear the figure and obtain a plain graph. If you want a thicker line we use
plot(x, sin(x), linewidth=2)
As you can see the line is a little thicker.
Let us now clear the figure using:
What if we want to plot points instead of lines. To achieve this we pass an additional parameter called '.' to plot.
plot(x, sin(x), '.')
You can read more attributes of plot by reading the documentation of plot by typing
hit enter. You can see the numerous options available and the various combinations possible.
Lets get back to the Ipython shell by pressing 'q'
Let us now close the figure
and exit the Ipython shell.
This brings us to the end of this tutorial. In this tutorial we have learned how to start and quit Ipython . Some IPython features such as autocompletion and help. We also learned how to create simple plots , overlaying plots and plotting in different figures. We learned how to add legends, labels and annotations and finally how to change the looks of the plot using colours and linewidth.
This brings us to the end of the first tutorial in the series Python for Scientific Computing. This tutorial was created by the FOSSEE team at IIT Bombay under the FOSSEE project that is funded by NME, through ICT from MHRD, Govt. of India . Hope you have enjoyed it and found it useful. Thank you.