plotting-files.org
changeset 2 008c0edc6eac
equal deleted inserted replaced
1:f48921e39df1 2:008c0edc6eac
       
     1 * Plotting Experimental Data
       
     2 *** Outline
       
     3 ***** Introduction
       
     4 ******* Why do we want to do that?
       
     5 ******* Inputting data
       
     6 ********* Lists
       
     7 ********* Files
       
     8 ******* Arsenal Required
       
     9 ********* Lists
       
    10 *********** initializing
       
    11 *********** appending to lists
       
    12 ********* for loops
       
    13 *********** iterating over a list
       
    14 ********* basic plotting
       
    15 ***** Inputting data as lists
       
    16 ******* Input the dependent and independent variables in two lists
       
    17 ******* Plot the points
       
    18 ***** Plotting from files
       
    19 ******* Look at the file
       
    20 ******* Read the file and get data into variables
       
    21 ******* Do any massaging required
       
    22 ******* Plot the points
       
    23 
       
    24 *** Script
       
    25 
       
    26 This video will teach you how to plot experimental data, with two
       
    27 variables. 
       
    28 
       
    29 In general, we don't plot (analytical) functions. We often have
       
    30 experimental data points, that we wish to plot. We shall look at
       
    31 inputting this data and plotting it. 
       
    32 
       
    33 The data could be input (or entered) in two formats. For smaller data
       
    34 sets we could use lists to input the data and use plain text files for
       
    35 (somewhat?) larger ones. (Binary files?)
       
    36 
       
    37 [[[Before starting with this video, you should be comfortable with
       
    38   - Lists
       
    39     - initializing them
       
    40     - appending elements to lists
       
    41   - for command
       
    42     - iterating over a list
       
    43   - split command
       
    44   - plot command
       
    45     - plotting two variables
       
    46 ]]]
       
    47 
       
    48 Let's begin with inputting the data as lists and plotting it. 
       
    49 
       
    50 x = [0, 1, 2.1, 3.1, 4.2, 5.2]
       
    51 y = [0, 0.8, 0.9, 0, -0.9, -0.8]
       
    52 Now we have two lists x and y, with the values that we wish to plot. 
       
    53 
       
    54 We say:
       
    55 plot (x, y, 'o')
       
    56 
       
    57 and there, we have our plot!
       
    58 
       
    59 [We close the plot window. ]
       
    60 
       
    61 Now, that we know how to plot data which is in lists, we will look at
       
    62 plotting data which is in a text file. Essentially, we read the data
       
    63 from the file and massage it into lists again. Then we can easily
       
    64 plot it, as we already did. 
       
    65 
       
    66 As an example we will use the data collected from a simple pendulum
       
    67 experiment. We have the data of, the length of pendulum vs. the time
       
    68 period of the pendulum in the file pendulum.txt
       
    69 
       
    70 In []: cat pendulum.txt (windows?)
       
    71 
       
    72 The cat command, shows the contents of the file. 
       
    73 
       
    74 The first column is the length of the pendulum and the second column
       
    75 is the time. We read the file line-by-line, collect the data into
       
    76 lists and plot them. 
       
    77 
       
    78 We begin with initializing three empty lists for length, time-period
       
    79 and square of the time-period. 
       
    80 
       
    81 l = []
       
    82 t = []
       
    83 tsq = []
       
    84 
       
    85 Now we open the file and read it line by line. 
       
    86 for line in open('pendulum.txt'):
       
    87 
       
    88 We split each line at the space 
       
    89     point = line.split()
       
    90 
       
    91 Then we append the length and time values to the corresponding
       
    92 lists. Note that they are converted from strings to floats, before
       
    93 appending to the lists
       
    94     l.append(float(point[0])
       
    95     t.append(float(point[1])
       
    96 We also calculate the squares of the time-period and append to the end
       
    97 of the tsq list. 
       
    98     tsq.append(t[-1]*t[-1])
       
    99 As you might be aware, t[-1] gives the last element of the list t. 
       
   100 
       
   101 Now the lists l, t have the required data. We can simply plot them, as
       
   102 we did already. 
       
   103 
       
   104 plot(l, t, 'o')
       
   105 
       
   106 Enjoy!
       
   107 
       
   108 *** Notes
       
   109     - Also put in code snippets?