1 ********************************************************************************** |
1 ********************************************************************************** |
2 Hello and welcome, this is the second tutorial in the series of spoken tutorials on Python for Scientific computing. |
2 Hello and welcome, this is the second tutorial in the series of spoken tutorials on Python for Scientific computing. |
3 |
3 |
4 Here we will teach you how to plot experimental data, with two variables. |
4 Here we will teach you how to plot experimental data. |
5 |
5 |
6 You could input the data either as a list or read from a plain text/binary file. |
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. |
7 |
7 Please make sure you have pendulum.txt file, as mentioned on requirement list of session |
8 # Before going through this video, you should have a working knowledge of |
|
9 # - Lists |
|
10 # - How to initialize them |
|
11 # - How to append elements to the lists |
|
12 # - ??? for command |
|
13 # - How to iterate over a list |
|
14 # - How to split a command |
|
15 # - The plot command |
|
16 # - How to plot two variables |
|
17 |
8 |
18 So let's begin. First we will input the data as lists and then we will plot it. |
9 So let's begin. First we will input the data as lists and then we will plot it. |
19 So on the IPython Interpreter we will type |
10 So on the Interpreter we will type |
20 x = [0, 1, 2.1, 3.1, 4.2, 5.2] |
11 x = [0, 1, 2.1, 3.1, 4.2, 5.2] |
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. |
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. |
22 |
13 |
23 Now for the corresponding Y values type |
14 Now for the corresponding Y values type |
24 y = [0, 0.8, 0.9, 0, -0.9, -0.8] |
15 y = [0, 0.8, 0.9, 0, -0.9, -0.8] |
25 |
16 |
26 Now we have x and y in two separate lists and we plot x vs. y. |
17 Now we have x and y in two separate lists and we plot x vs. y via: |
27 plot (x, y, 'o') |
18 plot (x, y, 'o') |
28 |
19 |
29 Here, we have our plot! |
20 Here, we have our plot! |
30 [We close the plot window. ] |
21 [We close the plot window. ] |
31 |
22 |
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. |
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 already did before. |
33 |
24 |
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) |
25 As an example we will use the data collected from a simple pendulum experiment. |
35 |
26 |
36 In []: cat pendulum.txt (windows?) |
27 In []: cat pendulum.txt (windows wont work!) |
37 The cat command shows the file content. |
28 The cat command shows the file content. |
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. |
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. |
39 |
30 |
40 We begin with initializing three empty lists for length, time-period and square of the time-period. |
31 We begin with initializing three empty lists for length, time-period and square of the time-period. |
41 l = [] |
32 l = [] |
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' |
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' |
64 |
55 |
65 for time in t: |
56 for time in t: |
66 tsq.append(time*time) |
57 tsq.append(time*time) |
67 |
58 |
68 # We also calculate the squares of the time-period and append to the |
|
69 # end of the tsq list. |
|
70 # tsq.append(t[-1]*t[-1]) |
|
71 # For any given list to access the last element python provides '-1' |
|
72 # index, so we use t[-1]. |
|
73 |
|
74 Now lists l(en) and tsq have the required data. We can simply plot them, as we did earlier. |
59 Now lists l(en) and tsq have the required data. We can simply plot them, as we did earlier. |
75 plot(l, tsq, 'o') |
60 plot(l, tsq, 'o') |
76 |
61 |
77 So here is the required plot. In this way, you can plot data from files. Hope this information was helpful. See you. |
62 So here is the required plot. In this way, you can plot data from files. Hope this information was helpful. See you. |
78 |
63 |