author | Santosh G. Vattam <vattam.santosh@gmail.com> |
Sun, 04 Oct 2009 17:03:10 +0530 | |
changeset 24 | 95fdc10975d5 |
parent 4 | e5047bcbb608 |
child 6 | 1f9492506ba2 |
permissions | -rw-r--r-- |
3 | 1 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
2 |
% Tutorial slides on Python. |
|
3 |
% |
|
4 |
% Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in> |
|
5 |
% Copyright (c) 2005-2008, Prabhu Ramachandran |
|
6 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
7 |
||
8 |
\documentclass[14pt,compress]{beamer} |
|
9 |
%\documentclass[draft]{beamer} |
|
10 |
%\documentclass[compress,handout]{beamer} |
|
11 |
%\usepackage{pgfpages} |
|
12 |
%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] |
|
13 |
||
14 |
% Modified from: generic-ornate-15min-45min.de.tex |
|
15 |
\mode<presentation> |
|
16 |
{ |
|
17 |
\usetheme{Warsaw} |
|
18 |
\useoutertheme{split} |
|
19 |
\setbeamercovered{transparent} |
|
20 |
} |
|
21 |
||
22 |
\usepackage[english]{babel} |
|
23 |
\usepackage[latin1]{inputenc} |
|
24 |
%\usepackage{times} |
|
25 |
\usepackage[T1]{fontenc} |
|
26 |
||
27 |
% Taken from Fernando's slides. |
|
28 |
\usepackage{ae,aecompl} |
|
29 |
\usepackage{mathpazo,courier,euler} |
|
30 |
\usepackage[scaled=.95]{helvet} |
|
31 |
||
32 |
\definecolor{darkgreen}{rgb}{0,0.5,0} |
|
33 |
||
34 |
\usepackage{listings} |
|
35 |
\lstset{language=Python, |
|
36 |
basicstyle=\ttfamily\bfseries, |
|
37 |
commentstyle=\color{red}\itshape, |
|
38 |
stringstyle=\color{darkgreen}, |
|
39 |
showstringspaces=false, |
|
40 |
keywordstyle=\color{blue}\bfseries} |
|
41 |
||
42 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
43 |
% Macros |
|
44 |
\setbeamercolor{emphbar}{bg=blue!20, fg=black} |
|
45 |
\newcommand{\emphbar}[1] |
|
46 |
{\begin{beamercolorbox}[rounded=true]{emphbar} |
|
47 |
{#1} |
|
48 |
\end{beamercolorbox} |
|
49 |
} |
|
50 |
\newcounter{time} |
|
51 |
\setcounter{time}{0} |
|
52 |
\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} |
|
53 |
||
54 |
\newcommand{\typ}[1]{\texttt{#1}} |
|
55 |
||
56 |
\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } |
|
57 |
||
58 |
%%% This is from Fernando's setup. |
|
59 |
% \usepackage{color} |
|
60 |
% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} |
|
61 |
% % Use and configure listings package for nicely formatted code |
|
62 |
% \usepackage{listings} |
|
63 |
% \lstset{ |
|
64 |
% language=Python, |
|
65 |
% basicstyle=\small\ttfamily, |
|
66 |
% commentstyle=\ttfamily\color{blue}, |
|
67 |
% stringstyle=\ttfamily\color{orange}, |
|
68 |
% showstringspaces=false, |
|
69 |
% breaklines=true, |
|
70 |
% postbreak = \space\dots |
|
71 |
% } |
|
72 |
||
73 |
||
74 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
75 |
% Title page |
|
76 |
\title[]{Matrices and Arrays\\ \& \\2D Plotting} |
|
77 |
||
78 |
\author[Asokan \& Prabhu] {Asokan Pichai\\Prabhu Ramachandran} |
|
79 |
||
80 |
\institute[FOSSEE] {FOSSEE Team} |
|
81 |
\date[] {11, October 2009} |
|
82 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
83 |
||
84 |
%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
|
85 |
%\logo{\pgfuseimage{iitmlogo}} |
|
86 |
||
87 |
||
88 |
%% Delete this, if you do not want the table of contents to pop up at |
|
89 |
%% the beginning of each subsection: |
|
90 |
\AtBeginSubsection[] |
|
91 |
{ |
|
92 |
\begin{frame}<beamer> |
|
93 |
\frametitle{Outline} |
|
94 |
\tableofcontents[currentsection,currentsubsection] |
|
95 |
\end{frame} |
|
96 |
} |
|
97 |
||
98 |
\AtBeginSection[] |
|
99 |
{ |
|
100 |
\begin{frame}<beamer> |
|
101 |
\frametitle{Outline} |
|
102 |
\tableofcontents[currentsection,currentsubsection] |
|
103 |
\end{frame} |
|
104 |
} |
|
105 |
||
106 |
% If you wish to uncover everything in a step-wise fashion, uncomment |
|
107 |
% the following command: |
|
108 |
%\beamerdefaultoverlayspecification{<+->} |
|
109 |
||
110 |
%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} |
|
111 |
||
112 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
113 |
% DOCUMENT STARTS |
|
114 |
\begin{document} |
|
115 |
||
116 |
\begin{frame} |
|
117 |
\maketitle |
|
118 |
\end{frame} |
|
119 |
||
120 |
\begin{frame} |
|
121 |
\frametitle{Outline} |
|
122 |
\tableofcontents |
|
123 |
% You might wish to add the option [pausesections] |
|
124 |
\end{frame} |
|
125 |
||
126 |
\section{Matrices and Arrays} |
|
127 |
||
128 |
\subsection{Basic \typ{numpy} } |
|
129 |
||
130 |
\newcommand{\num}{\texttt{numpy}} |
|
131 |
||
132 |
\begin{frame} |
|
133 |
\frametitle{The \num\ module} |
|
134 |
\begin{itemize} |
|
135 |
\item Why? |
|
136 |
\item What: |
|
137 |
\begin{itemize} |
|
138 |
\item An efficient and powerful array type for various common data |
|
139 |
types |
|
140 |
\item Abstracts out the most commonly used standard operations on |
|
141 |
arrays |
|
142 |
\end{itemize} |
|
143 |
\end{itemize} |
|
144 |
\end{frame} |
|
145 |
||
146 |
\begin{frame}[fragile] |
|
147 |
\frametitle{Examples of \num} |
|
148 |
\begin{lstlisting} |
|
149 |
# Simple array math example |
|
150 |
>>> from numpy import * |
|
151 |
>>> a = array([1,2,3,4]) |
|
152 |
>>> b = array([2,3,4,5]) |
|
153 |
>>> a*2 + b + 1 # Basic math! |
|
154 |
array([5, 8, 11, 14]) |
|
155 |
# Pi and e are defined. |
|
156 |
>>> x = linspace(0.0, 10.0, 1000) |
|
157 |
>>> x *= 2*pi/10 # inplace. |
|
158 |
# apply functions to array. |
|
159 |
>>> y = sin(x) |
|
160 |
\end{lstlisting} |
|
4
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
161 |
\inctime{5} |
3 | 162 |
\end{frame} |
163 |
||
164 |
\begin{frame} |
|
165 |
\frametitle{Basic concepts} |
|
166 |
\begin{itemize} |
|
167 |
\item fixed size (\typ{arr.size}); |
|
168 |
\item Same type (\typ{arr.dtype}) of data |
|
169 |
\item arbitrary dimensionality |
|
170 |
\item \typ{arr.shape}: size in each dimension |
|
171 |
\item \alert{Note:} \typ{len(arr) != arr.size} in general |
|
172 |
\item \alert{Note:} By default array operations are performed |
|
173 |
\alert{elementwise} |
|
174 |
\item Indices, slicing: just like lists |
|
175 |
\end{itemize} |
|
176 |
\end{frame} |
|
177 |
||
178 |
||
179 |
\begin{frame}[fragile] |
|
180 |
\frametitle{More examples of \num} |
|
181 |
\vspace*{-8pt} |
|
182 |
\begin{lstlisting} |
|
183 |
>>> x = array([1., 2, 3, 4]) |
|
184 |
>>> size(x) |
|
185 |
4 |
|
186 |
>>> x.dtype # What is a.dtype? |
|
187 |
dtype('float64') |
|
188 |
>>> x.shape |
|
189 |
(4,) |
|
190 |
>>> print rank(x), x.itemsize |
|
191 |
1 8 |
|
192 |
>>> x[0] = 10 |
|
193 |
>>> print x[0], x[-1] |
|
194 |
10.0 4.0 |
|
195 |
\end{lstlisting} |
|
196 |
||
197 |
\inctime{10} |
|
198 |
\end{frame} |
|
199 |
||
200 |
\begin{frame}[fragile] |
|
201 |
\frametitle{Multi-dimensional arrays} |
|
202 |
\begin{lstlisting} |
|
203 |
>>> a = array([[ 0, 1, 2, 3], |
|
204 |
... [10,11,12,13]]) |
|
205 |
>>> a.shape # (rows, columns) |
|
206 |
(2, 4) |
|
207 |
# Accessing and setting values |
|
208 |
>>> a[1,3] |
|
209 |
13 |
|
210 |
>>> a[1,3] = -1 |
|
211 |
>>> a[1] # The second row |
|
212 |
array([10,11,12,-1]) |
|
213 |
||
214 |
\end{lstlisting} |
|
215 |
\end{frame} |
|
4
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
216 |
\begin{frame}[fragile] |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
217 |
\frametitle{Array math} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
218 |
\begin{itemize} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
219 |
\item Basic \alert{elementwise} math (given two arrays \typ{a, b}): |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
220 |
\typ{+, -, *, /, \%} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
221 |
\item Inplace operators: \typ{a += b}, or \typ{add(a, b, |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
222 |
a)} etc. |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
223 |
\item Logical operations: \typ{equal (==)}, \typ{not\_equal (!=)}, |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
224 |
\typ{less (<)}, \typ{greater (>)} etc. |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
225 |
\item Trig and other functions: \typ{sin(x), arcsin(x), sinh(x), |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
226 |
exp(x), sqrt(x)} etc. |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
227 |
\item \typ{sum(x, axis=0), product(x, axis=0)} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
228 |
\item \typ{dot(a, bp)} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
229 |
\end{itemize} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
230 |
\inctime{10} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
231 |
\end{frame} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
232 |
|
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
233 |
\subsection{Array Creation \& Slicing, Striding Arrays} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
234 |
\begin{frame}[fragile] |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
235 |
\frametitle{Array creation functions} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
236 |
\begin{itemize} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
237 |
\item \typ{array(object, dtype=None, \ldots)} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
238 |
\item \typ{arange(start, stop=None, step=1 \ldots)} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
239 |
\item \typ{linspace(start, stop, num=50, \ldots)} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
240 |
\item \typ{ones(shape, dtype=None, \ldots)} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
241 |
\item \typ{zeros(shape, dtype=float,\ldots)} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
242 |
\item \typ{identity(n)} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
243 |
\item \typ{empty(shape, dtype=float,\ldots)} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
244 |
\item \typ{ones\_like(x)}, |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
245 |
\item \typ{zeros\_like(x)}, \typ{empty\_like(x)} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
246 |
\end{itemize} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
247 |
\end{frame} |
3 | 248 |
|
249 |
\begin{frame}[fragile] |
|
250 |
\frametitle{Slicing arrays} |
|
251 |
\begin{lstlisting} |
|
252 |
>>> a = array([[1,2,3], [4,5,6], |
|
253 |
[7,8,9]]) |
|
254 |
>>> a[0,1:3] |
|
255 |
array([2, 3]) |
|
256 |
>>> a[1:,1:] |
|
257 |
array([[5, 6], |
|
258 |
[8, 9]]) |
|
259 |
>>> a[:,2] |
|
260 |
array([3, 6, 9]) |
|
261 |
\end{lstlisting} |
|
262 |
\end{frame} |
|
4
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
263 |
|
3 | 264 |
\begin{frame}[fragile] |
265 |
\frametitle{Striding arrays} |
|
266 |
\begin{lstlisting} |
|
267 |
>>> a[0::2,0::2] |
|
268 |
array([[1, 3], |
|
269 |
[7, 9]]) |
|
270 |
# Slices are references to the |
|
271 |
# same memory! |
|
272 |
\end{lstlisting} |
|
273 |
\end{frame} |
|
274 |
||
275 |
\begin{frame}[fragile] |
|
4
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
276 |
\frametitle{Random Numbers} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
277 |
\begin{lstlisting} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
278 |
>>> np.random.rand(3,2) |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
279 |
array([[ 0.96276665, 0.77174861], |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
280 |
[ 0.35138557, 0.61462271], |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
281 |
[ 0.16789255, 0.43848811]]) |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
282 |
>>> np.random.randint(1,100) |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
283 |
42 |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
284 |
\end{lstlisting} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
285 |
\inctime{15} |
3 | 286 |
\end{frame} |
287 |
||
288 |
\begin{frame}[fragile] |
|
4
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
289 |
\frametitle{Problem set 1.0} |
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
290 |
\inctime{15} |
3 | 291 |
\end{frame} |
4
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
292 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
3 | 293 |
|
294 |
\section{2D Plotting} |
|
295 |
\subsection{Getting Started} |
|
296 |
||
297 |
\begin{frame} |
|
298 |
{IPython's \typ{pylab} mode} |
|
299 |
\begin{itemize} |
|
300 |
\item \typ{pylab}: convenient 2D plotting interface to MPL |
|
301 |
\item Immediate use: \typ{ipython -pylab} |
|
302 |
\item Imports all of pylab for you! |
|
303 |
\item Allows for interactive plotting |
|
304 |
\end{itemize} |
|
305 |
\end{frame} |
|
306 |
||
307 |
\begin{frame}[fragile] |
|
308 |
\frametitle{Basic 2D plotting} |
|
309 |
||
310 |
\begin{lstlisting} |
|
311 |
>>> x = linspace(0, 2*pi, 1000) |
|
312 |
>>> plot(x, sin(x)) |
|
313 |
>>> plot(x, sin(x), 'ro') |
|
314 |
>>> xlabel(r'$\chi$', color='g') |
|
315 |
# LaTeX markup! |
|
316 |
>>> ylabel(r'sin($\chi$)', color='r') |
|
317 |
>>> title('Simple figure', fontsize=20) |
|
318 |
>>> savefig('/tmp/test.eps') |
|
319 |
\end{lstlisting} |
|
320 |
\begin{itemize} |
|
321 |
\item Also: PNG, PDF, PS, EPS, SVG, PDF |
|
322 |
\end{itemize} |
|
323 |
\end{frame} |
|
324 |
||
4
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
325 |
\subsection{Plots - Lines, Labels and Legends} |
3 | 326 |
\begin{frame}[fragile] |
327 |
\frametitle{Basic plotting \ldots} |
|
328 |
\begin{lstlisting} |
|
329 |
# Set properties of objects: |
|
330 |
>>> l, = plot(x, sin(x)) |
|
331 |
# Why "l,"? |
|
332 |
>>> setp(l, linewidth=2.0, color='r') |
|
333 |
>>> l.set_linewidth(2.0) |
|
334 |
>>> draw() # Redraw. |
|
335 |
>>> setp(l) # Print properties |
|
336 |
>>> clf() # Clear figure. |
|
337 |
>>> close() # Close figure. |
|
338 |
\end{lstlisting} |
|
339 |
\end{frame} |
|
340 |
||
341 |
\begin{frame}[fragile] |
|
342 |
\frametitle{Multiple figures} |
|
343 |
||
344 |
\begin{lstlisting} |
|
345 |
>>> figure(1) |
|
346 |
>>> plot(x, sin(x)) |
|
347 |
>>> figure(2) |
|
348 |
>>> plot(x, tanh(x)) |
|
349 |
>>> figure(1) |
|
350 |
>>> title('Easy as 1,2,3') |
|
351 |
\end{lstlisting} |
|
352 |
||
353 |
\end{frame} |
|
354 |
||
355 |
\begin{frame}[fragile] |
|
356 |
\frametitle{Legends and Annotation} |
|
357 |
\begin{lstlisting} |
|
358 |
>>> plot(x, cos(5*x), 'r--', |
|
359 |
label='cosine') |
|
360 |
>>> plot(x, sin(5*x), 'g--', |
|
361 |
label='sine') |
|
362 |
>>> legend() |
|
363 |
# Or use: |
|
364 |
>>> legend(['cosine', 'sine']) |
|
365 |
# Annotation: |
|
366 |
>>> text(1,0, '(1,0)') |
|
367 |
\end{lstlisting} |
|
368 |
\end{frame} |
|
369 |
||
370 |
\begin{frame}[fragile] |
|
371 |
\frametitle{More commands \ldots} |
|
372 |
\begin{lstlisting} |
|
373 |
# semilog, loglog |
|
374 |
>>> x = 10.**(-arange(100)*0.1) |
|
375 |
>>> semilogx(x, x) |
|
376 |
>>> semilogy(x, x) |
|
377 |
>>> loglog(x, x) |
|
378 |
>>> loglog(x, x*x) |
|
379 |
\end{lstlisting} |
|
380 |
\end{frame} |
|
381 |
||
382 |
\begin{frame}[fragile] |
|
383 |
\frametitle{More plots \ldots} |
|
384 |
\begin{lstlisting} |
|
385 |
>>> clf() |
|
386 |
>>> t = arange(0.1, 4, 0.1) |
|
387 |
>>> s = exp(-t) |
|
388 |
>>> e = 0.1*abs(randn(len(s))) |
|
389 |
>>> errorbar(t, s, e) |
|
390 |
# Scatter plots |
|
391 |
>>> clf() |
|
392 |
>>> t = randn(len(e)) |
|
393 |
>>> scatter(t, e, c=s) |
|
394 |
\end{lstlisting} |
|
395 |
\end{frame} |
|
396 |
||
397 |
\begin{frame}[fragile] |
|
398 |
\frametitle{Note: \typ{pylab} in Python scripts} |
|
399 |
\begin{lstlisting} |
|
400 |
import pylab |
|
401 |
x = pylab.linspace(0, 20, 1000) |
|
402 |
pylab.plot(x, pylab.sin(x)) |
|
403 |
||
404 |
# Can also use: |
|
405 |
from pylab import linspace, sin, plot |
|
406 |
\end{lstlisting} |
|
407 |
\end{frame} |
|
408 |
||
409 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
410 |
\subsection{Types of Plots} |
|
411 |
\begin{frame}[fragile] |
|
412 |
\frametitle{X-Y plot} |
|
413 |
\begin{columns} |
|
414 |
\column{0.5\textwidth} |
|
415 |
\hspace*{-0.5in} |
|
416 |
\includegraphics[height=2in, interpolate=true]{data/xyplot} |
|
417 |
\column{0.45\textwidth} |
|
418 |
\begin{block}{Example code} |
|
419 |
\tiny |
|
420 |
\begin{lstlisting} |
|
421 |
t1 = arange(0.0, 5.0, 0.1) |
|
422 |
t2 = arange(0.0, 5.0, 0.02) |
|
423 |
t3 = arange(0.0, 2.0, 0.01) |
|
424 |
subplot(211) |
|
425 |
plot(t1, cos(2*pi*t1)*exp(-t1), 'bo', |
|
426 |
t2, cos(2*pi*t2)*exp(-t2), 'k') |
|
427 |
grid(True) |
|
428 |
title('A tale of 2 subplots') |
|
429 |
ylabel('Damped') |
|
430 |
subplot(212) |
|
431 |
plot(t3, cos(2*pi*t3), 'r--') |
|
432 |
grid(True) |
|
433 |
xlabel('time (s)') |
|
434 |
ylabel('Undamped') |
|
435 |
\end{lstlisting} |
|
436 |
\end{block} |
|
437 |
\end{columns} |
|
438 |
\end{frame} |
|
439 |
||
440 |
\begin{frame}[fragile] \frametitle{Semi-log and log-log plots} |
|
441 |
\begin{columns} |
|
442 |
\column{0.5\textwidth} |
|
443 |
\hspace*{-0.5in} |
|
444 |
\includegraphics[height=2in, interpolate=true]{data/log} |
|
445 |
\column{0.45\textwidth} |
|
446 |
\begin{block}{Example code} |
|
447 |
\tiny |
|
448 |
\begin{lstlisting} |
|
449 |
dt = 0.01 |
|
450 |
t = arange(dt, 20.0, dt) |
|
451 |
subplot(311) |
|
452 |
semilogy(t, exp(-t/5.0)) |
|
453 |
ylabel('semilogy') |
|
454 |
grid(True) |
|
455 |
subplot(312) |
|
456 |
semilogx(t, sin(2*pi*t)) |
|
457 |
ylabel('semilogx') |
|
458 |
grid(True) |
|
459 |
# minor grid on too |
|
460 |
gca().xaxis.grid(True, which='minor') |
|
461 |
subplot(313) |
|
462 |
loglog(t, 20*exp(-t/10.0), basex=4) |
|
463 |
grid(True) |
|
464 |
ylabel('loglog base 4 on x') |
|
465 |
\end{lstlisting} |
|
466 |
\end{block} |
|
467 |
\end{columns} |
|
468 |
\end{frame} |
|
469 |
||
470 |
\begin{frame}[fragile] \frametitle{Errorbar} |
|
471 |
\begin{columns} |
|
472 |
\column{0.5\textwidth} |
|
473 |
\hspace*{-0.5in} |
|
474 |
\includegraphics[height=2in, interpolate=true]{data/errorbar} |
|
475 |
\column{0.45\textwidth} |
|
476 |
\begin{block}{Example code} |
|
477 |
\tiny |
|
478 |
\begin{lstlisting} |
|
479 |
t = arange(0.1, 4, 0.1) |
|
480 |
s = exp(-t) |
|
481 |
e = 0.1*abs(randn(len(s))) |
|
482 |
f = 0.1*abs(randn(len(s))) |
|
483 |
g = 2*e |
|
484 |
h = 2*f |
|
485 |
errorbar(t, s, [e,g], f, fmt='o') |
|
486 |
xlabel('Distance (m)') |
|
487 |
ylabel('Height (m)') |
|
488 |
title('Mean and standard error '\ |
|
489 |
'as a function of distance') |
|
490 |
\end{lstlisting} |
|
491 |
\end{block} |
|
492 |
\end{columns} |
|
493 |
\end{frame} |
|
494 |
||
495 |
\begin{frame}[fragile] \frametitle{Histogram} |
|
496 |
\begin{columns} |
|
497 |
\column{0.5\textwidth} |
|
498 |
\hspace*{-0.5in} |
|
499 |
\includegraphics[height=2in, interpolate=true]{data/histogram} |
|
500 |
\column{0.45\textwidth} |
|
501 |
\begin{block}{Example code} |
|
502 |
\tiny |
|
503 |
\begin{lstlisting} |
|
504 |
mu, sigma = 100, 15 |
|
505 |
x = mu + sigma*randn(10000) |
|
506 |
# the histogram of the data |
|
507 |
n, bins, patches = hist(x, 100, normed=1) |
|
508 |
# add a 'best fit' line |
|
509 |
y = normpdf( bins, mu, sigma) |
|
510 |
l = plot(bins, y, 'r--', linewidth=2) |
|
511 |
xlim(40, 160) |
|
512 |
xlabel('Smarts') |
|
513 |
ylabel('P') |
|
514 |
title(r'$\rm{IQ:}\/ \mu=100,\/ \sigma=15$') |
|
515 |
\end{lstlisting} |
|
516 |
\end{block} |
|
517 |
\end{columns} |
|
518 |
\end{frame} |
|
519 |
||
520 |
\begin{frame}[fragile] \frametitle{Bar charts} |
|
521 |
\begin{columns} |
|
522 |
\column{0.5\textwidth} |
|
523 |
\hspace*{-0.5in} |
|
524 |
\includegraphics[height=2in, interpolate=true]{data/barchart} |
|
525 |
\column{0.45\textwidth} |
|
526 |
\begin{block}{Example code} |
|
527 |
\tiny |
|
528 |
\begin{lstlisting} |
|
529 |
N = 5 |
|
530 |
menMeans = (20, 35, 30, 35, 27) |
|
531 |
menStd = ( 2, 3, 4, 1, 2) |
|
532 |
# the x locations for the groups |
|
533 |
ind = arange(N) |
|
534 |
# the width of the bars |
|
535 |
width = 0.35 |
|
536 |
p1 = bar(ind, menMeans, width, |
|
537 |
color='r', yerr=menStd) |
|
538 |
womenMeans = (25, 32, 34, 20, 25) |
|
539 |
womenStd = ( 3, 5, 2, 3, 3) |
|
540 |
p2 = bar(ind+width, womenMeans, width, |
|
541 |
color='y', yerr=womenStd) |
|
542 |
ylabel('Scores') |
|
543 |
title('Scores by group and gender') |
|
544 |
xticks(ind+width, |
|
545 |
('G1', 'G2', 'G3', 'G4', 'G5')) |
|
546 |
xlim(-width,len(ind)) |
|
547 |
yticks(arange(0,41,10)) |
|
548 |
legend((p1[0], p2[0]), |
|
549 |
('Men', 'Women'), shadow=True) |
|
550 |
\end{lstlisting} |
|
551 |
\end{block} |
|
552 |
\end{columns} |
|
553 |
\end{frame} |
|
554 |
||
555 |
\begin{frame}[fragile] \frametitle{Pie charts} |
|
556 |
\begin{columns} |
|
557 |
\column{0.5\textwidth} |
|
558 |
\hspace*{-0.4in} |
|
559 |
\includegraphics[height=2.0in, interpolate=true]{data/piechart} |
|
560 |
\column{0.45\textwidth} |
|
561 |
\begin{block}{Example code} |
|
562 |
\tiny |
|
563 |
\begin{lstlisting} |
|
564 |
# make a square figure and axes |
|
565 |
figure(1, figsize=(8,8)) |
|
566 |
ax = axes([0.1, 0.1, 0.8, 0.8]) |
|
567 |
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' |
|
568 |
fracs = [15,30,45, 10] |
|
569 |
explode=(0, 0.05, 0, 0) |
|
570 |
pie(fracs, explode=explode, labels=labels, |
|
571 |
autopct='%1.1f%%', shadow=True) |
|
572 |
title('Raining Hogs and Dogs', |
|
573 |
bbox={'facecolor':'0.8', 'pad':5}) |
|
574 |
\end{lstlisting} |
|
575 |
\end{block} |
|
576 |
\end{columns} |
|
577 |
\end{frame} |
|
578 |
||
579 |
\begin{frame}[fragile] \frametitle{Scatter plots} |
|
580 |
\begin{columns} |
|
581 |
\column{0.5\textwidth} |
|
582 |
\hspace*{-0.4in} |
|
583 |
\includegraphics[height=2in, interpolate=true]{data/scatter} |
|
584 |
\column{0.45\textwidth} |
|
585 |
\begin{block}{Example code} |
|
586 |
\tiny |
|
587 |
\begin{lstlisting} |
|
588 |
N = 30 |
|
589 |
x = 0.9*rand(N) |
|
590 |
y = 0.9*rand(N) |
|
591 |
# 0 to 10 point radiuses |
|
592 |
area = pi*(10 * rand(N))**2 |
|
593 |
volume = 400 + rand(N)*450 |
|
594 |
scatter(x,y,s=area, marker='o', c=volume, |
|
595 |
alpha=0.75) |
|
596 |
xlabel(r'$\Delta_i$', size='x-large') |
|
597 |
ylabel(r'$\Delta_{i+1}$', size='x-large') |
|
598 |
title(r'Volume and percent change') |
|
599 |
grid(True) |
|
600 |
colorbar() |
|
601 |
savefig('scatter') |
|
602 |
\end{lstlisting} |
|
603 |
\end{block} |
|
604 |
\end{columns} |
|
605 |
\end{frame} |
|
606 |
||
607 |
\begin{frame}[fragile] \frametitle{Polar} |
|
608 |
\begin{columns} |
|
609 |
\column{0.5\textwidth} |
|
610 |
\hspace*{-0.5in} |
|
611 |
\includegraphics[height=2in, interpolate=true]{data/polar} |
|
612 |
\column{0.45\textwidth} |
|
613 |
\begin{block}{Example code} |
|
614 |
\tiny |
|
615 |
\begin{lstlisting} |
|
616 |
figure(figsize=(8,8)) |
|
617 |
ax = axes([0.1, 0.1, 0.8, 0.8], |
|
618 |
polar=True, |
|
619 |
axisbg='#d5de9c') |
|
620 |
r = arange(0,1,0.001) |
|
621 |
theta = 2*2*pi*r |
|
622 |
polar(theta, r, color='#ee8d18', lw=3) |
|
623 |
# the radius of the grid labels |
|
624 |
setp(ax.thetagridlabels, y=1.075) |
|
4
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
625 |
title(r'$\theta=4\pi r$', fontsize=20) |
3 | 626 |
\end{lstlisting} |
4
e5047bcbb608
Moved Array Math, Creation slides around; Added time counters.
Puneeth Chaganti <punchagan@fossee.in>
parents:
3
diff
changeset
|
627 |
|
3 | 628 |
\end{block} |
629 |
\end{columns} |
|
630 |
\end{frame} |
|
631 |
||
632 |
\begin{frame}[fragile] \frametitle{Contours} |
|
633 |
\begin{columns} |
|
634 |
\column{0.45\textwidth} |
|
635 |
\hspace*{-0.5in} |
|
636 |
\includegraphics[height=2in, interpolate=true]{data/contour} |
|
637 |
\column{0.525\textwidth} |
|
638 |
\begin{block}{Example code} |
|
639 |
\tiny |
|
640 |
\begin{lstlisting} |
|
641 |
x = arange(-3.0, 3.0, 0.025) |
|
642 |
y = arange(-2.0, 2.0, 0.025) |
|
643 |
X, Y = meshgrid(x, y) |
|
644 |
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) |
|
645 |
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) |
|
646 |
# difference of Gaussians |
|
647 |
Z = 10.0 * (Z2 - Z1) |
|
648 |
im = imshow(Z, interpolation='bilinear', |
|
649 |
origin='lower', |
|
650 |
cmap=cm.gray, extent=(-3,3,-2,2)) |
|
651 |
levels = arange(-1.2, 1.6, 0.2) |
|
652 |
# label every second level |
|
653 |
clabel(CS, levels[1::2], inline=1, |
|
654 |
fmt='%1.1f', fontsize=14) |
|
655 |
CS = contour(Z, levels, |
|
656 |
origin='lower', |
|
657 |
linewidths=2, |
|
658 |
extent=(-3,3,-2,2)) |
|
659 |
# make a colorbar for the contour lines |
|
660 |
CB = colorbar(CS, shrink=0.8, extend='both') |
|
661 |
title('Lines with colorbar') |
|
662 |
hot(); flag() |
|
663 |
\end{lstlisting} |
|
664 |
\end{block} |
|
665 |
\end{columns} |
|
666 |
\end{frame} |
|
667 |
||
668 |
\begin{frame}[fragile] \frametitle{Velocity vectors} |
|
669 |
\begin{columns} |
|
670 |
\column{0.5\textwidth} |
|
671 |
\hspace*{-0.5in} |
|
672 |
\includegraphics[height=2in, interpolate=true]{data/quiver} |
|
673 |
\column{0.45\textwidth} |
|
674 |
\begin{block}{Example code} |
|
675 |
\tiny |
|
676 |
\begin{lstlisting} |
|
677 |
X,Y = meshgrid(arange(0,2*pi,.2), |
|
678 |
arange(0,2*pi,.2) ) |
|
679 |
U = cos(X) |
|
680 |
V = sin(Y) |
|
681 |
Q = quiver(X[::3, ::3], Y[::3, ::3], |
|
682 |
U[::3, ::3], V[::3, ::3], |
|
683 |
color='r', units='x', |
|
684 |
linewidths=(2,), |
|
685 |
edgecolors=('k'), |
|
686 |
headaxislength=5 ) |
|
687 |
qk = quiverkey(Q, 0.5, 0.03, 1, '1 m/s', |
|
688 |
fontproperties= |
|
689 |
{'weight': 'bold'}) |
|
690 |
axis([-1, 7, -1, 7]) |
|
691 |
title('triangular head; scale '\ |
|
692 |
'with x view; black edges') |
|
693 |
\end{lstlisting} |
|
694 |
\end{block} |
|
695 |
\end{columns} |
|
696 |
\end{frame} |
|
697 |
||
698 |
\begin{frame}[fragile] \frametitle{Maps} |
|
699 |
\includegraphics[height=2.5in, interpolate=true]{data/plotmap} |
|
700 |
\begin{center} |
|
701 |
\tiny |
|
702 |
For details see \url{http://matplotlib.sourceforge.net/screenshots/plotmap.py} |
|
703 |
\end{center} |
|
704 |
\end{frame} |
|
705 |
||
706 |
||
707 |
\begin{frame} |
|
708 |
\frametitle{More information} |
|
709 |
\begin{itemize} |
|
710 |
\item More information here: \url{http://matplotlib.sf.net} |
|
711 |
\item \url{http://matplotlib.sf.net/tutorial.html} |
|
712 |
\item \url{http://matplotlib.sf.net/screenshots.html} |
|
713 |
\end{itemize} |
|
714 |
||
715 |
\inctime{25} |
|
716 |
\end{frame} |
|
717 |
||
718 |
\begin{frame} |
|
719 |
\frametitle{Problem set 1.0} |
|
720 |
\begin{enumerate} |
|
721 |
\item Write a function that plots any n-gon given \typ{n}. |
|
722 |
\item Consider the logistic map, $f(x) = kx(1-x)$, plot it for |
|
723 |
$k=2.5, 3.5$ and $4$ |
|
724 |
\end{enumerate} |
|
725 |
\end{frame} |
|
726 |
||
727 |
\begin{frame} |
|
728 |
\frametitle{Problem set 1.1} |
|
729 |
\begin{enumerate} |
|
730 |
\item Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) = |
|
731 |
kx(1-x)$. Plot the successive iterates of this process. |
|
732 |
\item Plot this using a cobweb plot as follows: |
|
733 |
\begin{enumerate} |
|
734 |
\item Start at $(x_0, 0)$ |
|
735 |
\item Draw line to $(x_i, f(x_i))$; |
|
736 |
\item Set $x_{i+1} = f(x_i)$ |
|
737 |
\item Draw line to $(x_i, x_i)$ |
|
738 |
\item Repeat from 2 for as long as you want |
|
739 |
\end{enumerate} |
|
740 |
\end{enumerate} |
|
741 |
\end{frame} |
|
742 |
||
743 |
\begin{frame} |
|
744 |
\frametitle{Problem set 1.2} |
|
745 |
\begin{enumerate} |
|
746 |
||
747 |
\item Plot the Koch snowflake. Write a function to generate the |
|
748 |
necessary points given the two points constituting a line. |
|
749 |
\pause |
|
750 |
\begin{enumerate} |
|
751 |
\item Split the line into 4 segments. |
|
752 |
\item The first and last segments are trivial. |
|
753 |
\item To rotate the point you can use complex numbers, |
|
754 |
recall that $z e^{j \theta}$ rotates a point $z$ in 2D |
|
755 |
by $\theta$. |
|
756 |
\item Do this for all line segments till everything is |
|
757 |
done. |
|
758 |
\end{enumerate} |
|
759 |
\item Show rate of convergence for a first and second order finite |
|
760 |
difference of sin(x) |
|
761 |
\end{enumerate} |
|
762 |
\inctime{30} |
|
763 |
\end{frame} |
|
764 |
\end{document} |