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