author | shantanu <shantanu@fossee.in> |
Tue, 30 Mar 2010 11:00:01 +0530 | |
changeset 0 | 67604aed10e0 |
child 3 | 093edb39f292 |
permissions | -rw-r--r-- |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
1 |
Hello, this is second tutorial from the series of Python for |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
2 |
Scientific computing. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
3 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
4 |
Here we will teach you how to plot experimental data, with two |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
5 |
variables. Please make sure you have pendulum.txt file, we will |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
6 |
be using it for introduction of concepts(requirements page!). |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
7 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
8 |
In general, we don't plot (analytical) functions. We often have |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
9 |
experimental data points, that we wish to plot. We shall look at |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
10 |
inputting this data and plotting it. |
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 |
The data could be input (or entered) in two formats. For smaller data |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
13 |
sets we could use lists to input the data and use plain text files for |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
14 |
(somewhat?) larger ones. (Binary files?) |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
15 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
16 |
# Before starting with this video, you should be comfortable with |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
17 |
# - Lists |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
18 |
# - initializing them |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
19 |
# - appending elements to lists |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
20 |
# - for command |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
21 |
# - iterating over a list |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
22 |
# - split command |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
23 |
# - plot command |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
24 |
# - plotting two variables |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
25 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
26 |
Let's begin with inputting the data as lists and plotting it. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
27 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
28 |
x = [0, 1, 2.1, 3.1, 4.2, 5.2] |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
29 |
here x is a list. I python list is a container that holds number of |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
30 |
objects. Various functions related to lists will be covered in more |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
31 |
detail later. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
32 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
33 |
We create one more list to represent corresponding Y values |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
34 |
y = [0, 0.8, 0.9, 0, -0.9, -0.8] |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
35 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
36 |
Now we have x and y in two lists and we make a plot of x vs. y. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
37 |
plot (x, y, 'o') |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
38 |
Here, we have our plot! |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
39 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
40 |
[We close the plot window. ] |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
41 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
42 |
Now, that we know how to plot data which is in lists, we will look at |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
43 |
plotting data which is in a text file. Essentially, we read the data |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
44 |
from the file and massage it into lists again. Then we can easily |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
45 |
plot it, as we already did. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
46 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
47 |
As an example we will use the data collected from a simple pendulum |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
48 |
experiment. We have the data of, the length of pendulum vs. the time |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
49 |
period of the pendulum in the file pendulum.txt |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
50 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
51 |
In []: cat pendulum.txt (windows?) |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
52 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
53 |
The cat command, shows the contents of the file. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
54 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
55 |
The first column is the length of the pendulum and the second column |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
56 |
is the time. We read the file line-by-line, collect the data into |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
57 |
lists and plot them. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
58 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
59 |
We begin with initializing three empty lists for length, time-period |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
60 |
and square of the time-period. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
61 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
62 |
l = [] |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
63 |
#len? (confusion over 1 and l(damm they are really same looking:P)) |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
64 |
t = [] |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
65 |
tsq = [] |
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 we open the file and read it line by line. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
68 |
for line in open('pendulum.txt'): |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
69 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
70 |
open returns a iterable object which we traverse using for loop. In |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
71 |
python iterates over items of any sequence. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
72 |
#we will cover more of 'for' loop in later sections |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
73 |
line is a string variable storing one line at a time as for loop |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
74 |
iterates through file. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
75 |
We split each line at the space |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
76 |
point = line.split() |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
77 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
78 |
mind the indentation here. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
79 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
80 |
Then we append the length and time values to the corresponding |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
81 |
lists. Note that they are converted from strings to floats, before |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
82 |
appending to the lists |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
83 |
l.append(float(point[0]) |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
84 |
t.append(float(point[1]) |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
85 |
We also calculate the squares of the time-period and append to the end |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
86 |
of the tsq list. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
87 |
tsq.append(t[-1]*t[-1]) |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
88 |
For any given list to access last element python provides '-1' index, |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
89 |
so we use t[-1]. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
90 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
91 |
Now the lists l, t have the required data. We can simply plot them, as |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
92 |
we did already. |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
93 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
94 |
plot(l, t, 'o') |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
95 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
96 |
Enjoy! |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
97 |
|
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
98 |
****************** |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
99 |
We should have two tutorials here, one should be basic, using for loops |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
100 |
and lists |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
101 |
Second one using loadtxt. |