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