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