Initialization and scripts for first two sessions.
authorshantanu <shantanu@fossee.in>
Tue, 30 Mar 2010 11:00:01 +0530
changeset 0 67604aed10e0
child 1 f48921e39df1
child 6 e1fcec83e1ab
Initialization and scripts for first two sessions.
basic-plot.txt
plotting-files.txt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/basic-plot.txt	Tue Mar 30 11:00:01 2010 +0530
@@ -0,0 +1,57 @@
+* Script
+
+In this tutorial, we will cover the basics of Plotting features available in Python. We shall use Ipython and pylab. Ipython is An Enhanced Interactive Python interpreter. It provides additional features like tab completion, help etc. pylab is python library which provides plotting functionality. 
+
+Lets start ipython. Open up your terminal and type the following. 
+
+$ ipython -pylab
+
+This will give us a prompt where we can get started. 
+
+First, we create an array with equally spaced points from 0 to 2*pi
+In []: x = linspace(0, 2*pi, 100)
+
+We have passed three arguments to linspace function - the first point, the last point and the total number of points. 
+lets see what is x
+In []: x
+ x is a sequence of 100 points starting from 0 to 2*pi. 
+In []: len(x)
+ Shows the length of x to be 100 points. 
+
+To obtain the plot we say, 
+In []: plot(x, sin(x))
+
+It gives a plot of x vs y, where y is sin values of x, with the default color and line properties. 
+
+Both 'pi' and 'sin' come from 'pylab'. 
+
+Now that we have a basic plot, we can label and title the plot. 
+In []: xlabel('x') adds a label to the x-axis. Note that 'x' is enclosed in quotes. 
+In []: ylabel('sin(x)') adds a label to the y-axis. 
+In []: title('Sinusoid') adds a title to the plot. 
+
+Now we add a legend to the plot. 
+In []: legend(['sin(x)'])
+
+We can specify the location of the legend, by passing an additional argument to the function. 
+In []: legend(['sin(2y)'], loc = 'center')
+
+other positions which can be tried are 
+'best' 
+'right'
+
+We now annotate, i.e add a comment at, the point with maximum sin value. 
+In []: annotate('local max', xy=(1.5, 1))
+
+The first argument is the comment and second one is the position for it. 
+
+Now, we save the plot 
+In []: savefig('sin.png') saves the figure as sin.png in current directory. 
+
+?#other supported formats are: eps, emf, ps, pdf, ps, raw, rgba, svg
+
+and finally to close the plot
+In []: close()
+
+
+****************
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plotting-files.txt	Tue Mar 30 11:00:01 2010 +0530
@@ -0,0 +1,101 @@
+Hello, this is second tutorial from the series of Python for 
+Scientific computing.
+
+Here we will teach you how to plot experimental data, with two
+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. 
+
+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?)
+
+# Before starting with this video, you should be comfortable with
+#   - Lists
+#     - initializing them
+#     - appending elements to lists
+#   - for command
+#     - iterating over a list
+#   - split command
+#   - plot command
+#     - plotting two variables
+
+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
+objects. Various functions related to lists will be covered in more 
+detail later.
+
+We create one more list to represent corresponding Y values
+y = [0, 0.8, 0.9, 0, -0.9, -0.8]
+
+Now we have x and y in two lists and we make a plot of x vs. y. 
+plot (x, y, 'o')
+Here, we have our plot!
+
+[We close the plot window. ]
+
+Now, that we know how to plot data which is in lists, we will look at
+plotting data which is in 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. 
+
+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
+
+In []: cat pendulum.txt (windows?)
+
+The cat command, shows the contents of the file. 
+
+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 = []
+#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'):
+
+open returns a iterable object which we traverse using for loop. In 
+python  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 
+    point = line.split()
+
+mind the indentation here.
+
+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].
+
+Now the lists l, t have the required data. We can simply plot them, as
+we did already. 
+
+plot(l, t, 'o')
+
+Enjoy!
+
+******************
+We should have two tutorials here, one should be basic, using for loops
+and lists
+Second one using loadtxt.