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