author | Shantanu <shantanu@fossee.in> |
Wed, 14 Apr 2010 12:20:26 +0530 | |
changeset 54 | 46a3575919f5 |
parent 37 | c2634d874e33 |
permissions | -rw-r--r-- |
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
1 |
********************************************************************************** |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
2 |
Hello friends and welcome to the second tutorial in the series of spoken tutorials on Python for Scientific computing. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
3 |
|
35 | 4 |
In the previous tutorial we learnt how to obtain basic plots using a set of points. |
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
5 |
We plot experimental data more often that we plot mathematical curves. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
6 |
So here we shall learn how to plot experimental data. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
7 |
|
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
8 |
You can input the data either as a list or read from a plain text/binary file. We shall cover both one by one. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
9 |
Please make sure you have pendulum.txt file, as mentioned on requirement list of the session. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
10 |
As you can see the pendulum.txt file in our case is on the desktop and we are currently in the home directory. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
11 |
So we navigate to the desktop, using cd Desktop. Now let's start IPython by typing ipython -pylab. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
12 |
|
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
13 |
First we shall look into using lists to input the data and then we shall plot it. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
14 |
Type |
37
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
15 |
x = open square bracket 0, 1, 2.1, 3.1, 4.2, 5.2 close square bracket. |
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
16 |
here x is a list. In python, list is a container that holds a number of objects in the given order. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
17 |
We shall look into other functions related to lists a little later. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
18 |
|
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
19 |
Now for the corresponding Y values type |
37
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
20 |
y = open square bracket 0, 0.8, 0.9, 0, -0.9, -0.8 close square bracket. |
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
21 |
|
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
22 |
Now that we have x and y in two separate lists and we plot x vs. y using |
37
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
23 |
plot (x, y, 'o') The o within quotes plots with filled circles. And lo! We have our plot! |
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
24 |
|
37
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
25 |
|
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
26 |
[We close the plot window. ] |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
27 |
|
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
28 |
Now, that we know how to plot data from lists, we will look at plotting data from a text file. Essentially, if we read the data from the file and fit them into lists, we can easily plot the data, just as we did previously. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
29 |
|
37
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
30 |
Here we shall use the data collected from a simple pendulum experiment as an example. The aim of the experiment is to plot the length versus square of the time period. |
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
31 |
Let us check out what pendulum.txt contains. Type cat pendulum.txt |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
32 |
|
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
33 |
Windows users just double click on the file to open it. Please be careful not to edit the file. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
34 |
|
37
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
35 |
The first column is the length of the pendulum and the second column is the time period. We read the file line-by-line, collect the data into lists and plot them. |
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
36 |
|
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
37 |
Let's begin with initializing three empty lists for length, time-period and square of the time-period. |
36
57ed95acb13f
Made changes to second session script.
Shantanu <shantanu@fossee.in>
parents:
35
diff
changeset
|
38 |
l = [] |
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
39 |
t = [] |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
40 |
tsq = [] |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
41 |
|
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
42 |
Initializing an empty list is done as shown above using just a pair of square brackets without any content in them. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
43 |
|
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
44 |
Now we open the file and read it line by line. |
37
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
45 |
for line in open (within quotes the file name. )('pendulum.txt'): |
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
46 |
|
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
47 |
The ':' at the end of the 'for' statement marks the beginning of the for block. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
48 |
'open' returns an iterable object which we traverse using the 'for' loop. In python, 'for' iterates over items of a sequence. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
49 |
For more details regarding the for loop refer to our tutorial on loops and data structures. |
37
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
50 |
Whatever we read from a file is in the form of strings. Thus 'line' here is a string variable that contains one line of the file at a time as the 'for' loop iterates through the file. |
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
51 |
|
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
52 |
We split each line at the space using |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
53 |
point = line.split() |
37
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
54 |
the split function returns a list of elements from the 'line' variable split over spaces. In this case it will have two elements, first is length and second is time. point here contains 2 elements, the first one is the length and the second one is the time period |
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
55 |
|
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
56 |
Note the indentation here. Everything inside the 'for' loop has to be indented by 4 spaces. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
57 |
Then we append the length and time values to the appropriate lists. Since we cannot perform mathematical operations on strings, we need to convert the strings to floats, before appending to the lists. |
36
57ed95acb13f
Made changes to second session script.
Shantanu <shantanu@fossee.in>
parents:
35
diff
changeset
|
58 |
l.append(float(point[0])) |
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
59 |
append is a function used to append a single element to a list. |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
60 |
t.append(float(point[1])) |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
61 |
|
37
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
62 |
That's it, now we need to exit the loop. Hit the enter key twice. |
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
63 |
|
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
64 |
Now we have the time and length values in two lists. Now to get the square of the time values, we shall write one more 'for' loop which will iterate through list 't' |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
65 |
|
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
66 |
for time in t: |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
67 |
tsq.append(time*time) |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
68 |
|
36
57ed95acb13f
Made changes to second session script.
Shantanu <shantanu@fossee.in>
parents:
35
diff
changeset
|
69 |
Let us now verify if l, t and tsq have the same number of elements. Type |
57ed95acb13f
Made changes to second session script.
Shantanu <shantanu@fossee.in>
parents:
35
diff
changeset
|
70 |
print len(l), len(t), len(tsq) |
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
71 |
|
36
57ed95acb13f
Made changes to second session script.
Shantanu <shantanu@fossee.in>
parents:
35
diff
changeset
|
72 |
Now we have verified that all three have the same dimensions. lists l and tsq have the required data. Let's now plot them, as we did earlier. |
33
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
73 |
plot(l, tsq, 'o') |
72d80b7ab288
Added the script for session 2 tutorial.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff
changeset
|
74 |
|
37
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
75 |
So here is the required plot. We may proceed to label the axes, title the plot and save it. |
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
76 |
|
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
77 |
In this tutorial we have learnt how to create lists and append items to them. We have learnt how to process data using lists, how to open and read files and the 'for' loop. |
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
78 |
|
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
79 |
That brings us to the end of this session. |
c2634d874e33
Added script for third session first part.
Shantanu <shantanu@fossee.in>
parents:
36
diff
changeset
|
80 |
Hope this information was helpful. Thank you. |