author | Shantanu <shantanu@fossee.in> |
Tue, 06 Apr 2010 23:43:58 +0530 | |
changeset 21 | cc33c97ed034 |
parent 19 | b77b9fce62d6 |
child 27 | 5712cc7589f9 |
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 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
4 |
Here we will teach you how to plot experimental data, with two variables. |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
5 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
6 |
You could input the data either as a list or read from a plain text/binary file. |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
7 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
8 |
# Before going through this video, you should have a working knowledge of |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
9 |
# - Lists |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
10 |
# - How to initialize them |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
11 |
# - How to append elements to the lists |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
12 |
# - ??? for command |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
13 |
# - How to iterate over a list |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
14 |
# - How to split a command |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
15 |
# - The plot command |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
16 |
# - How to plot two variables |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
17 |
|
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
18 |
So let's begin. First we will input the data as lists and then we will plot it. |
21
cc33c97ed034
Changed presentation file for basic-plotting.
Shantanu <shantanu@fossee.in>
parents:
19
diff
changeset
|
19 |
So on the IPython Interpreter we will type |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
20 |
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
|
21 |
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
|
22 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
23 |
Now for the corresponding Y values type |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
24 |
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
|
25 |
|
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
26 |
Now we have x and y in two separate lists and we plot x vs. y. |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
27 |
plot (x, y, 'o') |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
28 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
29 |
Here, we have our plot! |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
30 |
[We close the plot window. ] |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
31 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
32 |
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 massage it into lists again. Then we can easily plot it, as we already did before. |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
33 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
34 |
As an example we will use the data collected from a simple pendulum experiment. Please make sure you have pendulum.txt file, as mentioned on requirement page(link) |
0
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 |
In []: cat pendulum.txt (windows?) |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
37 |
The cat command shows the file content. |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
38 |
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
|
39 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
40 |
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
|
41 |
l = [] |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
42 |
t = [] |
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
43 |
tsq = [] |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
44 |
|
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
45 |
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
|
46 |
for line in open('pendulum.txt'): |
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 |
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
|
49 |
'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
|
50 |
#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
|
51 |
'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
|
52 |
|
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
53 |
We split each line at the space using |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
54 |
point = line.split() |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
55 |
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
|
56 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
57 |
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
|
58 |
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
|
59 |
l.append(float(point[0])) |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
60 |
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
|
61 |
t.append(float(point[1])) |
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
62 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
63 |
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
|
64 |
|
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
65 |
for time in t: |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
66 |
tsq.append(time*time) |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
67 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
68 |
# We also calculate the squares of the time-period and append to the |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
69 |
# end of the tsq list. |
3
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
70 |
# tsq.append(t[-1]*t[-1]) |
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
71 |
# For any given list to access the last element python provides '-1' |
3
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
72 |
# index, so we use t[-1]. |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
73 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
74 |
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
|
75 |
plot(l, tsq, 'o') |
0
67604aed10e0
Initialization and scripts for first two sessions.
shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
76 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
77 |
So here is the required plot. In this way, you can plot data from files. Hope this information was helpful. See you. |
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
78 |
|
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
79 |
****************** |
3
093edb39f292
Added more changes to plotting-files script.
shantanu <shantanu@fossee.in>
parents:
0
diff
changeset
|
80 |
|
19
b77b9fce62d6
Some changes to basic-plot.txt.
Shantanu <shantanu@fossee.in>
parents:
3
diff
changeset
|
81 |
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
|
82 |
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
|
83 |
Second one using loadtxt. |