author | amit@thunder |
Fri, 02 Apr 2010 19:30:17 +0530 | |
changeset 13 | 847f9aefa7d4 |
parent 11 | eafc653206d8 |
child 16 | cdcad776b360 |
permissions | -rw-r--r-- |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
1 |
* Script |
5 | 2 |
********** |
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
3 |
Some greeting-- Hi or Hello or Welcome - would be polite to start with |
5 | 4 |
********** |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
5 |
|
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
6 |
Hello and welcome to the first tutorial in a series of tutorials on Python for Scientific Computing . |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
7 |
In this tutorial, we will cover the basics of the Plotting features available in Python. |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
8 |
For this we shall use Ipython and pylab. |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
9 |
Ipython is An Enhanced Interactive Python interpreter. It provides additional features like tab completion,easier access to help , and many other useful features. |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
10 |
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
|
11 |
|
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
12 |
I am assuming that you have both Ipython and Pylab installed on your system . |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
13 |
|
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
14 |
On your terminal type in the command Ipython -pylab |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
15 |
$ ipython -pylab |
4
4dee50d4804b
Added changes to array.org file and basic plotting.
Shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
16 |
press RETURN |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
17 |
|
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
18 |
We will first start with the absolute basic i.e how to print hello world |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
19 |
|
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
20 |
In []: print 'hello world' |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
21 |
|
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
22 |
Now to exit ipython type Ctrl-D . It will ask if you wish to exit type y to exit |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
23 |
|
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
24 |
Now we will get back to plotting . |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
25 |
|
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
26 |
type : |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
27 |
$ ipython -pylab |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
28 |
press RETURN |
5 | 29 |
|
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
30 |
First, we will create a sequence of equally spaced points starting from 0 to 2*pi |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
31 |
|
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
32 |
For this type: |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
33 |
|
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
34 |
In []: x = linspace(0, 2*pi, 50) |
5 | 35 |
|
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
36 |
You can also do : |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
37 |
In []: x = lins<Tab> This is an Ipython feature that will auto-suggest the word |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
38 |
|
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
39 |
|
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
40 |
|
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
41 |
|
9
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
42 |
To know more about the 'linspace' function type |
5 | 43 |
|
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
44 |
In []: lins<Tab>pace linspace? |
5 | 45 |
|
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
46 |
It shows documentation related to linspace function. 'help' talks in detail about arguments to be passed, return values, some examples on usage. |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
47 |
|
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
48 |
as you can see linspace can take three parameters start, stop, and num and returns num evenly space points . You can scroll through the help to know more about the function |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
49 |
|
5 | 50 |
|
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
51 |
|
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
52 |
You can Check value of x by |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
53 |
In []: x |
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
54 |
x is a sequence of 50 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
|
55 |
In []: len(x) |
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
56 |
which shows it to be 50 points. |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
57 |
|
5 | 58 |
To obtain the plot we say, |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
59 |
In []: plot(x, sin(x)) |
5 | 60 |
*** |
9
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
61 |
As you can see a plot has appeared on the screen. |
5 | 62 |
*** |
9
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
63 |
The plot has the default color and line properties. |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
64 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
65 |
Both 'pi' and 'sin' come from 'pylab'. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
66 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
67 |
Now that we have a basic plot, we can label and title the plot. |
5 | 68 |
In []: xla<TAB>bel('x') will add a label to the x-axis. Note that 'x' is enclosed in quotes. |
69 |
Similarly |
|
70 |
In []: ylabel('sin(x)') adds a label to the y-axis. |
|
71 |
To add a title to plot we simply use |
|
72 |
In []: tit<TAB>le('Sinusoid'). |
|
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
73 |
|
5 | 74 |
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
|
75 |
In []: legend(['sin(x)']) |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
76 |
|
5 | 77 |
To go to previous command, we can use 'UP Arrow key' and 'DOWN' will take us (in reverse order)/back. |
78 |
We can modify previous command to specify the location of the legend, by passing an additional argument to the function. |
|
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
79 |
In []: legend(['sin(x)'], loc = 'center') |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
80 |
|
5 | 81 |
other positions which can be tried are |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
82 |
'best' |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
83 |
'right' |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
84 |
|
9
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
85 |
We now annotate, i.e add a comment, at a specific position in the graph. In this case, let's add a comment at the point with maximum sin value. |
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
86 |
In []: annotate('origin', xy=(0, 0)) |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
87 |
|
9
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
88 |
The first argument is the comment string and second one is the position for it. |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
89 |
|
5 | 90 |
Now, we save the plot as follows |
9
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
91 |
In []: savefig('sin.png') saves the figure with the name 'sin.png' in the current directory. |
5 | 92 |
|
9
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
93 |
#other supported formats are: eps, ps, pdf etc. |
5 | 94 |
|
95 |
When we use plot again by default plots get overlaid. |
|
96 |
In []: plot(x, cos(x)) |
|
97 |
||
98 |
we update Y axis label |
|
99 |
In []: ylabel('f(x)') |
|
100 |
||
9
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
101 |
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. |
5 | 102 |
|
9
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
103 |
In []: legend( [ 'sin(x)' , 'cos(x)'] ) |
5 | 104 |
|
105 |
In []: clf() |
|
9
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
106 |
clears the plot area and starts afresh. |
5 | 107 |
|
108 |
In case we want to create multiple plots rather than overlaid plots, we use 'figure' function. |
|
109 |
The figure command is used to open a plain figure window without any plot. |
|
110 |
In []: figure(1) |
|
111 |
||
112 |
plot() plot command plots a sin plot on figure(1) |
|
9
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
113 |
In []: plot(x, sin(x)) |
5 | 114 |
|
115 |
to creates a new plain figure window without any plot. |
|
116 |
In []: figure(2) |
|
9
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
117 |
figure() is also used to shift the focus between multiple windows. |
5 | 118 |
|
119 |
Any command issued henceforth applies to this window only. |
|
120 |
In []: plot(x, cos(x)) |
|
121 |
The previous plot window remains unchanged to these commands. |
|
122 |
||
123 |
In []: savefig('cosine.png') |
|
124 |
||
125 |
figure(1) shifts the focus back to figure(1). |
|
126 |
In []: figure(1) |
|
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
127 |
|
5 | 128 |
title() sets the title of figure(1) |
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
129 |
In []: title('sin(x)') |
5 | 130 |
|
131 |
Here we save the plot of figure(1). |
|
132 |
In []: savefig('sine.png') |
|
133 |
||
134 |
close() closes figure(1). Now there is just one figure that is open and hence |
|
135 |
the focus is automatically shifted to figure(2). |
|
136 |
In []: close() |
|
137 |
||
138 |
close() now closes the figure(2). |
|
139 |
In []: close() |
|
140 |
||
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
141 |
The plot command takes the following optional parameters such as 'g' which generates the plot in green color. |
5 | 142 |
|
143 |
Passing the linewidth=2 option to plot, generates the plot with linewidth of two units. |
|
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
144 |
Use Up arrow to get to the previous commands |
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
145 |
In []: plot(x, sin(x), 'g', linewidth=2) and add arguments |
5 | 146 |
|
147 |
In []: clf() |
|
148 |
||
9
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
149 |
In order to plot points you may pass '.' as a parameter to plot |
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
150 |
In []: plot(x, sin(x), '.') |
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
151 |
|
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
152 |
In []: clf() |
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
153 |
|
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
154 |
In order to plot filled circles in black color you can pass 'ko' parameter to plot |
538f59bb598c
Minor edits to basic-plot.txt.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
155 |
In []: plot(x, cos(x), 'ko') |
5 | 156 |
|
157 |
In []: clf() |
|
158 |
||
159 |
A plot using dashed lines can be generated by passing the '--' parameter |
|
13
847f9aefa7d4
Changes in the end some problem with continuity of commands . Fixed it .
amit@thunder
parents:
11
diff
changeset
|
160 |
In []: plot(x, sin(x), '--') |
5 | 161 |
|
162 |
You may look at more options related to colors and type of lines using plot? |
|
163 |
||
164 |
In []: clf() |
|
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
165 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
166 |
and finally to close the plot |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
167 |
In []: close() |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
168 |
|
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
169 |
|
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
170 |
|
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
171 |
|
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
172 |
**************** |
11
eafc653206d8
Made changes to incorporate suggestions for adding info about ipython and pointers from spoken tutorials workshop
amit@thunder
parents:
9
diff
changeset
|
173 |
This brings us to the end of this tutorial. Thank you for attending this first tutorial on Python for Scientific Computing |
5 | 174 |
**************** |