plotting-files.txt
author asokan <asokan@fossee.in>
Tue, 18 May 2010 15:40:17 +0530
changeset 126 2eac725a5766
parent 32 74bf7904691d
permissions -rw-r--r--
changes to array.txt

**********************************************************************************
Hello and welcome, this is the second tutorial in the series of spoken tutorials on Python for Scientific computing. 

Here we will teach you how to plot experimental data.

You can input the data either as a list or read from a plain text/binary file. We will cover both one by one.
Please make sure you have pendulum.txt file, as mentioned on requirement list of session
 
So let's begin.  First we will input the data as lists and then we will plot it. 
So on the Interpreter we will type
x = [0, 1, 2.1, 3.1, 4.2, 5.2]
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. 

Now for the corresponding Y values type
y = [0, 0.8, 0.9, 0, -0.9, -0.8]
 
Now we have x and y in two separate lists and we plot x vs. y via:
plot (x, y, 'o')

Here, we have our plot! 
[We close the plot window. ] 

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 fit them into lists again. Then we can easily plot it, as we  did before. 

As an example we will use the data collected from a simple pendulum experiment. 

In []: cat pendulum.txt (windows wont work!)
The cat command shows the file content.
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.

We begin with initializing three empty lists for length, time-period and square of the time-period.
l = []
t = []
tsq = []
 
Now we open the file and read it line by line.
for line in open('pendulum.txt'): 

The ':' at the end of the 'for' statement marks the start of the block.
'open' returns an iterable object which we traverse using the 'for' loop. In  python, 'for' iterates over items of any sequence.
#we will cover more about the 'for' loop in other spoken tutorials
'line' is a string variable storing one line at a time as the 'for' loop iterates through the file.

We split each line at the space using
     point = line.split() 
split function will return a list. In this case it will have two elements, first is length and second is time. 

Note the indentation here. Everything inside the '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]))
append is a function used to append one element to the list.
    t.append(float(point[1]))

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'

for time in t:
    tsq.append(time*time) 

Now lists l(en) and tsq have the required data. We can simply plot them, as we did earlier. 
plot(l, tsq, 'o')

So here is the required plot. In this way, you can plot data from files.  Hope this information was helpful. See you in the next tutorial 

******************

For alternate ways of loading data from files go through the tutorial on loadtxt
We should have two tutorials here, one should be basic, using for loops and lists
Second one using loadtxt.