summaryrefslogtreecommitdiff
path: root/advanced_python
diff options
context:
space:
mode:
Diffstat (limited to 'advanced_python')
-rw-r--r--advanced_python/data/company-a-data.txt2
-rw-r--r--advanced_python/data/error-bar.pngbin0 -> 27326 bytes
-rw-r--r--advanced_python/images/L-Tsq.pngbin0 -> 27326 bytes
-rw-r--r--advanced_python/images/bar.pngbin0 -> 14049 bytes
-rw-r--r--advanced_python/images/cosine.pngbin0 -> 47888 bytes
-rw-r--r--advanced_python/images/loglog.pngbin0 -> 20208 bytes
-rw-r--r--advanced_python/images/lst-sq-fit.pngbin0 -> 30317 bytes
-rw-r--r--advanced_python/images/overlaid.pngbin0 -> 65633 bytes
-rw-r--r--advanced_python/images/pie.pngbin0 -> 49559 bytes
-rw-r--r--advanced_python/images/plot.pngbin0 -> 26100 bytes
-rw-r--r--advanced_python/images/scatter.pngbin0 -> 14917 bytes
-rw-r--r--advanced_python/images/sine.pngbin0 -> 42508 bytes
-rw-r--r--advanced_python/images/slice.pngbin0 -> 12825 bytes
-rw-r--r--advanced_python/images/squares.pngbin0 -> 480 bytes
-rw-r--r--advanced_python/images/subplot.pngbin0 -> 41041 bytes
-rw-r--r--advanced_python/slides/arrays.tex167
-rw-r--r--advanced_python/slides/modules.tex12
-rw-r--r--advanced_python/slides/plotting.tex206
18 files changed, 225 insertions, 162 deletions
diff --git a/advanced_python/data/company-a-data.txt b/advanced_python/data/company-a-data.txt
new file mode 100644
index 0000000..06f4ca4
--- /dev/null
+++ b/advanced_python/data/company-a-data.txt
@@ -0,0 +1,2 @@
+2.000000000000000000e+03 2.001000000000000000e+03 2.002000000000000000e+03 2.003000000000000000e+03 2.004000000000000000e+03 2.005000000000000000e+03 2.006000000000000000e+03 2.007000000000000000e+03 2.008000000000000000e+03 2.009000000000000000e+03 2.010000000000000000e+03
+2.300000000000000000e+01 5.500000000000000000e+01 3.200000000000000000e+01 6.500000000000000000e+01 8.800000000000000000e+01 5.000000000000000000e+00 1.400000000000000000e+01 6.700000000000000000e+01 2.300000000000000000e+01 2.300000000000000000e+01 1.200000000000000000e+01
diff --git a/advanced_python/data/error-bar.png b/advanced_python/data/error-bar.png
new file mode 100644
index 0000000..cc1a454
--- /dev/null
+++ b/advanced_python/data/error-bar.png
Binary files differ
diff --git a/advanced_python/images/L-Tsq.png b/advanced_python/images/L-Tsq.png
new file mode 100644
index 0000000..cc1a454
--- /dev/null
+++ b/advanced_python/images/L-Tsq.png
Binary files differ
diff --git a/advanced_python/images/bar.png b/advanced_python/images/bar.png
new file mode 100644
index 0000000..cee54fb
--- /dev/null
+++ b/advanced_python/images/bar.png
Binary files differ
diff --git a/advanced_python/images/cosine.png b/advanced_python/images/cosine.png
new file mode 100644
index 0000000..2b15a32
--- /dev/null
+++ b/advanced_python/images/cosine.png
Binary files differ
diff --git a/advanced_python/images/loglog.png b/advanced_python/images/loglog.png
new file mode 100644
index 0000000..824b9f1
--- /dev/null
+++ b/advanced_python/images/loglog.png
Binary files differ
diff --git a/advanced_python/images/lst-sq-fit.png b/advanced_python/images/lst-sq-fit.png
new file mode 100644
index 0000000..7e9dfe2
--- /dev/null
+++ b/advanced_python/images/lst-sq-fit.png
Binary files differ
diff --git a/advanced_python/images/overlaid.png b/advanced_python/images/overlaid.png
new file mode 100644
index 0000000..41e1891
--- /dev/null
+++ b/advanced_python/images/overlaid.png
Binary files differ
diff --git a/advanced_python/images/pie.png b/advanced_python/images/pie.png
new file mode 100644
index 0000000..7791e54
--- /dev/null
+++ b/advanced_python/images/pie.png
Binary files differ
diff --git a/advanced_python/images/plot.png b/advanced_python/images/plot.png
new file mode 100644
index 0000000..89cbc46
--- /dev/null
+++ b/advanced_python/images/plot.png
Binary files differ
diff --git a/advanced_python/images/scatter.png b/advanced_python/images/scatter.png
new file mode 100644
index 0000000..66bd5d8
--- /dev/null
+++ b/advanced_python/images/scatter.png
Binary files differ
diff --git a/advanced_python/images/sine.png b/advanced_python/images/sine.png
new file mode 100644
index 0000000..7667886
--- /dev/null
+++ b/advanced_python/images/sine.png
Binary files differ
diff --git a/advanced_python/images/slice.png b/advanced_python/images/slice.png
new file mode 100644
index 0000000..7f53d2a
--- /dev/null
+++ b/advanced_python/images/slice.png
Binary files differ
diff --git a/advanced_python/images/squares.png b/advanced_python/images/squares.png
new file mode 100644
index 0000000..ee102e6
--- /dev/null
+++ b/advanced_python/images/squares.png
Binary files differ
diff --git a/advanced_python/images/subplot.png b/advanced_python/images/subplot.png
new file mode 100644
index 0000000..2c1a662
--- /dev/null
+++ b/advanced_python/images/subplot.png
Binary files differ
diff --git a/advanced_python/slides/arrays.tex b/advanced_python/slides/arrays.tex
index 7b829f9..e4501c8 100644
--- a/advanced_python/slides/arrays.tex
+++ b/advanced_python/slides/arrays.tex
@@ -7,49 +7,48 @@
\item Much faster than arrays
\end{itemize}
\begin{lstlisting}
- a1 = array([1,2,3,4])
- a1 # 1-D
- a2 = array([[1,2,3,4],[5,6,7,8]])
- a2 # 2-D
+ In[]: a1 = array([1,2,3,4])
+ In[]: a1 # 1-D
+ In[]: a2 = array([[1,2,3,4],[5,6,7,8]])
+ In[]: a2 # 2-D
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{arange} and \texttt{shape}}
\begin{lstlisting}
- ar1 = arange(1, 5)
-
- ar2 = arange(1, 9)
- print ar2
- ar2.shape = 2, 4
- print ar2
+ In[]: ar1 = arange(1, 5)
+ In[]: ar2 = arange(1, 9)
+ In[]: print ar2
+ In[]: ar2.shape = 2, 4
+ In[]: print ar2
\end{lstlisting}
\begin{itemize}
\item \texttt{linspace} and \texttt{loadtxt} also returned arrays
\end{itemize}
\begin{lstlisting}
- ar1.shape
- ar2.shape
+ In[]: ar1.shape
+ In[]: ar2.shape
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Special methods}
\begin{lstlisting}
- identity(3)
+ In[]: identity(3)
\end{lstlisting}
\begin{itemize}
\item array of shape (3, 3) with diagonals as 1s, rest 0s
\end{itemize}
\begin{lstlisting}
- zeros((4,5))
+ In[]: zeros((4,5))
\end{lstlisting}
\begin{itemize}
\item array of shape (4, 5) with all 0s
\end{itemize}
\begin{lstlisting}
- a = zeros_like([1.5, 1, 2, 3])
- print a, a.dtype
+ In[]: a = zeros_like([1.5, 1, 2, 3])
+ In[]: print a, a.dtype
\end{lstlisting}
\begin{itemize}
\item An array with all 0s, with similar shape and dtype as argument
@@ -61,17 +60,17 @@
\begin{frame}[fragile]
\frametitle{Operations on arrays}
\begin{lstlisting}
- a1
- a1 * 2
- a1
+ In[]: a1
+ In[]: a1 * 2
+ In[]: a1
\end{lstlisting}
\begin{itemize}
\item The array is not changed; New array is returned
\end{itemize}
\begin{lstlisting}
- a1 + 3
- a1 - 7
- a1 / 2.0
+ In[]: a1 + 3
+ In[]: a1 - 7
+ In[]: a1 / 2.0
\end{lstlisting}
\end{frame}
@@ -81,25 +80,25 @@
\item Like lists, we can assign the new array, the old name
\end{itemize}
\begin{lstlisting}
- a1 = a1 + 2
- a1
+ In[]: a1 = a1 + 2
+ In[]: a1
\end{lstlisting}
\begin{itemize}
\item \alert{Beware of Augmented assignment!}
\end{itemize}
\begin{lstlisting}
- a, b = arange(1, 5), arange(1, 5)
- print a, a.dtype, b, b.dtype
- a = a/2.0
- b /= 2.0
- print a, a.dtype, b, b.dtype
+ In[]: a, b = arange(1, 5), arange(1, 5)
+ In[]: print a, a.dtype, b, b.dtype
+ In[]: a = a/2.0
+ In[]: b /= 2.0
+ In[]: print a, a.dtype, b, b.dtype
\end{lstlisting}
\begin{itemize}
\item Operations on two arrays; element-wise
\end{itemize}
\begin{lstlisting}
- a1 + a1
- a1 * a2
+ In[]: a1 + a1
+ In[]: a1 * a2
\end{lstlisting}
\end{frame}
@@ -108,24 +107,24 @@
\begin{frame}[fragile]
\frametitle{Accessing \& changing elements}
\begin{lstlisting}
- A = array([12, 23, 34, 45, 56])
+ In[]: A = array([12, 23, 34, 45, 56])
- C = array([[11, 12, 13, 14, 15],
- [21, 22, 23, 24, 25],
- [31, 32, 33, 34, 35],
- [41, 42, 43, 44, 45],
- [51, 52, 53, 54, 55]])
+ In[]: C = array([[11, 12, 13, 14, 15],
+ [21, 22, 23, 24, 25],
+ [31, 32, 33, 34, 35],
+ [41, 42, 43, 44, 45],
+ [51, 52, 53, 54, 55]])
- A[2]
- C[2, 3]
+ In[]: A[2]
+ In[]: C[2, 3]
\end{lstlisting}
\begin{itemize}
\item Indexing starts from 0
\item Assign new values, to change elements
\end{itemize}
\begin{lstlisting}
- A[2] = -34
- C[2, 3] = -34
+ In[]: A[2] = -34
+ In[]: C[2, 3] = -34
\end{lstlisting}
\end{frame}
@@ -135,49 +134,49 @@
\item Indexing works just like with lists
\end{itemize}
\begin{lstlisting}
- C[2]
- C[4]
- C[-1]
+ In[]: C[2]
+ In[]: C[4]
+ In[]: C[-1]
\end{lstlisting}
\begin{itemize}
\item Change the last row into all zeros
\end{itemize}
\begin{lstlisting}
- C[-1] = [0, 0, 0, 0, 0]
+ In[]: C[-1] = [0, 0, 0, 0, 0]
\end{lstlisting}
OR
\begin{lstlisting}
- C[-1] = 0
+ In[]: C[-1] = 0
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Accessing columns}
\begin{lstlisting}
- C[:, 2]
- C[:, 4]
- C[:, -1]
+ In[]: C[:, 2]
+ In[]: C[:, 4]
+ In[]: C[:, -1]
\end{lstlisting}
\begin{itemize}
\item The first parameter is replaced by a \texttt{:} to specify we
require all elements of that dimension
\end{itemize}
\begin{lstlisting}
- C[:, -1] = 0
+ In[]: C[:, -1] = 0
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Slicing}
\begin{lstlisting}
- I = imread('squares.png')
- imshow(I)
+ In[]: I = imread('squares.png')
+ In[]: imshow(I)
\end{lstlisting}
\begin{itemize}
\item The image is just an array
\end{itemize}
\begin{lstlisting}
- print I, I.shape
+ In[]: print I, I.shape
\end{lstlisting}
\begin{enumerate}
\item Get the top left quadrant of the image
@@ -191,17 +190,24 @@
\item Slicing works just like with lists
\end{itemize}
\begin{lstlisting}
- C[0:3, 2]
- C[2, 0:3]
- C[2, :3]
+ In[]: C[0:3, 2]
+ In[]: C[2, 0:3]
+ In[]: C[2, :3]
\end{lstlisting}
\begin{lstlisting}
- imshow(I[:150, :150])
+ In[]: imshow(I[:150, :150])
- imshow(I[75:225, 75:225])
+ In[]: imshow(I[75:225, 75:225])
\end{lstlisting}
\end{frame}
+\begin{frame}
+\frametitle{Image after slicing}
+\includegraphics[scale=0.45]{../advanced_python/images/slice.png}\\
+\end{frame}
+
+
+
\begin{frame}[fragile]
\frametitle{Striding}
\begin{itemize}
@@ -211,15 +217,15 @@
\item The idea is similar to striding in lists
\end{itemize}
\begin{lstlisting}
- C[0:5:2, 0:5:2]
- C[::2, ::2]
- C[1::2, ::2]
+ In[]: C[0:5:2, 0:5:2]
+ In[]: C[::2, ::2]
+ In[]: C[1::2, ::2]
\end{lstlisting}
\begin{itemize}
\item Now, the image can be shrunk by
\end{itemize}
\begin{lstlisting}
- imshow(I[::2, ::2])
+ In[]: imshow(I[::2, ::2])
\end{lstlisting}
\end{frame}
@@ -252,12 +258,13 @@
\begin{frame}[fragile]
\frametitle{Least Square Fit}
\begin{lstlisting}
- l, t = loadtxt("pendulum.txt", unpack=True)
- l
- t
- tsq = t * t
- plot(l, tsq, 'bo')
- plot(l, tsq, 'r')
+ In[]: L, t = loadtxt("pendulum.txt",
+ unpack=True)
+ In[]: L
+ In[]: t
+ In[]: tsq = t * t
+ In[]: plot(L, tsq, 'bo')
+ In[]: plot(L, tsq, 'r')
\end{lstlisting}
\begin{itemize}
\item Both the plots, aren't what we expect -- linear plot
@@ -295,22 +302,22 @@
\begin{frame}[fragile]
\frametitle{Least Square Fit Line}
\begin{lstlisting}
- A = array((l, ones_like(l)))
- A.T
- A
+ In[]: A = array((L, ones_like(L)))
+ In[]: A.T
+ In[]: A
\end{lstlisting}
\begin{itemize}
\item We now have \texttt{A} and \texttt{tsq}
\end{itemize}
\begin{lstlisting}
- result = lstsq(A, tsq)
+ In[]: result = lstsq(A, tsq)
\end{lstlisting}
\begin{itemize}
\item Result has a lot of values along with m and c, that we need
\end{itemize}
\begin{lstlisting}
- m, c = result[0]
- print m, c
+ In[]: m, c = result[0]
+ In[]: print m, c
\end{lstlisting}
\end{frame}
@@ -320,9 +327,15 @@
\item Now that we have m and c, we use them to generate line and plot
\end{itemize}
\begin{lstlisting}
- tsq_fit = m * l + c
- plot(l, tsq, 'bo')
- plot(l, tsq_fit, 'r')
+ In[]: tsq_fit = m * L + c
+ In[]: plot(L, tsq, 'bo')
+ In[]: plot(L, tsq_fit, 'r')
\end{lstlisting}
\end{frame}
+\begin{frame}
+\frametitle{Least Square Fit Line}
+\includegraphics[scale=0.45]{../advanced_python/images/lst-sq-fit.png}\\
+\end{frame}
+
+
diff --git a/advanced_python/slides/modules.tex b/advanced_python/slides/modules.tex
index 957789a..d6de640 100644
--- a/advanced_python/slides/modules.tex
+++ b/advanced_python/slides/modules.tex
@@ -6,13 +6,13 @@
\item Script to print `hello world' -- \texttt{hello.py}
\end{itemize}
\begin{lstlisting}
- print "Hello world!"
+ print "Hello world!"
\end{lstlisting}
\begin{itemize}
\item We have been running scripts from IPython
\end{itemize}
\begin{lstlisting}
- %run -i hello.py
+ In[]: %run -i hello.py
\end{lstlisting}
\begin{itemize}
\item Now, we run from the shell using python
@@ -28,9 +28,9 @@
\item Save the following in \texttt{sine\_plot.py}
\end{itemize}
\begin{lstlisting}
- x = linspace(-2*pi, 2*pi, 100)
- plot(x, sin(x))
- show()
+ x = linspace(-2*pi, 2*pi, 100)
+ plot(x, sin(x))
+ show()
\end{lstlisting}
\begin{itemize}
\item Now, let us run the script
@@ -51,7 +51,7 @@
\item Add the following to the top of your file
\end{itemize}
\begin{lstlisting}
- from scipy import *
+ from scipy import *
\end{lstlisting}
\begin{lstlisting}
$ python sine_plot.py
diff --git a/advanced_python/slides/plotting.tex b/advanced_python/slides/plotting.tex
index b8b4bce..fbc7aa2 100644
--- a/advanced_python/slides/plotting.tex
+++ b/advanced_python/slides/plotting.tex
@@ -9,8 +9,8 @@
$ ipython -pylab
\end{lstlisting} % $
\begin{lstlisting}
- p = linspace(-pi,pi,100)
- plot(p, cos(p))
+ In[]: p = linspace(-pi,pi,100)
+ In[]: plot(p, cos(p))
\end{lstlisting}
\end{frame}
@@ -20,11 +20,11 @@
\begin{itemize}
\item \texttt{p} has a hundred points in the range -pi to pi
\begin{lstlisting}
- print p[0], p[-1], len(p)
+ In[]: print p[0], p[-1], len(p)
\end{lstlisting}
\item Look at the doc-string of \texttt{linspace} for more details
\begin{lstlisting}
- linspace?
+ In[]: linspace?
\end{lstlisting}
\end{itemize}
\begin{itemize}
@@ -38,50 +38,51 @@
\begin{frame}[fragile]
\frametitle{Plot color and thickness}
\begin{lstlisting}
- clf()
- plot(p, sin(p), 'r')
+ In[]: clf()
+ In[]: plot(p, sin(p), 'r')
\end{lstlisting}
\begin{itemize}
\item Gives a sine curve in Red.
\end{itemize}
\begin{lstlisting}
- plot(p, cos(p), linewidth=2)
+ In[]: plot(p, cos(p), linewidth=2)
\end{lstlisting}
\begin{itemize}
\item Sets line thickness to 2
\end{itemize}
\begin{lstlisting}
- clf()
- plot(p, sin(p), '.')
+ In[]: clf()
+ In[]: plot(p, sin(p), '.')
\end{lstlisting}
\begin{itemize}
\item Produces a plot with only points
\end{itemize}
\begin{lstlisting}
- plot?
+ In[]: plot?
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{title}}
\begin{lstlisting}
- x = linspace(-2, 4, 50)
- plot(x, -x*x + 4*x - 5, 'r', linewidth=2)
- title("Parabolic function -x^2+4x-5")
+ In[]: x = linspace(-2, 4, 50)
+ In[]: plot(x, -x*x + 4*x - 5, 'r',
+ linewidth=2)
+ In[]: title("Parabolic function -x^2+4x-5")
\end{lstlisting}
\begin{itemize}
\item We can set title using \LaTeX~
\end{itemize}
\begin{lstlisting}
- title("Parabolic function $-x^2+4x-5$")
+ In[]: title("Parabolic function $-x^2+4x-5$")
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Axes labels}
\begin{lstlisting}
- xlabel("x")
- ylabel("f(x)")
+ In[]: xlabel("x")
+ In[]: ylabel("f(x)")
\end{lstlisting}
\begin{itemize}
\item We could, if required use \LaTeX~
@@ -91,7 +92,7 @@
\begin{frame}[fragile]
\frametitle{Annotate}
\begin{lstlisting}
- annotate("local maxima", xy=(2, -1))
+ In[]: annotate("local maxima", xy=(2, -1))
\end{lstlisting}
\begin{itemize}
\item First argument is the annotation text
@@ -103,8 +104,8 @@
\begin{frame}[fragile]
\frametitle{Limits of Plot area}
\begin{lstlisting}
- xlim()
- ylim()
+ In[]: xlim()
+ In[]: ylim()
\end{lstlisting}
\begin{itemize}
\item With no arguments, \texttt{xlim} \& \texttt{ylim} get the
@@ -112,13 +113,20 @@
\item New limits are set, when arguments are passed to them
\end{itemize}
\begin{lstlisting}
- xlim(-4, 5)
+ In[]: xlim(-4, 5)
\end{lstlisting}
\begin{lstlisting}
- ylim(-15, 2)
+ In[]: ylim(-15, 2)
\end{lstlisting}
\end{frame}
+\begin{frame}
+\frametitle{Plot}
+\includegraphics[scale=0.45]{../advanced_python/images/plot.png}\\
+\end{frame}
+
+
+
\section{Saving to Scripts}
\begin{frame}[fragile]
@@ -126,17 +134,17 @@
\begin{itemize}
\item To see the history of commands, we typed
\begin{lstlisting}
- %hist
+ In[]: %hist
\end{lstlisting}
\item All commands, valid or invalid, appear in the history
\item \texttt{\%hist} is a magic command, available only in IPython
\end{itemize}
\begin{lstlisting}
- %hist 5
+ In[]: %hist 5
# last 5 commands
\end{lstlisting}
\begin{lstlisting}
- %hist 5 10
+ In[]: %hist 5 10
# commands between 5 and 10
\end{lstlisting}
\end{frame}
@@ -150,7 +158,7 @@
\item \texttt{\%save} magic command to save the commands to a file
\end{itemize}
\begin{lstlisting}
- %save plot_script.py 1 3-6 8
+ In[]: %save plot_script.py 1 3-6 8
\end{lstlisting}
\begin{itemize}
\item File name must have a \texttt{.py} extension
@@ -160,7 +168,7 @@
\begin{frame}[fragile]
\frametitle{Running the script}
\begin{lstlisting}
- %run -i plot_script.py
+ In[]: %run -i plot_script.py
\end{lstlisting}
\begin{itemize}
\item There were no errors in the plot, but we don't see it!
@@ -168,7 +176,7 @@
\item We need to explicitly ask for the image to be shown
\end{itemize}
\begin{lstlisting}
- show()
+ In[]: show()
\end{lstlisting}
\begin{itemize}
\item \texttt{-i} asks the interpreter to check for names,
@@ -183,9 +191,9 @@
\begin{frame}[fragile]
\frametitle{\texttt{savefig}}
\begin{lstlisting}
- x = linspace(-3*pi,3*pi,100)
- plot(x,sin(x))
- savefig('sine.png')
+ In[]: x = linspace(-3*pi,3*pi,100)
+ In[]: plot(x,sin(x))
+ In[]: savefig('sine.png')
\end{lstlisting}
\begin{itemize}
\item \texttt{savefig} takes one argument
@@ -199,16 +207,16 @@
\begin{frame}[fragile]
\frametitle{Overlaid plots}
\begin{lstlisting}
- x = linspace(0, 50, 10)
- plot(x, sin(x))
+ In[]: x = linspace(0, 50, 10)
+ In[]: plot(x, sin(x))
\end{lstlisting}
\begin{itemize}
\item The curve isn't as smooth as we expected
\item We chose too few points in the interval
\end{itemize}
\begin{lstlisting}
- y = linspace(0, 50, 500)
- plot(y, sin(y))
+ In[]: y = linspace(0, 50, 500)
+ In[]: plot(y, sin(y))
\end{lstlisting}
\begin{itemize}
\item The plots are overlaid
@@ -219,39 +227,48 @@
\begin{frame}[fragile]
\frametitle{Legend}
\begin{lstlisting}
- legend(['sine-10 points', 'sine-500 points'])
+ In[]: legend(['sine-10 points',
+ 'sine-500 points'])
\end{lstlisting}
\begin{itemize}
\item Placed in the location, \texttt{pylab} thinks is `best'
\item \texttt{loc} parameter allows to change the location
\end{itemize}
\begin{lstlisting}
- legend(['sine-10 points', 'sine-500 points'],
+ In[]: legend(['sine-10 points',
+ 'sine-500 points'],
loc='center')
\end{lstlisting}
\end{frame}
+\begin{frame}
+\frametitle{Overlaid Plots}
+\includegraphics[scale=0.45]{../advanced_python/images/overlaid.png}\\
+\end{frame}
+
+
+
\begin{frame}[fragile]
\frametitle{Plotting in separate figures}
\begin{lstlisting}
- clf()
- x = linspace(0, 50, 500)
- figure(1)
- plot(x, sin(x), 'b')
- figure(2)
- plot(x, cos(x), 'g')
+ In[]: clf()
+ In[]: x = linspace(0, 50, 500)
+ In[]: figure(1)
+ In[]: plot(x, sin(x), 'b')
+ In[]: figure(2)
+ In[]: plot(x, cos(x), 'g')
\end{lstlisting}
\begin{itemize}
\item \texttt{figure} command allows us to have plots separately
\item It is also used to switch context between the plots
\end{itemize}
\begin{lstlisting}
- savefig('cosine.png')
- figure(1)
- title('sin(y)')
- savefig('sine.png')
- close()
- close()
+ In[]: savefig('cosine.png')
+ In[]: figure(1)
+ In[]: title('sin(y)')
+ In[]: savefig('sine.png')
+ In[]: close()
+ In[]: close()
\end{lstlisting}
\begin{itemize}
\item \texttt{close('all')} closes all the figures
@@ -261,7 +278,7 @@
\begin{frame}[fragile]
\frametitle{Subplots}
\begin{lstlisting}
- subplot(2, 1, 1)
+ In[]: subplot(2, 1, 1)
\end{lstlisting}
\begin{itemize}
\item number of rows
@@ -269,16 +286,23 @@
\item plot number, in serial order, to access or create
\end{itemize}
\begin{lstlisting}
- subplot(2, 1, 2)
- x = linspace(0, 50, 500)
- plot(x, cos(x))
+ In[]: subplot(2, 1, 2)
+ In[]: x = linspace(0, 50, 500)
+ In[]: plot(x, cos(x))
- subplot(2, 1, 1)
- y = linspace(0, 5, 100)
- plot(y, y ** 2)
+ In[]: subplot(2, 1, 1)
+ In[]: y = linspace(0, 5, 100)
+ In[]: plot(y, y ** 2)
\end{lstlisting}
\end{frame}
+\begin{frame}
+\frametitle{Subplots}
+\includegraphics[scale=0.45]{../advanced_python/images/subplot.png}\\
+\end{frame}
+
+
+
\section{Plotting Data}
\begin{frame}[fragile]
@@ -289,8 +313,8 @@
\item We read the data using \texttt{loadtxt}
\end{itemize}
\begin{lstlisting}
- primes = loadtxt('primes.txt')
- print primes
+ In[]: primes = loadtxt('primes.txt')
+ In[]: print primes
\end{lstlisting}
\begin{itemize}
\item \texttt{primes} is a sequence of floats
@@ -306,8 +330,8 @@
\item \texttt{loadtxt} requires both columns to be of same length
\end{itemize}
\begin{lstlisting}
- pend = loadtxt('pendulum.txt')
- print pend
+ In[]: pend = loadtxt('pendulum.txt')
+ In[]: print pend
\end{lstlisting}
\begin{itemize}
\item \texttt{pend} is not a simple sequence like \texttt{primes}
@@ -317,9 +341,10 @@
\begin{frame}[fragile]
\frametitle{Unpacking with \texttt{loadtxt}}
\begin{lstlisting}
- L, T = loadtxt('pendulum.txt', unpack=True)
- print L
- print T
+ In[]: L, T = loadtxt('pendulum.txt',
+ unpack=True)
+ In[]: print L
+ In[]: print T
\end{lstlisting}
\begin{itemize}
\item We wish to plot L vs. $T^2$
@@ -327,9 +352,9 @@
\item (We could instead iterate over T and calculate)
\end{itemize}
\begin{lstlisting}
- Tsq = square(T)
+ In[]: Tsq = square(T)
- plot(L, Tsq, '.')
+ In[]: plot(L, Tsq, '.')
\end{lstlisting}
\end{frame}
@@ -341,15 +366,21 @@
\item Read the values and make an error bar plot
\end{itemize}
\begin{lstlisting}
- L, T, L_err, T_err = \
- loadtxt('pendulum_error.txt', unpack=True)
- Tsq = square(T)
+ In[]: L, T, L_err, T_err = \
+ loadtxt('pendulum_error.txt',
+ unpack=True)
+ In[]: Tsq = square(T)
- errorbar(L, Tsq , xerr=L_err,
- yerr=T_err, fmt='b.')
+ In[]: errorbar(L, Tsq , xerr=L_err,
+ yerr=T_err, fmt='b.')
\end{lstlisting}
\end{frame}
+\begin{frame}
+\frametitle{Errorbar}
+\includegraphics[scale=0.45]{../advanced_python/images/L-Tsq.png}\\
+\end{frame}
+
\section{Other kinds of Plots}
\begin{frame}[fragile]
@@ -361,25 +392,36 @@
\item Let's plot the data of profits of a company
\end{itemize}
\begin{lstlisting}
- year, profit = loadtxt('company-a-data.txt',
- dtype=type(int()))
+ In[]: year, profit = loadtxt(
+ 'company-a-data.txt',
+ dtype=type(int()))
- scatter(year, profit)
+ In[]: scatter(year, profit)
\end{lstlisting}
\begin{itemize}
\item \alert{\texttt{dtype=int}; default is float}
\end{itemize}
\end{frame}
+\begin{frame}
+\frametitle{Scatter Plot}
+\includegraphics[scale=0.45]{../advanced_python/images/scatter.png}\\
+\end{frame}
+
\begin{frame}[fragile]
- \frametitle{Pie Chart \& Bar Chart}
+ \frametitle{Pie Chart}
\begin{lstlisting}
- pie(profit, labels=year)
+ In[]: pie(profit, labels=year)
\end{lstlisting}
+\includegraphics[scale=0.35]{../advanced_python/images/pie.png}\\
+\end{frame}
+\begin{frame}[fragile]
+ \frametitle{Bar Chart}
\begin{lstlisting}
- bar(year, profit)
+ In[]: bar(year, profit)
\end{lstlisting}
+\includegraphics[scale=0.35]{../advanced_python/images/bar.png}\\
\end{frame}
\begin{frame}[fragile]
@@ -388,11 +430,11 @@
\item Plot a \texttt{log-log} chart of $y=5x^3$ for x from 1 to 20
\end{itemize}
\begin{lstlisting}
- x = linspace(1,20,100)
- y = 5*x**3
+ In[]: x = linspace(1,20,100)
+ In[]: y = 5*x**3
- loglog(x, y)
- plot(x, y)
+ In[]: loglog(x, y)
+ In[]: plot(x, y)
\end{lstlisting}
\begin{itemize}
\item Look at \url{http://matplotlib.sourceforge.net/contents.html}
@@ -400,3 +442,9 @@
\end{itemize}
\end{frame}
+\begin{frame}
+\frametitle{Log-log plot Plot}
+\includegraphics[scale=0.45]{../advanced_python/images/loglog.png}\\
+\end{frame}
+
+