%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Tutorial slides on Python. % % Author: Prabhu Ramachandran % Copyright (c) 2005-2009, Prabhu Ramachandran %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \documentclass[14pt,compress]{beamer} %\documentclass[draft]{beamer} %\documentclass[compress,handout]{beamer} %\usepackage{pgfpages} %\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] % Modified from: generic-ornate-15min-45min.de.tex \mode { \usetheme{Warsaw} \useoutertheme{split} \setbeamercovered{transparent} } \usepackage[english]{babel} \usepackage[latin1]{inputenc} %\usepackage{times} \usepackage[T1]{fontenc} % Taken from Fernando's slides. \usepackage{ae,aecompl} \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} \definecolor{darkgreen}{rgb}{0,0.5,0} \usepackage{listings} \lstset{language=Python, basicstyle=\ttfamily\bfseries, commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, showstringspaces=false, keywordstyle=\color{blue}\bfseries} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Macros \setbeamercolor{emphbar}{bg=blue!20, fg=black} \newcommand{\emphbar}[1] {\begin{beamercolorbox}[rounded=true]{emphbar} {#1} \end{beamercolorbox} } \newcounter{time} \setcounter{time}{0} \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} \newcommand{\typ}[1]{\lstinline{#1}} \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } %%% This is from Fernando's setup. % \usepackage{color} % \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} % % Use and configure listings package for nicely formatted code % \usepackage{listings} % \lstset{ % language=Python, % basicstyle=\small\ttfamily, % commentstyle=\ttfamily\color{blue}, % stringstyle=\ttfamily\color{orange}, % showstringspaces=false, % breaklines=true, % postbreak = \space\dots % } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Title page \title[]{Matrices and Arrays\\ \& \\2D Plotting} \author[FOSSEE Team] {Asokan Pichai\\Prabhu Ramachandran} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} \date[] {11, October 2009} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} %\logo{\pgfuseimage{iitmlogo}} %% Delete this, if you do not want the table of contents to pop up at %% the beginning of each subsection: \AtBeginSubsection[] { \begin{frame} \frametitle{Outline} \tableofcontents[currentsection,currentsubsection] \end{frame} } \AtBeginSection[] { \begin{frame} \frametitle{Outline} \tableofcontents[currentsection,currentsubsection] \end{frame} } % If you wish to uncover everything in a step-wise fashion, uncomment % the following command: %\beamerdefaultoverlayspecification{<+->} %\includeonlyframes{current,current1,current2,current3,current4,current5,current6} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % DOCUMENT STARTS \begin{document} \begin{frame} \maketitle \end{frame} \begin{frame} \frametitle{Outline} \tableofcontents % You might wish to add the option [pausesections] \end{frame} \section{Matrices and Arrays} \subsection{Basic \typ{numpy} } \newcommand{\num}{\texttt{numpy}} \begin{frame} \frametitle{The \num\ module} \begin{itemize} \item Why? \item What: \begin{itemize} \item An efficient and powerful array type for various common data types \item Abstracts out the most commonly used standard operations on arrays \end{itemize} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Examples of \num} \begin{lstlisting} # Simple array math example >>> import numpy as np >>> a = np.array([1,2,3,4]) >>> b = np.arange(2,6) >>> b array([2,3,4,5]) >>> a*2 + b + 1 # Basic math! array([5, 8, 11, 14]) \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Examples of \num} \begin{lstlisting} # Pi and e are defined. >>> x = np.linspace(0.0, 10.0, 1000) >>> x *= 2*np.pi/10 # inplace. # apply functions to array. >>> y = np.sin(x) >>> z = np.exp(y) \end{lstlisting} \inctime{5} \end{frame} \begin{frame} \frametitle{Basic concepts} \begin{itemize} \item fixed size (\typ{arr.size}); \item Same type (\typ{arr.dtype}) of data \item arbitrary dimensionality \item \typ{arr.shape}: size in each dimension \item \alert{Note:} \typ{len(arr) != arr.size} in general \item \alert{Note:} By default array operations are performed \alert{elementwise} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{More examples of \num} \vspace*{-8pt} \begin{lstlisting} >>> x = np.array([1., 2, 3, 4]) >>> np.size(x) 4 >>> x.dtype # What is a.dtype? dtype('float64') >>> x.shape (4,) >>> print np.rank(x), x.itemsize 1 8 >>> x[0] = 10 >>> print x[0], x[-1] 10.0 4.0 \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Multi-dimensional arrays} \begin{lstlisting} >>> a = np.array([[ 0, 1, 2, 3], ... [10,11,12,13]]) >>> a.shape # (rows, columns) (2, 4) # Accessing and setting values >>> a[1,3] 13 >>> a[1,3] = -1 >>> a[1] # The second row array([10,11,12,-1]) \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Array math} \begin{itemize} \item Basic \alert{elementwise} math (given two arrays \typ{a, b}): \typ{+, -, *, /, \%} \item Inplace operators: \typ{a += b}, or \typ{np.add(a, b, a)} etc. \item \typ{np.sum(x, axis=0)}, \typ{np.product(x, axis=0)}, \typ{np.dot(a, bp)} \end{itemize} \begin{lstlisting} >>> b=np.array([[0,2,4,2],[1,2,3,4]]) >>> np.add(a,b,a) >>> np.sum(x,axis=1) \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Array math cont.} \begin{itemize} \item Logical operations: \typ{np.equal (==)}, \typ{np.not\_equal (!=)}, \typ{np.less (<)}, \typ{np.greater (>)} etc. \item Trig and other functions: \typ{np.sin(x),} \typ{np.arcsin(x), np.sinh(x),} \typ{np.exp(x), np.sqrt(x)} etc. \end{itemize} \begin{lstlisting} >>> np.greater(a,4) >>> np.sqrt(a) \end{lstlisting} \inctime{10} \end{frame} \subsection{Array Creation \& Slicing, Striding Arrays} \begin{frame}[fragile] \frametitle{Array creation functions} \begin{itemize} \item {\typ{np.array(object,dtype=None,...)} \begin{lstlisting} >>> np.array([2,3,4]) array([2, 3, 4]) \end{lstlisting} \item \typ{np.linspace(start,stop,...)} \begin{lstlisting} >>> np.linspace(0, 2, 4) array([0.,0.6666667,1.3333333,2.]) \end{lstlisting} \item Also try \typ{np.arange} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Array creation functions cont.} \begin{itemize} \item \typ{np.ones(shape, dtype=None, ...)} \begin{lstlisting} >>>np.ones([2,2]) array([[ 1., 1.], [ 1., 1.]]) \end{lstlisting} \item \typ{np.identity(n)} \item \typ{np.ones\_like(x)} \begin{lstlisting} >>>a = np.array([[1,2,3],[4,5,6]]) >>>np.ones_like(a) array([[1, 1, 1], [1, 1, 1]]) \end{lstlisting} \item Also try \typ{zeros, zeros\_like, empty} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Slicing arrays} \begin{lstlisting} >>> a = np.array([[1,2,3], [4,5,6], [7,8,9]]) >>> a[0,1:3] array([2, 3]) >>> a[1:,1:] array([[5, 6], [8, 9]]) >>> a[:,2] array([3, 6, 9]) >>> a[...,2] array([3, 6, 9]) \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Striding arrays} \begin{lstlisting} >>> a[0::2,0::2] array([[1, 3], [7, 9]]) # Slices are references to the # same memory! \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Random Numbers} \begin{lstlisting} >>> np.random.rand(3,2) array([[ 0.96276665, 0.77174861], [ 0.35138557, 0.61462271], [ 0.16789255, 0.43848811]]) >>> np.random.randint(1,100) 42 \end{lstlisting} \inctime{15} \end{frame} \begin{frame}[fragile] \frametitle{Problem Set} \begin{lstlisting} >>> from scipy import misc >>> A=misc.imread(name) >>> misc.imshow(A) \end{lstlisting} \begin{enumerate} \item Convert an RGB image to Grayscale. $ Y = 0.5R + 0.25G + 0.25B $ \item Scale the image to 50\% \item Introduce some random noise \item Smooth the image using a mean filter \\\small{Each element in the array is replaced by mean of all the neighbouring elements} \\\small{How fast does your code run?} \end{enumerate} \inctime{15} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{2D Plotting} \subsection{Getting Started} \begin{frame} {IPython's \typ{pylab} mode} \begin{block}{Immediate use:} \typ{\$ ipython -pylab} \end{block} \begin{itemize} \item \typ{pylab}: convenient 2D plotting interface to MPL \item Imports all of pylab for you! \item Allows for interactive plotting \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Basic 2D plotting} \begin{lstlisting} >>> x = linspace(0, 2*pi, 1000) >>> plot(x, sin(x)) >>> plot(x, sin(x), 'ro') >>> xlabel(r'$\chi$', color='g') # LaTeX markup! >>> ylabel(r'sin($\chi$)', color='r') >>> title('Simple figure', fontsize=20) >>> savefig('/tmp/test.eps') \end{lstlisting} \begin{itemize} \item Also: PNG, PDF, PS, EPS, SVG, PDF \end{itemize} \inctime{5} \end{frame} \subsection{Plots - Lines, Labels and Legends} \begin{frame}[fragile] \frametitle{Tweaking plots} \begin{lstlisting} # Set properties of objects: >>> l, = plot(x, sin(x)) # Why "l,"? >>> setp(l, linewidth=2.0, color='r') >>> l.set_linewidth(2.0) >>> draw() # Redraw. >>> setp(l) # Print properties. >>> clf() # Clear figure. >>> close() # Close figure. \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Working with text \ldots} %\begin{itemize} % \item We already saw LaTeX markup support! %\end{itemize} \begin{lstlisting} >>> w = arange(-2,2,.1) >>> plot(w,exp(-(w*w))*cos) >>> ylabel('$f(\omega)$') >>> xlabel('$\omega$') >>> title(r"$f(\omega)=e^{-\omega^2} cos({\omega^2})$") >>> annotate('maxima',xy=(0, 1), xytext=(1, 0.8), arrowprops=dict( facecolor='black', shrink=0.05)) \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Legends} \begin{lstlisting} >>> x = linspace(0, 2*np.pi, 1000) >>> plot(x, cos(5*x), 'r--', label='cosine') >>> plot(x, sin(5*x), 'g--', label='sine') >>> legend() # Or use: >>> legend(['cosine', 'sine']) \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Multiple figures} \begin{lstlisting} >>> figure(1) >>> plot(x, sin(x)) >>> figure(2) >>> plot(x, tanh(x)) >>> figure(1) >>> title('Easy as 1,2,3') \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{\typ{pylab} in Python scripts} \begin{lstlisting} import pylab x = pylab.linspace(0, 20, 1000) pylab.plot(x, pylab.sin(x)) # Can also use: from pylab import linspace, sin, plot \end{lstlisting} \inctime{10} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Types of Plots} \begin{frame}[fragile] \frametitle{X-Y plot} \begin{columns} \column{0.5\textwidth} \hspace*{-0.5in} \includegraphics[height=2in, interpolate=true]{data/xyplot} \column{0.45\textwidth} \begin{block}{Example code} \tiny \begin{lstlisting} t1 = arange(0.0, 5.0, 0.1) t2 = arange(0.0, 5.0, 0.02) t3 = arange(0.0, 2.0, 0.01) subplot(211) plot(t1, cos(2*pi*t1)*exp(-t1), 'bo', t2, cos(2*pi*t2)*exp(-t2), 'k') grid(True) title('A tale of 2 subplots') ylabel('Damped') subplot(212) plot(t3, cos(2*pi*t3), 'r--') grid(True) xlabel('time (s)') ylabel('Undamped') \end{lstlisting} \end{block} \end{columns} \end{frame} \begin{frame}[fragile] \frametitle{Semi-log and log-log plots} \begin{columns} \column{0.5\textwidth} \hspace*{-0.5in} \includegraphics[height=2in, interpolate=true]{data/log} \column{0.45\textwidth} \begin{block}{Example code} \tiny \begin{lstlisting} dt = 0.01 t = arange(dt, 20.0, dt) subplot(311) semilogy(t, exp(-t/5.0)) ylabel('semilogy') grid(True) subplot(312) semilogx(t, sin(2*pi*t)) ylabel('semilogx') grid(True) # minor grid on too gca().xaxis.grid(True, which='minor') subplot(313) loglog(t, 20*exp(-t/10.0), basex=4) grid(True) ylabel('loglog base 4 on x') \end{lstlisting} \end{block} \end{columns} \end{frame} \begin{frame}[fragile] \frametitle{Errorbar} \begin{columns} \column{0.5\textwidth} \hspace*{-0.5in} \includegraphics[height=2in, interpolate=true]{data/errorbar} \column{0.45\textwidth} \begin{block}{Example code} \tiny \begin{lstlisting} t = arange(0.1, 4, 0.1) s = exp(-t) e = 0.1*abs(randn(len(s))) f = 0.1*abs(randn(len(s))) g = 2*e h = 2*f errorbar(t, s, [e,g], f, fmt='o') xlabel('Distance (m)') ylabel('Height (m)') title('Mean and standard error '\ 'as a function of distance') \end{lstlisting} \end{block} \end{columns} \end{frame} \begin{frame}[fragile] \frametitle{Histogram} \begin{columns} \column{0.5\textwidth} \hspace*{-0.5in} \includegraphics[height=2in, interpolate=true]{data/histogram} \column{0.45\textwidth} \begin{block}{Example code} \tiny \begin{lstlisting} mu, sigma = 100, 15 x = mu + sigma*randn(10000) # the histogram of the data n, bins, patches = hist(x, 100, normed=1) # add a 'best fit' line y = normpdf( bins, mu, sigma) l = plot(bins, y, 'r--', linewidth=2) xlim(40, 160) xlabel('Smarts') ylabel('P') title(r'$\rm{IQ:}\/ \mu=100,\/ \sigma=15$') \end{lstlisting} \end{block} \end{columns} \end{frame} \begin{frame}[fragile] \frametitle{Bar charts} \begin{columns} \column{0.5\textwidth} \hspace*{-0.5in} \includegraphics[height=2in, interpolate=true]{data/barchart} \column{0.45\textwidth} \begin{block}{Example code} \tiny \begin{lstlisting} N = 5 menMeans = (20, 35, 30, 35, 27) menStd = ( 2, 3, 4, 1, 2) # the x locations for the groups ind = arange(N) # the width of the bars width = 0.35 p1 = bar(ind, menMeans, width, color='r', yerr=menStd) womenMeans = (25, 32, 34, 20, 25) womenStd = ( 3, 5, 2, 3, 3) p2 = bar(ind+width, womenMeans, width, color='y', yerr=womenStd) ylabel('Scores') title('Scores by group and gender') xticks(ind+width, ('G1', 'G2', 'G3', 'G4', 'G5')) xlim(-width,len(ind)) yticks(arange(0,41,10)) legend((p1[0], p2[0]), ('Men', 'Women'), shadow=True) \end{lstlisting} \end{block} \end{columns} \end{frame} \begin{frame}[fragile] \frametitle{Pie charts} \begin{columns} \column{0.5\textwidth} \hspace*{-0.4in} \includegraphics[height=2.0in, interpolate=true]{data/piechart} \column{0.45\textwidth} \begin{block}{Example code} \tiny \begin{lstlisting} # make a square figure and axes figure(1, figsize=(8,8)) ax = axes([0.1, 0.1, 0.8, 0.8]) labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' fracs = [15,30,45, 10] explode=(0, 0.05, 0, 0) pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True) title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5}) \end{lstlisting} \end{block} \end{columns} \end{frame} \begin{frame}[fragile] \frametitle{Scatter plots} \begin{columns} \column{0.5\textwidth} \hspace*{-0.4in} \includegraphics[height=2in, interpolate=true]{data/scatter} \column{0.45\textwidth} \begin{block}{Example code} \tiny \begin{lstlisting} N = 30 x = 0.9*rand(N) y = 0.9*rand(N) # 0 to 10 point radiuses area = pi*(10 * rand(N))**2 volume = 400 + rand(N)*450 scatter(x,y,s=area, marker='o', c=volume, alpha=0.75) xlabel(r'$\Delta_i$', size='x-large') ylabel(r'$\Delta_{i+1}$', size='x-large') title(r'Volume and percent change') grid(True) colorbar() savefig('scatter') \end{lstlisting} \end{block} \end{columns} \end{frame} \begin{frame}[fragile] \frametitle{Polar} \begin{columns} \column{0.5\textwidth} \hspace*{-0.5in} \includegraphics[height=2in, interpolate=true]{data/polar} \column{0.45\textwidth} \begin{block}{Example code} \tiny \begin{lstlisting} figure(figsize=(8,8)) ax = axes([0.1, 0.1, 0.8, 0.8], polar=True, axisbg='#d5de9c') r = arange(0,1,0.001) theta = 2*2*pi*r polar(theta, r, color='#ee8d18', lw=3) # the radius of the grid labels setp(ax.thetagridlabels, y=1.075) title(r'$\theta=4\pi r$', fontsize=20) \end{lstlisting} \end{block} \end{columns} \end{frame} \begin{frame}[fragile] \frametitle{Contours} \begin{columns} \column{0.45\textwidth} \hspace*{-0.5in} \includegraphics[height=2in, interpolate=true]{data/contour} \column{0.525\textwidth} \begin{block}{Example code} \tiny \begin{lstlisting} x = arange(-3.0, 3.0, 0.025) y = arange(-2.0, 2.0, 0.025) X, Y = meshgrid(x, y) Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) # difference of Gaussians Z = 10.0 * (Z2 - Z1) im = imshow(Z, interpolation='bilinear', origin='lower', cmap=cm.gray, extent=(-3,3,-2,2)) levels = arange(-1.2, 1.6, 0.2) # label every second level clabel(CS, levels[1::2], inline=1, fmt='%1.1f', fontsize=14) CS = contour(Z, levels, origin='lower', linewidths=2, extent=(-3,3,-2,2)) # make a colorbar for the contour lines CB = colorbar(CS, shrink=0.8, extend='both') title('Lines with colorbar') hot(); flag() \end{lstlisting} \end{block} \end{columns} \end{frame} \begin{frame}[fragile] \frametitle{Velocity vectors} \begin{columns} \column{0.5\textwidth} \hspace*{-0.5in} \includegraphics[height=2in, interpolate=true]{data/quiver} \column{0.45\textwidth} \begin{block}{Example code} \tiny \begin{lstlisting} X,Y = meshgrid(arange(0,2*pi,.2), arange(0,2*pi,.2) ) U = cos(X) V = sin(Y) Q = quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3], color='r', units='x', linewidths=(2,), edgecolors=('k'), headaxislength=5 ) qk = quiverkey(Q, 0.5, 0.03, 1, '1 m/s', fontproperties= {'weight': 'bold'}) axis([-1, 7, -1, 7]) title('triangular head; scale '\ 'with x view; black edges') \end{lstlisting} \end{block} \end{columns} \end{frame} \begin{frame}[fragile] \frametitle{Maps} \includegraphics[height=2.3in, interpolate=true]{data/plotmap} \begin{center} \tiny For details see \url{http://matplotlib.sourceforge.net/screenshots/plotmap.py} \end{center} \end{frame} \begin{frame} \frametitle{More information} \begin{itemize} \item More information here: \url{http://matplotlib.sf.net} \item \url{http://matplotlib.sf.net/tutorial.html} \item \url{http://matplotlib.sf.net/screenshots.html} \end{itemize} \inctime{5} \end{frame} \begin{frame} \frametitle{Problem Set} \begin{enumerate} \item Write a function that plots any regular n-gon given \typ{n}. \item Consider the logistic map, $f(x) = kx(1-x)$, plot it for $k=2.5, 3.5$ and $4$ in the same plot. \end{enumerate} \end{frame} \begin{frame}[fragile] \frametitle{Problem Set} \begin{columns} \column{0.6\textwidth} \small{ \begin{enumerate} \item Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) = kx(1-x)$. Plot the successive iterates of this process. \item Plot this using a cobweb plot as follows: \begin{enumerate} \item Start at $(x_0, 0)$ \item Draw line to $(x_i, f(x_i))$; \item Set $x_{i+1} = f(x_i)$ \item Draw line to $(x_i, x_i)$ \item Repeat from 2 for as long as you want \end{enumerate} \end{enumerate}} \column{0.35\textwidth} \hspace*{-0.5in} \includegraphics[height=1.6in, interpolate=true]{data/cobweb} \end{columns} \inctime{20} \end{frame} \begin{frame}{Summary} \begin{itemize} \item Basics of Numpy. \item Array operations. \item Plotting in 2D. \end{itemize} \end{frame} \end{document}