# HG changeset patch # User shantanu # Date 1269943844 -19800 # Node ID 093edb39f292e733bf1ae512b0085b67252479d6 # Parent 008c0edc6eac045aadacd99dae911ac81e6cf074 Added more changes to plotting-files script. diff -r 008c0edc6eac -r 093edb39f292 plotting-files.txt --- a/plotting-files.txt Tue Mar 30 14:53:58 2010 +0530 +++ b/plotting-files.txt Tue Mar 30 15:40:44 2010 +0530 @@ -5,13 +5,10 @@ variables. Please make sure you have pendulum.txt file, we will be using it for introduction of concepts(requirements page!). -In general, we don't plot (analytical) functions. We often have -experimental data points, that we wish to plot. We shall look at -inputting this data and plotting it. +We shall look at inputting experimental data and plotting it. -The data could be input (or entered) in two formats. For smaller data -sets we could use lists to input the data and use plain text files for -(somewhat?) larger ones. (Binary files?) +The data could be input either as lists or read from plain text/ +binary files # Before starting with this video, you should be comfortable with # - Lists @@ -26,7 +23,7 @@ Let's begin with inputting the data as lists and plotting it. x = [0, 1, 2.1, 3.1, 4.2, 5.2] -here x is a list. I python list is a container that holds number of +here x is a list. In python list is a container that holds number of objects. Various functions related to lists will be covered in more detail later. @@ -45,8 +42,7 @@ plot it, as we already did. As an example we will use the data collected from a simple pendulum -experiment. We have the data of, the length of pendulum vs. the time -period of the pendulum in the file pendulum.txt +experiment. In []: cat pendulum.txt (windows?) @@ -60,42 +56,60 @@ and square of the time-period. l = [] -#len? (confusion over 1 and l(damm they are really same looking:P)) t = [] tsq = [] Now we open the file and read it line by line. for line in open('pendulum.txt'): +':' in end of for statement marks the start of block. + open returns a iterable object which we traverse using for loop. In -python iterates over items of any sequence. +python for iterates over items of any sequence. #we will cover more of 'for' loop in later sections line is a string variable storing one line at a time as for loop iterates through file. -We split each line at the space + +We split each line at the space using + point = line.split() -mind the indentation here. +split function will return list. In this case it will have two +elements, first is length and second being time. + +mind the indentation here. Everything inside 'for' loop has to be +indented by 4 spaces. 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 - l.append(float(point[0]) - t.append(float(point[1]) -We also calculate the squares of the time-period and append to the end -of the tsq list. - tsq.append(t[-1]*t[-1]) -For any given list to access last element python provides '-1' index, -so we use t[-1]. + l.append(float(point[0])) +append is function to append one element to the list. + t.append(float(point[1])) + +By this time we have time and length values in two lists. Now to get +square of time values we will write one more for loop which this time +iterate through list 't' + +for time in t: + tsq.append(time*time) -Now the lists l, t have the required data. We can simply plot them, as -we did already. +# We also calculate the squares of the time-period and append to the +# end of the tsq list. +# tsq.append(t[-1]*t[-1]) +# For any given list to access last element python provides '-1' +# index, so we use t[-1]. -plot(l, t, 'o') +Now lists l(en) and tsq have the required data. We can simply plot +them, as we did already. -Enjoy! +plot(l, tsq, 'o') +Enjoy(Thank you)! + +For alternate ways of loading data from files go through tutorial on +loadtxt ****************** -We should have two tutorials here, one should be basic, using for loops -and lists +We should have two tutorials here, one should be basic, using for +loops and lists Second one using loadtxt.