summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dictionaries/script.rst (renamed from dictionaries.rst)0
-rw-r--r--getting-started-with-arrays/script.rst (renamed from getting_started_with_arrays.rst)33
-rw-r--r--getting-started-with-for/script.rst (renamed from getting_started_with_for.rst)1
-rw-r--r--gettings_started_with_for.rst217
-rw-r--r--matrices/script.rst265
-rw-r--r--other-type-of-plots/script.rst (renamed from other_type_of_plots.rst)0
-rw-r--r--other_types_of_plots.rst220
-rw-r--r--savefig/script.rst (renamed from savefig.rst)15
-rw-r--r--using python modules/four_plot.pngbin0 -> 54760 bytes
-rw-r--r--using python modules/four_plot.py11
-rw-r--r--using python modules/script.rst227
-rw-r--r--using python modules/sine.py11
12 files changed, 557 insertions, 443 deletions
diff --git a/dictionaries.rst b/dictionaries/script.rst
index 63fca2a..63fca2a 100644
--- a/dictionaries.rst
+++ b/dictionaries/script.rst
diff --git a/getting_started_with_arrays.rst b/getting-started-with-arrays/script.rst
index d01b884..eb9ba80 100644
--- a/getting_started_with_arrays.rst
+++ b/getting-started-with-arrays/script.rst
@@ -37,6 +37,8 @@ lists, it is at least 80 to 100 times faster than lists.
{{{ switch to the next slide, creating arrays }}}
+Now let us see how to create arrays.
+
I am assuming that you have your IPython interpreter running with the
``-pylab`` option, so that you have the required modules loaded.
@@ -47,11 +49,36 @@ To create an array we will use the function ``array()`` as,
Notice that here we created a one dimensional array. Also notice the
object we passed to create an array. Now let us see how to create a
-two dimensional array.
+two dimensional array. Pause here and try to do it yourself before
+looking at the solution.
+
+This is how we create two dimensional arrays.
::
a2 = array([[1,2,3,4],[5,6,7,8]])
+Let us see an easy method of creating an array with elements 1 to 8.
+::
+
+ ar = arange(1,9)
+
+And it created a single dimensional array of elements from 1 to 8.
+::
+
+ print ar
+
+And how can we make it a two dimensional array of order 2 by 4. Pause
+here and try to do it yourself, try ``ar.tab`` and find a suitable
+method for that.
+
+We can use the function ``reshape()`` for that purpose and it can be
+done as,
+::
+
+ ar.reshape(2,4)
+ ar.reshape(4,2)
+ ar = ar.reshape(2,4)
+
Now, let us see how to convert a list object to an array. As you have
already seen, in both of the previous statements we have passed a
list, so creating an array can be done so, first let us create a list
@@ -201,7 +228,9 @@ it does not perform matrix multiplication.
{{{ switch to next slide, recap slide }}}
-So this brings us to the end of this tutorial, in this tutorial we covered basics of arrays, how to create an array, converting a list to an array, basic array operations etc.
+So this brings us to the end of this tutorial, in this tutorial we
+covered basics of arrays, how to create an array, converting a list to
+an array, basic array operations etc.
{{{ switch to next slide, thank you }}}
diff --git a/getting_started_with_for.rst b/getting-started-with-for/script.rst
index d5a844f..ce6e26c 100644
--- a/getting_started_with_for.rst
+++ b/getting-started-with-for/script.rst
@@ -270,3 +270,4 @@ Thank you!
Reviewer 1: Nishanth
Reviewer 2: Amit Sethi
External reviewer:
+
diff --git a/gettings_started_with_for.rst b/gettings_started_with_for.rst
deleted file mode 100644
index f832319..0000000
--- a/gettings_started_with_for.rst
+++ /dev/null
@@ -1,217 +0,0 @@
-.. 3.2 LO: getting started with =for= (2) [anoop]
-.. -----------------------------------------------
-.. * blocks in python
-.. + (indentation)
-.. * blocks in ipython
-.. + ... prompt
-.. + hitting enter
-.. * =for= with a list
-.. * =range= function
-
-=============================
-Getting started with for loop
-=============================
-
-{{{ show welcome slide }}}
-
-Hello and welcome to the tutorial getting started with ``for`` loop.
-
-{{{ switch to next slide, outline slide }}}
-
-In this tutorial we will see ``for`` loops in python, and also cover
-the basics of indenting code in python.
-
-{{{ switch to next slide, about whitespaces }}}
-
-In Python whitespace is significant, and the blocks are visually
-separated rather than using braces or any other mechanisms for
-defining blocks. And by this method Python forces the programmers to
-stick on to one way of writing or beautifying the code rather than
-debating over where to place the braces. This way it produces uniform
-code than obscure or unreadable code.
-
-A block may be defined by a suitable indentation level which can be
-either be a tab or few spaces. And the best practice is to indent the
-code using four spaces.
-
-Now let us move straight into ``for`` loop.
-
-{{{ switch to next slide, problem statement of exercise 1 }}}
-
-Write a for loop which iterates through a list of numbers and find the
-square root of each number. Also make a new list with the square roots
-and print it at the end.
-::
-
- numbers are 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916
-
-For the problem, first we need to create a ``list`` of numbers and
-then iterate over the list and find the square root of each element in
-it. And let us create a script, rather than typing it out in the
-interpreter itself. Create a script called list_roots.py and type the
-following.
-
-{{{ open the text editor and paste the following code there }}}
-::
-
- numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916]
- square_roots = []
- for each in numbers:
- sq_root = sqrt(each)
- print "Square root of", each, "is", sq_root
- square_roots.append(sq_root)
- print
- print square_roots
-
-{{{ save the script }}}
-
-Now save the script, and run it from your IPython interpreter. I
-assume that you have started your IPython interpreter using ``-pylab``
-option.
-
-Run the script as,
-::
-
- %run -i list_roots.py
-
-{{{ run the script }}}
-
-So that was easy! We didn't have to find the length of the string nor
-address of each element of the list one by one. All what we did was
-iterate over the list element by element and then use the element for
-calculation. Note that here we used three variables. One the variable
-``numbers``, which is a list, another one ``each``, which is the
-element of list under consideration in each cycle of the ``for`` loop,
-and then a variable ``sq_root`` for storing the square root in each
-cycle of the ``for`` loop. The variable names can be chosen by you.
-
-{{{ show the script which was created }}}
-
-Note that three lines after ``for`` statement, are indented using four
-spaces.
-
-{{{ highlight the threee lines after for statement }}}
-
-It means that those three lines are part of the for loop. And it is
-called a block of statements. And the seventh line or the immediate
-line after the third line in the ``for`` loop is not indented,
-
-{{{ highlight the seventh line - the line just after for loop }}}
-
-it means that it is not part of the ``for`` loop and the lines after
-that doesn't fall in the scope of the ``for`` loop. Thus each block is
-separated by the indentation level. Thus marking the importance of
-white-spaces in Python.
-
-{{{ switch to the slide which shows the problem statement of the first
-problem to be tried out }}}
-
-Now a question for you to try, from the given numbers make a list of
-perfect squares and a list of those which are not. The numbers are,
-::
-
- 7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, 7056, 576, 2916
-
-{{{ switch to next slide, problem statement of second problem in
-solved exercie}}}
-
-Now let us try a simple one, to print the square root of numbers in
-the list. And this time let us do it right in the IPython
-interpreter.
-
-{{{ switch focus to the IPython interpreter }}}
-
-So let us start with making a list. Type the following
-::
-
- numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916]
- for each in numbers:
-
-and now you will notice that, as soon as you press the return key
-after for statement, the prompt changes to four dots and the cursor is
-not right after the four dots but there are four spaces from the
-dots. The four dots tell you that you are inside a block. Now type the
-rest of the ``for`` loop,
-::
-
- sq_root = sqrt(each)
- print "Square root of", each, "is", sq_root
-
-Now we have finished the statements in the block, and still the
-interpreter is showing four dots, which means you are still inside the
-block. To exit from the block press return key or the enter key twice
-without entering anything else. It printed the square root of each
-number in the list, and that is executed in a ``for`` loop.
-
-Now, let us generate the multiplication table of 10 from one to
-ten. But this time let us try it in the vanilla version of Python
-interpreter.
-
-Start the vanilla version of Python interpreter by issuing the command
-``python`` in your terminal.
-
-{{{ open the python interpreter in the terminal using the command
-python to start the vanilla Python interpreter }}}
-
-Start with,
-::
-
- for i in range(1,11):
-
-and press enter once, and we will see that this time it shows four
-dots, but the cursor is close to the dots, so we have to intend the
-block. So enter four spaces there and then type the following
-::
-
-
- print "10 *",i,"=",i*10
-
-Now when we hit enter, we still see the four dots, to get out of the
-block type enter once.
-
-Okay! so the main thing here we learned is how to use Python
-interpreter and IPython interpreter to specify blocks. But while we
-were generating the multiplication table we used something new,
-``range()`` function. ``range()`` is an inbuilt function in Python
-which can be used to generate a ``list`` of integers from a starting
-range to an ending range. Note that the ending number that you specify
-will not be included in the ``list``.
-
-Now, let us print all the odd numbers from 1 to 50. Let us do it in
-our IPython interpreter for ease of use.
-
-{{{ switch focus to ipython interpreter }}}
-
-{{{ switch to next slide, problem statement of the next problem in
-solved exercises }}}
-
-Print the list of odd numbers from 1 to 50. It will be better if
-you can try it out yourself.
-
-It is a very trivial problem and can be solved as,
-::
-
- print range(1,51,2)
-
-This time we passed three parameters to ``range()`` function unlike
-the previous case where we passed only two parameters. The first two
-parameters are the same in both the cases. The first parameter is the
-starting number of the sequence and the second parameter is the end of
-the range. Note that the sequence doesn't include the ending
-number. The third parameter is for stepping through the sequence. Here
-we gave two which means we are skipping every alternate element.
-
-{{{ switch to next slide, recap slide }}}
-
-Thus we come to the end of this tutorial. We learned about blocks in
-Python, indentation, blocks in IPython, for loop, iterating over a
-list and then the ``range()`` function.
-
-{{{ switch to next slide, thank you slide }}}
-
-Thank you!
-
-.. Author: Anoop Jacob Thomas <anoop@fossee.in>
- Reviewer 1:
- Reviewer 2:
- External reviewer:
diff --git a/matrices/script.rst b/matrices/script.rst
new file mode 100644
index 0000000..fa30811
--- /dev/null
+++ b/matrices/script.rst
@@ -0,0 +1,265 @@
+.. 4.3 LO: Matrices (3) [anoop]
+.. -----------------------------
+.. * creating matrices
+.. + direct data
+.. + list conversion
+.. + builtins - identitiy, zeros,
+.. * matrix operations
+.. + + - * /
+.. + dot
+.. + inv
+.. + det
+.. + eig
+.. + norm
+.. + svd
+
+========
+Matrices
+========
+{{{ show the welcome slide }}}
+
+Welcome to the spoken tutorial on Matrices.
+
+{{{ switch to next slide, outline slide }}}
+
+In this tutorial we will learn about matrices, creating matrices and
+matrix operations.
+
+{{{ creating a matrix }}}
+
+All matrix operations are done using arrays. Thus all the operations
+on arrays are valid on matrices also. A matrix may be created as,
+::
+
+ m1 = matrix([1,2,3,4])
+
+Using the tuple ``m1.shape`` we can find out the shape or size of the
+matrix,
+::
+
+ m1.shape
+
+Since it is a one row four column matrix it returned a tuple, one by
+four.
+
+A list can be converted to a matrix as follows,
+::
+
+ l1 = [[1,2,3,4],[5,6,7,8]]
+ m2 = matrix(l1)
+
+Note that all matrix operations are done using arrays, so a matrix may
+also be created as
+::
+
+ m3 = array([[5,6,7,8],[9,10,11,12]])
+
+{{{ switch to next slide, matrix operations }}}
+
+We can do matrix addition and subtraction as,
+::
+
+ m3 + m2
+
+does element by element addition, thus matrix addition.
+
+Similarly,
+::
+
+ m3 - m2
+
+it does matrix subtraction, that is element by element
+subtraction. Now let us try,
+::
+
+ m3 * m2
+
+Note that in arrays ``array(A) star array(B)`` does element wise
+multiplication and not matrix multiplication, but unlike arrays, the
+operation ``matrix(A) star matrix(B)`` does matrix multiplication and
+not element wise multiplication. And in this case since the sizes are
+not compatible for multiplication it returned an error.
+
+And element wise multiplication in matrices are done using the
+function ``multiply()``
+::
+
+ multiply(m3,m2)
+
+Now let us see an example for matrix multiplication. For doing matrix
+multiplication we need to have two matrices of the order n by m and m
+by r and the resulting matrix will be of the order n by r. Thus let us
+first create two matrices which are compatible for multiplication.
+::
+
+ m1.shape
+
+matrix m1 is of the shape one by four, let us create another one of
+the order four by two,
+::
+
+ m4 = matrix([[1,2],[3,4],[5,6],[7,8]])
+ m1 * m4
+
+thus unlike in array object ``star`` can be used for matrix multiplication
+in matrix object.
+
+{{{ switch to next slide, recall from arrays }}}
+
+As we already saw in arrays, the functions ``identity()``,
+``zeros()``, ``zeros_like()``, ``ones()``, ``ones_like()`` may also be
+used with matrices.
+
+{{{ switch to next slide, matrix operations }}}
+
+To find out the transpose of a matrix we can do,
+::
+
+ print m4
+ m4.T
+
+Matrix name dot capital T will give the transpose of a matrix
+
+{{{ switch to next slide, Euclidean norm of inverse of matrix }}}
+
+Now let us try to find out the Euclidean norm of inverse of a 4 by 4
+matrix, the matrix being,
+::
+
+ m5 = matrix(arange(1,17).reshape(4,4))
+ print m5
+
+The inverse of a matrix A, A raise to minus one is also called the
+reciprocal matrix such that A multiplied by A inverse will give 1. The
+Euclidean norm or the Frobenius norm of a matrix is defined as square
+root of sum of squares of elements in the matrix. Pause here and try
+to solve the problem yourself, the inverse of a matrix can be found
+using the function ``inv(A)``.
+
+And here is the solution, first let us find the inverse of matrix m5.
+::
+
+ im5 = inv(m5)
+
+And the euclidean norm of the matrix ``im5`` can be found out as,
+::
+
+ sum = 0
+ for each in array(im5.flatten())[0]:
+ sum += each * each
+ print sqrt(sum)
+
+{{{ switch to next slide, infinity norm }}}
+
+Now try to find out the infinity norm of the matrix im5. The infinity
+norm of a matrix is defined as the maximum value of sum of the
+absolute of elements in each row. Pause here and try to solve the
+problem yourself.
+
+The solution for the problem is,
+::
+
+ sum_rows = []
+ for i in im5:
+ sum_rows.append(abs(i).sum())
+ print max(sum_rows)
+
+{{{ switch to slide the ``norm()`` method }}}
+
+Well! to find the Euclidean norm and Infinity norm we have an even easier
+method, and let us see that now.
+
+The norm of a matrix can be found out using the method
+``norm()``. Inorder to find out the Euclidean norm of the matrix im5,
+we do,
+::
+
+ norm(im5)
+
+And to find out the Infinity norm of the matrix im5, we do,
+::
+
+ norm(im5,ord=inf)
+
+This is easier when compared to the code we wrote. Do ``norm``
+question mark to read up more about ord and the possible type of norms
+the norm function produces.
+
+{{{ switch to next slide, determinant }}}
+
+Now let us find out the determinant of a the matrix m5.
+
+The determinant of a square matrix can be obtained using the function
+``det()`` and the determinant of m5 can be found out as,
+::
+
+ det(m5)
+
+{{{ switch to next slide, eigen vectors and eigen values }}}
+
+The eigen values and eigen vector of a square matrix can be computed
+using the function ``eig()`` and ``eigvals()``.
+
+Let us find out the eigen values and eigen vectors of the matrix
+m5. We can do it as,
+::
+
+ eig(m5)
+
+Note that it returned a tuple of two matrices. The first element in
+the tuple are the eigen values and the second element in the tuple are
+the eigen vectors. Thus the eigen values are,
+::
+
+ eig(m5)[0]
+
+and the eigen vectors are,
+::
+
+ eig(m5)[1]
+
+The eigen values can also be computed using the function ``eigvals()`` as,
+::
+
+ eigvals(m5)
+
+{{{ switch to next slide, singular value decomposition }}}
+
+Now let us learn how to do the singular value decomposition or S V D
+of a matrix.
+
+Suppose M is an m×n matrix whose entries come from the field K, which
+is either the field of real numbers or the field of complex
+numbers. Then there exists a factorization of the form
+
+ M = U\Sigma V star
+
+where U is an (m by m) unitary matrix over K, the matrix \Sigma is an
+(m by n) diagonal matrix with nonnegative real numbers on the
+diagonal, and V*, an (n by n) unitary matrix over K, denotes the
+conjugate transpose of V. Such a factorization is called the
+singular-value decomposition of M.
+
+The SVD of matrix m5 can be found as
+::
+
+ svd(m5)
+
+Notice that it returned a tuple of 3 elements. The first one U the
+next one Sigma and the third one V star.
+
+{{{ switch to next slide, recap slide }}}
+
+So this brings us to the end of this tutorial. In this tutorial, we
+learned about matrices, creating matrices, matrix operations, inverse
+of matrices, determinant, norm, eigen values and vectors and singular
+value decomposition of matrices.
+
+{{{ switch to next slide, thank you }}}
+
+Thank you!
+
+.. Author: Anoop Jacob Thomas <anoop@fossee.in>
+ Reviewer 1:
+ Reviewer 2:
+ External reviewer:
diff --git a/other_type_of_plots.rst b/other-type-of-plots/script.rst
index 010045b..010045b 100644
--- a/other_type_of_plots.rst
+++ b/other-type-of-plots/script.rst
diff --git a/other_types_of_plots.rst b/other_types_of_plots.rst
deleted file mode 100644
index 010045b..0000000
--- a/other_types_of_plots.rst
+++ /dev/null
@@ -1,220 +0,0 @@
-.. 2.4 LO: other types of plots (3) [anoop]
-.. -----------------------------------------
-.. * scatter
-.. * pie chart
-.. * bar chart
-.. * log
-.. * illustration of other plots, matplotlib help
-
-===================
-Other type of plots
-===================
-
-{{{ show the first slide }}}
-
-Hello and welcome to the tutorial other type of plots.
-
-{{{ show the outline slide }}}
-
-In this tutorial we will cover scatter plot, pie chart, bar chart and
-log plot. We will also see few other plots and also introduce you to
-the matplotlib help.
-
-
-Let us start with scatter plot.
-
-{{{ switch to the next slide }}}
-
-In a scatter plot, the data is displayed as a collection of points,
-each having the value of one variable determining the position on the
-horizontal axis and the value of the other variable determining the
-position on the vertical axis. This kind of plot is also called a
-scatter chart, scatter diagram and scatter graph.
-
-Before we proceed further get your IPython interpreter running with
-the ``-pylab`` option. Start your IPython interpreter as
-::
-
- ipython -pylab
-
-{{{ open the ipython interpreter in the terminal using the command
-ipython -pylab }}}
-
-{{{ switch to the next slide having the problem statement of first
-exercise }}}
-
-Now, let us plot a scatter plot showing the percentage profit of company A
-from the year 2000-2010. The data for the same is available in the
-file ``company-a-data.txt``.
-
-{{{ open the file company-a-data.txt and show the content }}}
-
-The data file has two lines with a set of values in each line, the
-first line representing years and the second line representing the
-profit percentages.
-
-{{{ close the file and switch to the terminal }}}
-
-To product the scatter plot first we need to load the data from the
-file using ``loadtxt``. We learned in one of the previous sessions,
-and it can be done as ::
-
- year,profit = loadtxt('/home/fossee/other-plot/company-a-data.txt',dtype=type(int()))
-
-Now in-order to generate the scatter graph we will use the function
-``scatter()``
-::
-
- scatter(year,profit)
-
-Notice that we passed two arguments to ``scatter()`` function, first
-one the values in x-coordinate, year, and the other the values in
-y-coordinate, the profit percentage.
-
-{{{ switch to the next slide which has the problem statement of
-problem to be tried out }}}
-
-Now here is a question for you to try out, plot the same data with red
-diamonds.
-
-**Clue** - *try scatter? in your ipython interpreter*
-
-.. scatter(year,profit,color='r',marker='d')
-
-Now let us move on to pie chart.
-
-{{{ switch to the slide which says about pie chart }}}
-
-A pie chart or a circle graph is a circular chart divided into
-sectors, illustrating proportion.
-
-{{{ switch to the slide showing the problem statement of second
-exercise question }}}
-
-Plot a pie chart representing the profit percentage of company A, with
-the same data from file ``company-a-data.txt``. So let us reuse the
-data we have loaded from the file previously.
-
-We can plot the pie chart using the function ``pie()``.
-::
-
- pie(profit,labels=year)
-
-Notice that we passed two arguments to the function ``pie()``. The
-first one the values and the next one the set of labels to be used in
-the pie chart.
-
-{{{ switch to the next slide which has the problem statement of
-problem to be tried out }}}
-
-Now here is a question for you to try out, plot a pie chart with the
-same data with colors for each wedges as white, red, black, magenta,
-yellow, blue, green, cyan, yellow, magenta and blue respectively.
-
-**Clue** - *try pie? in your ipython interpreter*
-
-.. pie(t,labels=s,colors=('w','r','k','m','y','b','g','c','y','m','b'))
-
-{{{ switch to the slide which says about bar chart }}}
-
-Now let us move on to bar chart. A bar chart or bar graph is a chart
-with rectangular bars with lengths proportional to the values that
-they represent.
-
-{{{ switch to the slide showing the problem statement of third
-exercise question }}}
-
-Plot a bar chart representing the profit percentage of company A, with
-the same data from file ``company-a-data.txt``.
-
-So let us reuse the data we have loaded from the file previously.
-
-We can plot the bar chart using the function ``bar()``.
-::
-
- bar(year,profit)
-
-Note that the function ``bar()`` needs at least two arguments one the
-values in x-coordinate and the other values in y-coordinate which is
-used to determine the height of the bars.
-
-{{{ switch to the next slide which has the problem statement of
-problem to be tried out }}}
-
-Now here is a question for you to try, plot a bar chart which is not
-filled and which is hatched with 45\ :sup:`o` slanting lines as shown
-in the image in the slide.
-
-**Clue** - *try bar? in your ipython interpreter*
-
-.. bar(year,profit,fill=False,hatch='/')
-
-{{{ switch to the slide which says about bar chart }}}
-
-Now let us move on to log-log plot. A log-log graph or log-log plot is
-a two-dimensional graph of numerical data that uses logarithmic scales
-on both the horizontal and vertical axes. Because of the nonlinear
-scaling of the axes, a function of the form y = ax\ :sup:`b` will
-appear as a straight line on a log-log graph
-
-{{{ switch to the slide showing the problem statement of fourth
-exercise question }}}
-
-
-Plot a `log-log` chart of y=5*x\ :sup:`3` for x from 1-20.
-
-Before we actually plot let us calculate the points needed for
-that. And it could be done as,
-::
-
- x = linspace(1,20,100)
- y = 5*x**3
-
-Now we can plot the log-log chart using ``loglog()`` function,
-::
-
- loglog(x,y)
-
-To understand the difference between a normal ``plot`` and a ``log-log
-plot`` let us create another plot using the function ``plot``.
-::
-
- figure(2)
- plot(x,y)
-
-{{{ show both the plots side by side }}}
-
-So that was ``log-log() plot``.
-
-{{{ switch to the next slide which says: "How to get help on
-matplotlib online"}}}
-
-Now we will see few more plots and also see how to access help of
-matplotlib over the internet.
-
-Help about matplotlib can be obtained from
-matplotlib.sourceforge.net/contents.html
-
-.. #[[Anoop: I am not so sure how to do the rest of it, so I guess we
- can just browse through the side and tell them few. What is your
- opinion??]]
-
-Now let us see few plots from
-matplotlib.sourceforge.net/users/screenshots.html
-
-{{{ browse through the site quickly }}}
-
-{{{ switch to recap slide }}}
-
-Now we have come to the end of this tutorial. We have covered scatter
-plot, pie chart, bar chart, log-log plot and also saw few other plots
-and covered how to access the matplotlib online help.
-
-{{{ switch to the thank you slide }}}
-
-Thank you!
-
-.. Author: Anoop Jacob Thomas <anoop@fossee.in>
- Reviewer 1:
- Reviewer 2:
- External reviewer:
diff --git a/savefig.rst b/savefig/script.rst
index 013ef06..506428e 100644
--- a/savefig.rst
+++ b/savefig/script.rst
@@ -1,3 +1,10 @@
+.. 2.5 LO: saving plots (2)
+.. -------------------------
+.. * Outline
+.. + basic savefig
+.. + png, pdf, ps, eps, svg
+.. + going to OS and looking at the file
+
=======
Savefig
=======
@@ -60,7 +67,7 @@ file browser.
{{{ Open the browser, navigate to /home/fossee and highlight the file
sine.png }}}
-Yes, the file ``sine.png`` is here and let us check the it.
+Yes, the file ``sine.png`` is here and let us check it.
{{{ Open the file sine.png and show it for two-three seconds and then
close it and return to IPython interpreter, make sure the plot window
@@ -72,7 +79,7 @@ format, ps - post script, eps - encapsulated post script, svg -
scalable vector graphics, png - portable network graphics which
support transparency etc.
-#[slide must give the extensions for the files - Anoop]
+.. #[[slide must give the extensions for the files - Anoop]]
Let us now try to save the plot in eps format. ``eps`` stands for
encapsulated post script, and it can be embedded in your latex
@@ -80,8 +87,8 @@ documents.
{{{ Switch focus to the already open plot window }}}
-We still have the old sine plot with us, and now let us save the plot
-as ``sine.eps``.
+We still have the sine plot with us, and now let us save the plot as
+``sine.eps``.
{{{ Switch focus to IPython interpreter }}}
diff --git a/using python modules/four_plot.png b/using python modules/four_plot.png
new file mode 100644
index 0000000..00a3a7a
--- /dev/null
+++ b/using python modules/four_plot.png
Binary files differ
diff --git a/using python modules/four_plot.py b/using python modules/four_plot.py
new file mode 100644
index 0000000..b158717
--- /dev/null
+++ b/using python modules/four_plot.py
@@ -0,0 +1,11 @@
+x=linspace(-5*pi, 5*pi, 500)
+plot(x, x, 'b')
+plot(x, -x, 'b')
+plot(x, sin(x), 'g', linewidth=2)
+plot(x, x*sin(x), 'r', linewidth=3)
+legend(['x', '-x', 'sin(x)', 'xsin(x)'])
+annotate('origin', xy = (0, 0))
+title('Four Plot')
+xlim(-5*pi, 5*pi)
+ylim(-5*pi, 5*pi)
+#show()
diff --git a/using python modules/script.rst b/using python modules/script.rst
new file mode 100644
index 0000000..aa00863
--- /dev/null
+++ b/using python modules/script.rst
@@ -0,0 +1,227 @@
+.. 9.3 LO: using python modules (3)
+.. ---------------------------------
+.. * executing python scripts from command line
+.. * import
+.. * scipy
+.. * pylab
+.. * sys
+.. * STDLIB modules show off
+
+====================
+Using Python modules
+====================
+{{{ show the welcome slide }}}
+
+Welcome to the spoken tutorial on using python modules.
+
+{{{ switch to next slide, outline slide }}}
+
+In this tutorial, we will see how to run python scripts from command
+line, importing modules, importing scipy and pylab modules.
+
+{{{ switch to next slide on executing python scripts from command line }}}
+
+Let us create a simple python script to print hello world. Open your
+text editor and type the following,
+
+{{{ open the text editor and type the following }}}
+::
+
+ print "Hello world!"
+ print
+
+and save the script as hello.py,
+
+{{{ save the script as hello.py }}}
+
+Till now we saw how to run a script using the IPython interpreter
+using the
+::
+
+ %run -i hello.py
+
+option, but that is not the correct way of running a python
+script.
+
+The correct method is to run it using the Python interpreter. Open the
+terminal and navigate to the directory where hello.py is,
+
+{{{ open terminal and navigate to directory where hello.py was saved }}}
+
+now run the Python script as,
+::
+
+ python hello.py
+
+It executed the script and we got the output ``Hello World!``.
+
+{{{ highlight ``python filename`` syntax on slide while narrating }}}
+
+The syntax is python space filename.
+
+Now recall the four plot problem where we plotted four plots in a single
+figure. Let us run that script from command line.
+
+If you don't have the script,
+
+{{{ open the four_plot.py file in text editor }}}
+
+just pause here and create a python script with the following lines
+and save it as four_plot.py.
+
+Now let us run four_plot.py as a python script.
+::
+
+ python four_plot.py
+
+Oops! even though it was supposed to work, it didn't. It gave an error
+``linspace()`` is not defined, which means that the function
+``linspace()`` is not available in the current name-space.
+
+But if you try to run the same script using ``%run -i four_plot.py``
+in your IPython interpreter started with the option ``-pylab`` it will
+work, because the ``-pylab`` option does some work for us by importing
+the required modules to our name-space when ipython interpreter
+starts. And thus we don't have to explicitly import modules.
+
+So now let us try to fix the problem and run the script in command
+line,
+
+add the following line as the first line in the script,
+{{{ add the line as first line in four_plot.py and save }}}
+::
+
+ from scipy import *
+
+Now let us run the script again,
+::
+
+ python four_plot.py
+
+Now it gave another error plot not defined, let us edit the file again
+and add the line below the line we just added,
+{{{ add the line as second line in four_plot.py and save }}}
+::
+
+ from pylab import *
+
+And run the script,
+::
+
+ python four_plot.py
+
+Yes! it worked. So what did we do?
+
+We actually imported the required modules using the keyword ``import``.
+It could have also be done as,
+
+{{{ highlight the following in slide and say it loud }}}
+::
+
+ from scipy import linspace
+
+instead of,
+::
+
+ from scipy import *
+
+So in practice it is always good to use function names instead of
+asterisk or star. As if we use asterisk to import from a particular
+module then it will replace any existing functions with the same name
+in our name-space.
+
+So let us modify four_plot.py as,
+{{{ delete the first two lines and add the following }}}
+::
+
+ from scipy import linspace, pi, sin
+ from pylab import plot, legend, annotate, title, show
+ from pylab import xlim, ylim
+
+{{{ switch to next slide }}}
+it could also be done as,
+
+.. import scipy
+.. import pylab
+.. x = scipy.linspace(-5*scipy.pi, 5*scipy.pi, 500)
+.. pylab.plot(x, x, 'b')
+.. pylab.plot(x, -x, 'b')
+.. pylab.plot(x, scipy.sin(x), 'g', linewidth=2)
+.. pylab.plot(x, x*scipy.sin(x), 'r', linewidth=3)
+.. pylab.legend(['x', '-x', 'sin(x)', 'xsin(x)'])
+.. pylab.annotate('origin', xy = (0, 0))
+.. pylab.xlim(-5*scipy.pi, 5*scipy.pi)
+.. pylab.ylim(-5*scipy.pi, 5*scipy.pi)
+
+
+Notice that we use ``scipy.pi`` instead of just ``pi`` as in the
+previous method, and the functions are called as ``pylab.plot()`` and
+``pylab.annotate()`` and not as ``plot()`` and ``annotate()``.
+
+{{{ switch to next slide, problem statement }}}
+
+Write a script to plot a sine wave from minus two pi to two pi.
+
+Pause here and try to solve the problem yourself before looking at the
+solution.
+
+It can solved as,
+
+{{{ open sine.py and show it }}}
+
+the first line we import the required functions ``linspace()`` and
+``sin()`` and constant ``pi`` from the module scipy. the second and
+third line we import the functions ``plot()``, ``legend()``,
+``show()``, ``title()``, ``xlabel()`` and ``ylabel()``. And the rest
+the code to generate the plot.
+
+We can run it as,
+{{{ now switch focus to terminal and run the script }}}
+::
+
+ python sine.py
+
+{{{ switch to next slide, What is a module? }}}
+
+So till now we have been learning about importing modules, now what is
+a module?
+
+A module is simply a file containing Python definitions and
+statements. Definitions from a module can be imported into other
+modules or into the main module.
+
+{{{ switch to next slide, Python standard library }}}
+
+Python has a very rich standard library of modules
+
+Python's standard library is very extensive, offering a wide range of
+facilities. Some of the standard modules are,
+
+for Math: math, random
+for Internet access: urllib2, smtplib
+for System, Command line arguments: sys
+for Operating system interface: os
+for regular expressions: re
+for compression: gzip, zipfile, tarfile
+And there are lot more.
+
+Find more information at Python Library reference,
+``http://docs.python.org/library/``
+
+The modules pylab, scipy, Mayavi are not part of the standard python
+library.
+
+{{{ switch to next slide, recap }}}
+
+This brings us to the end of this tutorial, in this tutorial we
+learned running scripts from command line, learned about modules, saw
+the python standard library.
+
+{{{ switch to next slide, thank you slide }}}
+
+Thank you!
+
+.. Author: Anoop Jacob Thomas <anoop@fossee.in>
+ Reviewer 1:
+ Reviewer 2:
+ External reviewer:
diff --git a/using python modules/sine.py b/using python modules/sine.py
new file mode 100644
index 0000000..109308e
--- /dev/null
+++ b/using python modules/sine.py
@@ -0,0 +1,11 @@
+from scipy import linspace, pi, sin
+from pylab import plot, legend, show, title
+from pylab import xlabel, ylabel
+
+x = linspace(-2*pi,2*pi,100)
+plot(x,sin(x))
+legend(['sin(x)'])
+title('Sine plot')
+xlabel('x')
+ylabel('sin(x)')
+show()