3 |
3 |
4 Here we will teach you how to plot experimental data, with two |
4 Here we will teach you how to plot experimental data, with two |
5 variables. Please make sure you have pendulum.txt file, we will |
5 variables. Please make sure you have pendulum.txt file, we will |
6 be using it for introduction of concepts(requirements page!). |
6 be using it for introduction of concepts(requirements page!). |
7 |
7 |
8 In general, we don't plot (analytical) functions. We often have |
8 We shall look at inputting experimental data and plotting it. |
9 experimental data points, that we wish to plot. We shall look at |
|
10 inputting this data and plotting it. |
|
11 |
9 |
12 The data could be input (or entered) in two formats. For smaller data |
10 The data could be input either as lists or read from plain text/ |
13 sets we could use lists to input the data and use plain text files for |
11 binary files |
14 (somewhat?) larger ones. (Binary files?) |
|
15 |
12 |
16 # Before starting with this video, you should be comfortable with |
13 # Before starting with this video, you should be comfortable with |
17 # - Lists |
14 # - Lists |
18 # - initializing them |
15 # - initializing them |
19 # - appending elements to lists |
16 # - appending elements to lists |
24 # - plotting two variables |
21 # - plotting two variables |
25 |
22 |
26 Let's begin with inputting the data as lists and plotting it. |
23 Let's begin with inputting the data as lists and plotting it. |
27 |
24 |
28 x = [0, 1, 2.1, 3.1, 4.2, 5.2] |
25 x = [0, 1, 2.1, 3.1, 4.2, 5.2] |
29 here x is a list. I python list is a container that holds number of |
26 here x is a list. In python list is a container that holds number of |
30 objects. Various functions related to lists will be covered in more |
27 objects. Various functions related to lists will be covered in more |
31 detail later. |
28 detail later. |
32 |
29 |
33 We create one more list to represent corresponding Y values |
30 We create one more list to represent corresponding Y values |
34 y = [0, 0.8, 0.9, 0, -0.9, -0.8] |
31 y = [0, 0.8, 0.9, 0, -0.9, -0.8] |
43 plotting data which is in a text file. Essentially, we read the data |
40 plotting data which is in a text file. Essentially, we read the data |
44 from the file and massage it into lists again. Then we can easily |
41 from the file and massage it into lists again. Then we can easily |
45 plot it, as we already did. |
42 plot it, as we already did. |
46 |
43 |
47 As an example we will use the data collected from a simple pendulum |
44 As an example we will use the data collected from a simple pendulum |
48 experiment. We have the data of, the length of pendulum vs. the time |
45 experiment. |
49 period of the pendulum in the file pendulum.txt |
|
50 |
46 |
51 In []: cat pendulum.txt (windows?) |
47 In []: cat pendulum.txt (windows?) |
52 |
48 |
53 The cat command, shows the contents of the file. |
49 The cat command, shows the contents of the file. |
54 |
50 |
58 |
54 |
59 We begin with initializing three empty lists for length, time-period |
55 We begin with initializing three empty lists for length, time-period |
60 and square of the time-period. |
56 and square of the time-period. |
61 |
57 |
62 l = [] |
58 l = [] |
63 #len? (confusion over 1 and l(damm they are really same looking:P)) |
|
64 t = [] |
59 t = [] |
65 tsq = [] |
60 tsq = [] |
66 |
61 |
67 Now we open the file and read it line by line. |
62 Now we open the file and read it line by line. |
68 for line in open('pendulum.txt'): |
63 for line in open('pendulum.txt'): |
69 |
64 |
|
65 ':' in end of for statement marks the start of block. |
|
66 |
70 open returns a iterable object which we traverse using for loop. In |
67 open returns a iterable object which we traverse using for loop. In |
71 python iterates over items of any sequence. |
68 python for iterates over items of any sequence. |
72 #we will cover more of 'for' loop in later sections |
69 #we will cover more of 'for' loop in later sections |
73 line is a string variable storing one line at a time as for loop |
70 line is a string variable storing one line at a time as for loop |
74 iterates through file. |
71 iterates through file. |
75 We split each line at the space |
72 |
|
73 We split each line at the space using |
|
74 |
76 point = line.split() |
75 point = line.split() |
77 |
76 |
78 mind the indentation here. |
77 split function will return list. In this case it will have two |
|
78 elements, first is length and second being time. |
|
79 |
|
80 mind the indentation here. Everything inside 'for' loop has to be |
|
81 indented by 4 spaces. |
79 |
82 |
80 Then we append the length and time values to the corresponding |
83 Then we append the length and time values to the corresponding |
81 lists. Note that they are converted from strings to floats, before |
84 lists. Note that they are converted from strings to floats, before |
82 appending to the lists |
85 appending to the lists |
83 l.append(float(point[0]) |
86 l.append(float(point[0])) |
84 t.append(float(point[1]) |
87 append is function to append one element to the list. |
85 We also calculate the squares of the time-period and append to the end |
88 t.append(float(point[1])) |
86 of the tsq list. |
|
87 tsq.append(t[-1]*t[-1]) |
|
88 For any given list to access last element python provides '-1' index, |
|
89 so we use t[-1]. |
|
90 |
89 |
91 Now the lists l, t have the required data. We can simply plot them, as |
90 By this time we have time and length values in two lists. Now to get |
92 we did already. |
91 square of time values we will write one more for loop which this time |
|
92 iterate through list 't' |
93 |
93 |
94 plot(l, t, 'o') |
94 for time in t: |
|
95 tsq.append(time*time) |
95 |
96 |
96 Enjoy! |
97 # We also calculate the squares of the time-period and append to the |
|
98 # end of the tsq list. |
|
99 # tsq.append(t[-1]*t[-1]) |
|
100 # For any given list to access last element python provides '-1' |
|
101 # index, so we use t[-1]. |
97 |
102 |
|
103 Now lists l(en) and tsq have the required data. We can simply plot |
|
104 them, as we did already. |
|
105 |
|
106 plot(l, tsq, 'o') |
|
107 |
|
108 Enjoy(Thank you)! |
|
109 |
|
110 For alternate ways of loading data from files go through tutorial on |
|
111 loadtxt |
98 ****************** |
112 ****************** |
99 We should have two tutorials here, one should be basic, using for loops |
113 We should have two tutorials here, one should be basic, using for |
100 and lists |
114 loops and lists |
101 Second one using loadtxt. |
115 Second one using loadtxt. |