basic-plot.txt
author shantanu <shantanu@fossee.in>
Tue, 30 Mar 2010 19:11:39 +0530
changeset 5 76fe6a48386f
parent 4 4dee50d4804b
child 9 538f59bb598c
permissions -rw-r--r--
Restored basic-plotting file.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
     1
* Script
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
     2
**********
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
     3
Some greeting - Hi or Hello or Welcome - would be polite to start with
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
     4
**********
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
     5
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
     6
Hello, in this tutorial, we will cover the basics of the Plotting features available in Python. We shall use Ipython and pylab. Ipython is An Enhanced Interactive Python interpreter. It provides additional features like tab completion, help etc. pylab is python library which provides plotting functionality. 
4
4dee50d4804b Added changes to array.org file and basic plotting.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
     7
4dee50d4804b Added changes to array.org file and basic plotting.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
     8
I am assuming that you have them installed on your system.
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
     9
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    10
Lets start IPython. Click Applications - Accessories - Terminal.  The terminal window will open. Type the following command. 
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    11
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    12
$ ipython -pylab
4
4dee50d4804b Added changes to array.org file and basic plotting.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    13
press RETURN
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    14
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    15
This will give us a prompt where we can get started. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    16
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    17
First, we create a sequence of numbers which are equally spaced starting from 0 till/to(?) 2*pi
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    18
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    19
In []: x = lins<Tab> will auto complete the function. This is one of the feature of IPython.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    20
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    21
In []: x = linspace(0, 2*pi, 100)
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    22
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    23
To check or read documentation on 'linspace' function type
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    24
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    25
In []: lins<Tab>pace?
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    26
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    27
It shows documentation related to linspace function. 'help' talks in detail about arguments to be passed, return values, some examples on usage. (To scroll down the page use 'SPACE' key and to scroll up use 'b')To navigate through content use arrow(/Page Up and Page Down) keys. ':See Also' section hints about other related or similar functions which might be useful. To exit help (mode) press 'q'.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    28
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    29
In our case, we have passed three arguments to the linspace function - the starting point, the last point and the total number of points. 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    30
Check value of x by
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    31
In []: x
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    32
 x is a sequence of 100 points starting from 0 to 2*pi. Length of x can be seen via function
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    33
In []: len(x)
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    34
which shows the length of x to be 100 points.
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    35
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    36
To obtain the plot we say,
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    37
In []: plot(x, sin(x))
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    38
***
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    39
As you can see a plot has come on the screen. 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    40
***
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    41
A plot of x vs sin(x) appears on screen, with the default color and line properties. 
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    42
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    43
Both 'pi' and 'sin' come from 'pylab'. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    44
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    45
Now that we have a basic plot, we can label and title the plot. 
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    46
In []: xla<TAB>bel('x') will add a label to the x-axis. Note that 'x' is enclosed in quotes. 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    47
Similarly
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    48
In []: ylabel('sin(x)') adds a label to the y-axis.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    49
To add a title to plot we simply use 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    50
In []: tit<TAB>le('Sinusoid').
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    51
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    52
Now we will add a legend to the plot. 
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    53
In []: legend(['sin(x)'])
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    54
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    55
To go to previous command, we can use 'UP Arrow key' and 'DOWN' will take us (in reverse order)/back.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    56
We can modify previous command to specify the location of the legend, by passing an additional argument to the function. 
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    57
In []: legend(['sin(2y)'], loc = 'center')
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    58
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    59
other positions which can be tried are
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    60
'best' 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    61
'right'
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    62
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    63
We now annotate, i.e add a comment, at the point with maximum sin value. 
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    64
In []: annotate('local max', xy=(1.5, 1))
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    65
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    66
The first argument is the comment and second one is the position for it. 
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
    67
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    68
Now, we save the plot as follows
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    69
In []: savefig('sin.png') saves the figure as sin.png in the current directory. 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    70
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    71
?#other supported formats are: eps, ps, pdf etc.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    72
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    73
When we use plot again by default plots get overlaid.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    74
In []: plot(x, cos(x))
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    75
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    76
we update Y axis label 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    77
In []: ylabel('f(x)')
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    78
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    79
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.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    80
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    81
In []: legend( [ 'sin(y)' , 'cos(y)'] )
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    82
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    83
In []: clf()
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    84
clears the plot area and start afresh.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    85
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    86
In case we want to create multiple plots rather than overlaid plots, we use 'figure' function.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    87
The figure command is used to open a plain figure window without any plot.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    88
In []: figure(1)
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    89
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    90
plot() plot command plots a sin plot on figure(1)
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    91
In []: plot(y, sin(y))
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    92
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    93
to creates a new plain figure window without any plot. 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    94
In []: figure(2)
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    95
figure() also shifts the focus between multiple windows. 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    96
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    97
Any command issued henceforth applies to this window only.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    98
In []: plot(x, cos(x))
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
    99
The previous plot window remains unchanged to these commands.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   100
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   101
In []: savefig('cosine.png')
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   102
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   103
figure(1) shifts the focus back to figure(1).
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   104
In []: figure(1)
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
   105
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   106
title() sets the title of figure(1) 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   107
In []: title('sin(y)')
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   108
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   109
Here we save the plot of figure(1). 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   110
In []: savefig('sine.png')
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   111
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   112
close() closes figure(1). Now there is just one figure that is open and hence 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   113
the focus is automatically shifted to figure(2).
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   114
In []: close()
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   115
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   116
close() now closes the figure(2).
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   117
In []: close()
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   118
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   119
The plot command takes the following optional parameters such as 'r' which generates the plot in red color. 
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   120
Use up arrow key to get till this command
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   121
In []: plot(x, cos(x), 'r') and add argument.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   122
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   123
# For other color options you may check out 'plot?'
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   124
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   125
In []: clf()
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   126
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   127
Passing the linewidth=2 option to plot, generates the plot with linewidth of two units.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   128
In []: plot(x, sin(x), 'g', linewidth=2)
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   129
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   130
In []: clf()
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   131
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   132
In order to plot points in black color you can pass 'k.' parameter to plot
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   133
In []: plot(x, , 'k.')
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   134
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   135
In []: clf()
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   136
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   137
A plot using dashed lines can be generated by passing the '--' parameter
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   138
In []: plot(x, y, '--')
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   139
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   140
You may look at more options related to colors and type of lines using plot?
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   141
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   142
In []: clf()
0
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
   143
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
   144
and finally to close the plot
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
   145
In []: close()
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
   146
67604aed10e0 Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff changeset
   147
****************
5
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   148
This brings us to the end of this tutorial.  This tutorial is first in the series of Python for Scientific Computing Tutorials.
76fe6a48386f Restored basic-plotting file.
shantanu <shantanu@fossee.in>
parents: 4
diff changeset
   149
****************