author | Puneeth Chaganti <punchagan@gmail.com> |
Tue, 13 Apr 2010 14:32:38 +0530 | |
changeset 53 | 3d2c2c0bc3e2 |
parent 32 | 74bf7904691d |
permissions | -rw-r--r-- |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
1 |
********************************************************************************** |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
2 |
Hello and welcome, this is the second tutorial in the series of spoken tutorials on Python for Scientific computing. |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
3 |
|
27
5712cc7589f9
Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents:
21
diff
changeset
|
4 |
Here we will teach you how to plot experimental data. |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
5 |
|
27
5712cc7589f9
Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents:
21
diff
changeset
|
6 |
You can input the data either as a list or read from a plain text/binary file. We will cover both one by one. |
5712cc7589f9
Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents:
21
diff
changeset
|
7 |
Please make sure you have pendulum.txt file, as mentioned on requirement list of session |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
8 |
|
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
9 |
So let's begin. First we will input the data as lists and then we will plot it. |
27
5712cc7589f9
Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents:
21
diff
changeset
|
10 |
So on the Interpreter we will type |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
11 |
x = [0, 1, 2.1, 3.1, 4.2, 5.2] |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
12 |
here x is a list. In python, list is a container that holds a number of objects. Various functions related to lists will be covered in more detail later. |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
13 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
14 |
Now for the corresponding Y values type |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
15 |
y = [0, 0.8, 0.9, 0, -0.9, -0.8] |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
16 |
|
27
5712cc7589f9
Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents:
21
diff
changeset
|
17 |
Now we have x and y in two separate lists and we plot x vs. y via: |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
18 |
plot (x, y, 'o') |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
19 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
20 |
Here, we have our plot! |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
21 |
[We close the plot window. ] |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
22 |
|
32 | 23 |
Now, that we know how to plot data from lists, we will look at plotting data from a text file. Essentially, we read the data from the file and fit them into lists again. Then we can easily plot it, as we did before. |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
24 |
|
27
5712cc7589f9
Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents:
21
diff
changeset
|
25 |
As an example we will use the data collected from a simple pendulum experiment. |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
26 |
|
27
5712cc7589f9
Changes to slides of seccond session.
Shantanu <shantanu@fossee.in>
parents:
21
diff
changeset
|
27 |
In []: cat pendulum.txt (windows wont work!) |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
28 |
The cat command shows the file content. |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
29 |
The first column is the length of the pendulum and the second column is the time. We read the file line-by-line, collect the data into lists and plot them. |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
30 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
31 |
We begin with initializing three empty lists for length, time-period and square of the time-period. |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
32 |
l = [] |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
33 |
t = [] |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
34 |
tsq = [] |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
35 |
|
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
36 |
Now we open the file and read it line by line. |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
37 |
for line in open('pendulum.txt'): |
3
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
38 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
39 |
The ':' at the end of the 'for' statement marks the start of the block. |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
40 |
'open' returns an iterable object which we traverse using the 'for' loop. In python, 'for' iterates over items of any sequence. |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
41 |
#we will cover more about the 'for' loop in other spoken tutorials |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
42 |
'line' is a string variable storing one line at a time as the 'for' loop iterates through the file. |
3
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
43 |
|
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
44 |
We split each line at the space using |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
45 |
point = line.split() |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
46 |
split function will return a list. In this case it will have two elements, first is length and second is time. |
3
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
47 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
48 |
Note the indentation here. Everything inside the 'for' loop has to be indented by 4 spaces. |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
49 |
Then we append the length and time values to the corresponding lists. Note that they are converted from strings to floats, before appending to the lists |
3
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
50 |
l.append(float(point[0])) |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
51 |
append is a function used to append one element to the list. |
3
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
52 |
t.append(float(point[1])) |
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
53 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
54 |
By now we have the time and length values in two lists. Now to get the square of the time values, we will write one more 'for' loop which will iterate through list 't' |
3
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
55 |
|
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
56 |
for time in t: |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
57 |
tsq.append(time*time) |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
58 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
59 |
Now lists l(en) and tsq have the required data. We can simply plot them, as we did earlier. |
3
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
60 |
plot(l, tsq, 'o') |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
61 |
|
32 | 62 |
So here is the required plot. In this way, you can plot data from files. Hope this information was helpful. See you in the next tutorial |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
63 |
|
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
64 |
****************** |
3
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
65 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
66 |
For alternate ways of loading data from files go through the tutorial on loadtxt |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
67 |
We should have two tutorials here, one should be basic, using for loops and lists |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
68 |
Second one using loadtxt. |