1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
2 % Tutorial slides on Python. |
|
3 % |
|
4 % Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in> |
|
5 % Copyright (c) 2005-2009, 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]{\lstinline{#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 % Title page |
|
75 \title[]{Arrays\\ \& \\2D Plotting} |
|
76 |
|
77 \author[FOSSEE Team] {FOSSEE} |
|
78 |
|
79 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
|
80 \date[] {11, October 2009} |
|
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
82 |
|
83 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
|
84 %\logo{\pgfuseimage{iitmlogo}} |
|
85 |
|
86 |
|
87 %% Delete this, if you do not want the table of contents to pop up at |
|
88 %% the beginning of each subsection: |
|
89 \AtBeginSubsection[] |
|
90 { |
|
91 \begin{frame}<beamer> |
|
92 \frametitle{Outline} |
|
93 \tableofcontents[currentsection,currentsubsection] |
|
94 \end{frame} |
|
95 } |
|
96 |
|
97 \AtBeginSection[] |
|
98 { |
|
99 \begin{frame}<beamer> |
|
100 \frametitle{Outline} |
|
101 \tableofcontents[currentsection,currentsubsection] |
|
102 \end{frame} |
|
103 } |
|
104 |
|
105 \newcommand{\num}{\texttt{numpy}} |
|
106 |
|
107 |
|
108 % If you wish to uncover everything in a step-wise fashion, uncomment |
|
109 % the following command: |
|
110 %\beamerdefaultoverlayspecification{<+->} |
|
111 |
|
112 %\includeonlyframes{current,current1,current2,current3,current4,current5,current6} |
|
113 |
|
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
115 % DOCUMENT STARTS |
|
116 \begin{document} |
|
117 |
|
118 \begin{frame} |
|
119 \maketitle |
|
120 \end{frame} |
|
121 |
|
122 \begin{frame} |
|
123 \frametitle{Outline} |
|
124 \tableofcontents |
|
125 % You might wish to add the option [pausesections] |
|
126 \end{frame} |
|
127 |
|
128 \section{Arrays} |
|
129 |
|
130 \subsection{Basic \typ{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 types |
|
139 \item Abstracts out the most commonly used standard operations on arrays |
|
140 \end{itemize} |
|
141 \end{itemize} |
|
142 \end{frame} |
|
143 |
|
144 \begin{frame}[fragile] |
|
145 \frametitle{Examples of \num} |
|
146 \begin{lstlisting} |
|
147 # Simple array math example |
|
148 >>> from numpy import * |
|
149 >>> a = array([1,2,3,4]) |
|
150 >>> b = arange(2,6) |
|
151 >>> b |
|
152 array([2,3,4,5]) |
|
153 >>> a*2 + b + 1 # Basic math! |
|
154 array([5, 8, 11, 14]) |
|
155 \end{lstlisting} |
|
156 \end{frame} |
|
157 |
|
158 \begin{frame}[fragile] |
|
159 \frametitle{Examples of \num} |
|
160 \begin{lstlisting} |
|
161 # Pi and e are defined. |
|
162 >>> x = linspace(0.0, 10.0, 1000) |
|
163 >>> x *= 2*pi/10 # inplace. |
|
164 # apply functions to array. |
|
165 >>> y = sin(x) |
|
166 >>> z = exp(y) |
|
167 \end{lstlisting} |
|
168 \inctime{5} |
|
169 \end{frame} |
|
170 |
|
171 \begin{frame}[fragile] |
|
172 \frametitle{More examples of \num} |
|
173 \vspace*{-8pt} |
|
174 \begin{lstlisting} |
|
175 >>> x = array([1., 2, 3, 4]) |
|
176 >>> size(x) |
|
177 4 |
|
178 >>> x.dtype # What is a.dtype? |
|
179 dtype('float64') |
|
180 >>> x.shape |
|
181 (4,) |
|
182 >>> print x.itemsize |
|
183 8 |
|
184 >>> x[0] = 10 |
|
185 >>> print x[0], x[-1] |
|
186 10.0 4.0 |
|
187 \end{lstlisting} |
|
188 \end{frame} |
|
189 |
|
190 \begin{frame}[fragile] |
|
191 \frametitle{Multi-dimensional arrays} |
|
192 \begin{lstlisting} |
|
193 >>> a = array([[ 0, 1, 2, 3], |
|
194 ... [10,11,12,13]]) |
|
195 >>> a.shape # (rows, columns) |
|
196 (2, 4) |
|
197 >>> a.shape=4,2 |
|
198 # Accessing and setting values |
|
199 >>> a[1][3] |
|
200 >>> a[1,3] |
|
201 >>> a[1,3] = -1 |
|
202 >>> a[1] # The second row |
|
203 \end{lstlisting} |
|
204 \end{frame} |
|
205 |
|
206 \begin{frame}[fragile] |
|
207 \frametitle{Array math} |
|
208 \begin{itemize} |
|
209 \item Basic \alert{elementwise} math (given two arrays \typ{a, b}): |
|
210 \typ{+, -, *, /, \%} |
|
211 \item Inplace operators: \typ{a += b} |
|
212 \item \typ{sum(x, axis=0)}, |
|
213 \typ{product(x, axis=0)}, |
|
214 \typ{dot(a, bp)} |
|
215 \end{itemize} |
|
216 \begin{lstlisting} |
|
217 >>> x = array([[0,2,4,2],[1,2,3,4]]) |
|
218 >>> sum(x) |
|
219 >>> sum(x,axis=1) |
|
220 \end{lstlisting} |
|
221 \end{frame} |
|
222 |
|
223 \begin{frame}[fragile] |
|
224 \frametitle{Array math cont.} |
|
225 \begin{itemize} |
|
226 \item Logical operations: \typ{==}, \typ{!=}, |
|
227 \typ{<}, \typ{>} etc. |
|
228 \item Trig and other functions: \typ{sin(x),} |
|
229 \typ{arcsin(x), sinh(x),} |
|
230 \typ{exp(x), sqrt(x)} etc. |
|
231 \end{itemize} |
|
232 \begin{lstlisting} |
|
233 >>> a<4, a!=3 |
|
234 >>> sqrt(a) |
|
235 \end{lstlisting} |
|
236 \inctime{10} |
|
237 \end{frame} |
|
238 |
|
239 \begin{frame} |
|
240 \frametitle{Summary of Concepts} |
|
241 \begin{itemize} |
|
242 \item fixed size (\typ{arr.size}); |
|
243 \item Same type (\typ{arr.dtype}) of data |
|
244 \item arbitrary dimensionality |
|
245 \item \typ{arr.shape}: size in each dimension |
|
246 \item \alert{Note:} \typ{len(arr) != arr.size} in general |
|
247 \item \alert{Note:} By default array operations are performed |
|
248 \alert{elementwise} |
|
249 \end{itemize} |
|
250 \end{frame} |
|
251 |
|
252 \subsection{Array Creation \& Slicing, Striding Arrays} |
|
253 |
|
254 \begin{frame}[fragile] |
|
255 \frametitle{Array creation functions} |
|
256 \begin{itemize} |
|
257 \item \typ{array?} \alert{\#Doc string reading} |
|
258 \item \typ{array(object,dtype=None,...)} |
|
259 \begin{lstlisting} |
|
260 >>> array([2,3,4]) |
|
261 array([2, 3, 4]) |
|
262 \end{lstlisting} |
|
263 \item \typ{linspace(start,stop,num)} |
|
264 \begin{lstlisting} |
|
265 >>> linspace(0, 2, 4) |
|
266 array([0.,0.6666667,1.3333333,2.]) |
|
267 \end{lstlisting} |
|
268 \item \typ{arange?} |
|
269 \alert{\# float version of range} |
|
270 \end{itemize} |
|
271 \end{frame} |
|
272 |
|
273 \begin{frame}[fragile] |
|
274 \frametitle{Array creation functions cont.} |
|
275 \begin{itemize} |
|
276 \item \typ{ones(shape, dtype=None, ...)} |
|
277 \begin{lstlisting} |
|
278 >>> ones((2,2)) |
|
279 array([[ 1., 1.], |
|
280 [ 1., 1.]]) |
|
281 \end{lstlisting} |
|
282 \item \typ{identity(n)} |
|
283 \item \typ{ones\_like(x)} |
|
284 \begin{lstlisting} |
|
285 >>> a = array([[1,2,3],[4,5,6]]) |
|
286 >>> ones_like(a) |
|
287 array([[1, 1, 1], |
|
288 [1, 1, 1]]) |
|
289 \end{lstlisting} |
|
290 \item Also try \typ{zeros, zeros\_like, empty} |
|
291 \end{itemize} |
|
292 \end{frame} |
|
293 |
|
294 \begin{frame}[fragile] |
|
295 \frametitle{Slicing arrays} |
|
296 \begin{lstlisting} |
|
297 >>> a = array([[1,2,3], [4,5,6], |
|
298 [7,8,9]]) |
|
299 >>> a[0,1:3] |
|
300 array([2, 3]) |
|
301 >>> a[1:,1:] |
|
302 array([[5, 6], |
|
303 [8, 9]]) |
|
304 >>> a[:,2] |
|
305 array([3, 6, 9]) |
|
306 \end{lstlisting} |
|
307 \end{frame} |
|
308 |
|
309 \begin{frame}[fragile] |
|
310 \frametitle{Striding arrays} |
|
311 \begin{lstlisting} |
|
312 >>> a[0::2,0::2] |
|
313 array([[1, 3], |
|
314 [7, 9]]) |
|
315 # Slices are references to the |
|
316 # same memory! |
|
317 \end{lstlisting} |
|
318 \end{frame} |
|
319 |
|
320 \begin{frame}[fragile] |
|
321 \frametitle{Random Numbers} |
|
322 \begin{lstlisting} |
|
323 >>> random.random() |
|
324 0.94134734326214331 |
|
325 >>> random.random(2) |
|
326 array([ 0.73955352, 0.49463645]) |
|
327 >>> random.random(3,2) |
|
328 array([[ 0.96276665, 0.77174861], |
|
329 [ 0.35138557, 0.61462271], |
|
330 [ 0.16789255, 0.43848811]]) |
|
331 \end{lstlisting} |
|
332 \inctime{15} |
|
333 \end{frame} |
|
334 |
|
335 \begin{frame}[fragile] |
|
336 \frametitle{Problem} |
|
337 Finite difference |
|
338 \begin{equation*} |
|
339 \frac{sin(x+h)-sin(x)}{h} |
|
340 \end{equation*} |
|
341 \begin{lstlisting} |
|
342 >>> x = linspace(0,2*pi,100) |
|
343 >>> y = sin(x) |
|
344 >>> deltax = x[1]-x[0] |
|
345 \end{lstlisting} |
|
346 \pause |
|
347 \begin{enumerate} |
|
348 \item Given this, get the finite difference of sin in the range 0 to 2*pi |
|
349 \end{enumerate} |
|
350 \end{frame} |
|
351 |
|
352 |
|
353 \begin{frame}[fragile] |
|
354 \frametitle{Advanced Problem} |
|
355 \begin{lstlisting} |
|
356 >>> from scipy import misc |
|
357 >>> A=misc.imread('filename') |
|
358 >>> misc.imshow(A) |
|
359 \end{lstlisting} |
|
360 \begin{enumerate} |
|
361 \item Convert an RGB image to Grayscale. $ Y = 0.5R + 0.25G + 0.25B $ |
|
362 \item Scale the image to 50\% |
|
363 \item Introduce some random noise |
|
364 \item Smooth the image using a mean filter |
|
365 \\\small{Each element in the array is replaced by mean of all the neighbouring elements} |
|
366 \\\small{How fast does your code run?} |
|
367 \end{enumerate} |
|
368 \inctime{15} |
|
369 \end{frame} |
|
370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
371 |
|
372 \section{2D Plotting} |
|
373 \subsection{Getting Started} |
|
374 |
|
375 \begin{frame} |
|
376 {IPython's \typ{pylab} mode} |
|
377 \begin{block}{Immediate use -} |
|
378 \typ{\$ ipython -pylab} |
|
379 \end{block} |
|
380 \begin{itemize} |
|
381 \item \typ{pylab}: convenient 2D plotting interface to MPL |
|
382 \item Imports all of pylab for you! |
|
383 \item Allows for interactive plotting |
|
384 \end{itemize} |
|
385 \end{frame} |
|
386 |
|
387 \begin{frame}[fragile] |
|
388 \frametitle{Basic 2D plotting} |
|
389 |
|
390 \begin{lstlisting} |
|
391 >>> x = linspace(0, 2*pi, 1000) |
|
392 >>> plot(x, sin(x)) |
|
393 >>> plot(x, sin(x), 'ro') |
|
394 >>> xlabel(r'$\chi$', color='g') |
|
395 # LaTeX markup! |
|
396 >>> ylabel(r'sin($\chi$)', color='r') |
|
397 >>> title('Simple figure', fontsize=20) |
|
398 >>> savefig('/tmp/test.png') |
|
399 \end{lstlisting} |
|
400 \begin{itemize} |
|
401 \item Also: PS, EPS, SVG, PDF |
|
402 \end{itemize} |
|
403 \inctime{5} |
|
404 \end{frame} |
|
405 |
|
406 \subsection{Plots - Lines, Labels and Legends} |
|
407 \begin{frame}[fragile] |
|
408 \frametitle{Tweaking plots} |
|
409 \begin{lstlisting} |
|
410 # Set properties of objects: |
|
411 >>> l, = plot(x, sin(x)) |
|
412 # Why "l,"? |
|
413 >>> setp(l, linewidth=2.0, color='r') |
|
414 >>> l.set_linewidth(2.0) |
|
415 >>> draw() # Redraw. |
|
416 >>> setp(l) # Print properties. |
|
417 >>> clf() # Clear figure. |
|
418 >>> close() # Close figure. |
|
419 \end{lstlisting} |
|
420 \end{frame} |
|
421 |
|
422 \begin{frame}[fragile] |
|
423 \frametitle{Working with text \ldots} |
|
424 %\begin{itemize} |
|
425 % \item We already saw LaTeX markup support! |
|
426 %\end{itemize} |
|
427 \begin{lstlisting} |
|
428 >>> w = arange(-2,2,.1) |
|
429 >>> plot(w,exp(-(w*w))*cos) |
|
430 >>> ylabel('$f(\omega)$') |
|
431 >>> xlabel('$\omega$') |
|
432 >>> title(r"$f(\omega)=e^{-\omega^2} |
|
433 cos({\omega^2})$") |
|
434 \end{lstlisting} |
|
435 |
|
436 \end{frame} |
|
437 |
|
438 \begin{frame}[fragile] |
|
439 \frametitle{Legends} |
|
440 \begin{lstlisting} |
|
441 >>> x = linspace(0, 2*pi, 1000) |
|
442 >>> plot(x, cos(5*x), 'r--', |
|
443 label='cosine') |
|
444 >>> plot(x, sin(5*x), 'g--', |
|
445 label='sine') |
|
446 >>> legend() |
|
447 # Or use: |
|
448 >>> legend(['cosine', 'sine']) |
|
449 \end{lstlisting} |
|
450 \end{frame} |
|
451 |
|
452 \begin{frame}[fragile] |
|
453 \frametitle{Multiple figures} |
|
454 |
|
455 \begin{lstlisting} |
|
456 >>> figure(1) |
|
457 >>> plot(x, sin(x)) |
|
458 >>> figure(2) |
|
459 >>> plot(x, tanh(x)) |
|
460 >>> figure(1) |
|
461 >>> title('Easy as 1,2,3') |
|
462 \end{lstlisting} |
|
463 |
|
464 |
|
465 \end{frame} |
|
466 |
|
467 \begin{frame}[fragile] |
|
468 \frametitle{\typ{pylab} in Python scripts} |
|
469 \begin{lstlisting} |
|
470 import pylab |
|
471 x = pylab.linspace(0, 20, 1000) |
|
472 pylab.plot(x, pylab.sin(x)) |
|
473 |
|
474 # Can also use: |
|
475 from pylab import linspace, sin, plot |
|
476 \end{lstlisting} |
|
477 \inctime{10} |
|
478 \end{frame} |
|
479 |
|
480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
481 \subsection{Types of Plots} |
|
482 \begin{frame}[fragile] |
|
483 \frametitle{X-Y plot} |
|
484 \begin{columns} |
|
485 \column{0.5\textwidth} |
|
486 \hspace*{-0.5in} |
|
487 \includegraphics[height=2in, interpolate=true]{data/xyplot} |
|
488 \column{0.45\textwidth} |
|
489 \begin{block}{Example code} |
|
490 \tiny |
|
491 \begin{lstlisting} |
|
492 t1 = arange(0.0, 5.0, 0.1) |
|
493 t2 = arange(0.0, 5.0, 0.02) |
|
494 t3 = arange(0.0, 2.0, 0.01) |
|
495 subplot(211) |
|
496 plot(t1, cos(2*pi*t1)*exp(-t1), 'bo', |
|
497 t2, cos(2*pi*t2)*exp(-t2), 'k') |
|
498 grid(True) |
|
499 title('A tale of 2 subplots') |
|
500 ylabel('Damped') |
|
501 subplot(212) |
|
502 plot(t3, cos(2*pi*t3), 'r--') |
|
503 grid(True) |
|
504 xlabel('time (s)') |
|
505 ylabel('Undamped') |
|
506 \end{lstlisting} |
|
507 \end{block} |
|
508 \end{columns} |
|
509 \end{frame} |
|
510 |
|
511 \begin{frame}[fragile] \frametitle{Semi-log and log-log plots} |
|
512 \begin{columns} |
|
513 \column{0.5\textwidth} |
|
514 \hspace*{-0.5in} |
|
515 \includegraphics[height=2in, interpolate=true]{data/log} |
|
516 \column{0.45\textwidth} |
|
517 \begin{block}{Example code} |
|
518 \tiny |
|
519 \begin{lstlisting} |
|
520 dt = 0.01 |
|
521 t = arange(dt, 20.0, dt) |
|
522 subplot(311) |
|
523 semilogy(t, exp(-t/5.0)) |
|
524 ylabel('semilogy') |
|
525 grid(True) |
|
526 subplot(312) |
|
527 semilogx(t, sin(2*pi*t)) |
|
528 ylabel('semilogx') |
|
529 grid(True) |
|
530 # minor grid on too |
|
531 gca().xaxis.grid(True, which='minor') |
|
532 subplot(313) |
|
533 loglog(t, 20*exp(-t/10.0), basex=4) |
|
534 grid(True) |
|
535 ylabel('loglog base 4 on x') |
|
536 \end{lstlisting} |
|
537 \end{block} |
|
538 \end{columns} |
|
539 \end{frame} |
|
540 |
|
541 \begin{frame}[fragile] \frametitle{Errorbar} |
|
542 \begin{columns} |
|
543 \column{0.5\textwidth} |
|
544 \hspace*{-0.5in} |
|
545 \includegraphics[height=2in, interpolate=true]{data/errorbar} |
|
546 \column{0.45\textwidth} |
|
547 \begin{block}{Example code} |
|
548 \tiny |
|
549 \begin{lstlisting} |
|
550 t = arange(0.1, 4, 0.1) |
|
551 s = exp(-t) |
|
552 e = 0.1*abs(randn(len(s))) |
|
553 f = 0.1*abs(randn(len(s))) |
|
554 g = 2*e |
|
555 h = 2*f |
|
556 errorbar(t, s, [e,g], f, fmt='o') |
|
557 xlabel('Distance (m)') |
|
558 ylabel('Height (m)') |
|
559 title('Mean and standard error '\ |
|
560 'as a function of distance') |
|
561 \end{lstlisting} |
|
562 \end{block} |
|
563 \end{columns} |
|
564 \end{frame} |
|
565 |
|
566 \begin{frame}[fragile] \frametitle{Histogram} |
|
567 \begin{columns} |
|
568 \column{0.5\textwidth} |
|
569 \hspace*{-0.5in} |
|
570 \includegraphics[height=2in, interpolate=true]{data/histogram} |
|
571 \column{0.45\textwidth} |
|
572 \begin{block}{Example code} |
|
573 \tiny |
|
574 \begin{lstlisting} |
|
575 mu, sigma = 100, 15 |
|
576 x = mu + sigma*randn(10000) |
|
577 # the histogram of the data |
|
578 n, bins, patches = hist(x, 100, normed=1) |
|
579 # add a 'best fit' line |
|
580 y = normpdf( bins, mu, sigma) |
|
581 l = plot(bins, y, 'r--', linewidth=2) |
|
582 xlim(40, 160) |
|
583 xlabel('Smarts') |
|
584 ylabel('P') |
|
585 title(r'$\rm{IQ:}\/ \mu=100,\/ \sigma=15$') |
|
586 \end{lstlisting} |
|
587 \end{block} |
|
588 \end{columns} |
|
589 \end{frame} |
|
590 |
|
591 \begin{frame}[fragile] \frametitle{Bar charts} |
|
592 \begin{columns} |
|
593 \column{0.5\textwidth} |
|
594 \hspace*{-0.5in} |
|
595 \includegraphics[height=2in, interpolate=true]{data/barchart} |
|
596 \column{0.45\textwidth} |
|
597 \begin{block}{Example code} |
|
598 \tiny |
|
599 \begin{lstlisting} |
|
600 N = 5 |
|
601 menMeans = (20, 35, 30, 35, 27) |
|
602 menStd = ( 2, 3, 4, 1, 2) |
|
603 # the x locations for the groups |
|
604 ind = arange(N) |
|
605 # the width of the bars |
|
606 width = 0.35 |
|
607 p1 = bar(ind, menMeans, width, |
|
608 color='r', yerr=menStd) |
|
609 womenMeans = (25, 32, 34, 20, 25) |
|
610 womenStd = ( 3, 5, 2, 3, 3) |
|
611 p2 = bar(ind+width, womenMeans, width, |
|
612 color='y', yerr=womenStd) |
|
613 ylabel('Scores') |
|
614 title('Scores by group and gender') |
|
615 xticks(ind+width, |
|
616 ('G1', 'G2', 'G3', 'G4', 'G5')) |
|
617 xlim(-width,len(ind)) |
|
618 yticks(arange(0,41,10)) |
|
619 legend((p1[0], p2[0]), |
|
620 ('Men', 'Women'), shadow=True) |
|
621 \end{lstlisting} |
|
622 \end{block} |
|
623 \end{columns} |
|
624 \end{frame} |
|
625 |
|
626 \begin{frame}[fragile] \frametitle{Pie charts} |
|
627 \begin{columns} |
|
628 \column{0.5\textwidth} |
|
629 \hspace*{-0.4in} |
|
630 \includegraphics[height=2.0in, interpolate=true]{data/piechart} |
|
631 \column{0.45\textwidth} |
|
632 \begin{block}{Example code} |
|
633 \tiny |
|
634 \begin{lstlisting} |
|
635 # make a square figure and axes |
|
636 figure(1, figsize=(8,8)) |
|
637 ax = axes([0.1, 0.1, 0.8, 0.8]) |
|
638 labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' |
|
639 fracs = [15,30,45, 10] |
|
640 explode=(0, 0.05, 0, 0) |
|
641 pie(fracs, explode=explode, labels=labels, |
|
642 autopct='%1.1f%%', shadow=True) |
|
643 title('Raining Hogs and Dogs', |
|
644 bbox={'facecolor':'0.8', 'pad':5}) |
|
645 \end{lstlisting} |
|
646 \end{block} |
|
647 \end{columns} |
|
648 \end{frame} |
|
649 |
|
650 \begin{frame}[fragile] \frametitle{Scatter plots} |
|
651 \begin{columns} |
|
652 \column{0.5\textwidth} |
|
653 \hspace*{-0.4in} |
|
654 \includegraphics[height=2in, interpolate=true]{data/scatter} |
|
655 \column{0.45\textwidth} |
|
656 \begin{block}{Example code} |
|
657 \tiny |
|
658 \begin{lstlisting} |
|
659 N = 30 |
|
660 x = 0.9*rand(N) |
|
661 y = 0.9*rand(N) |
|
662 # 0 to 10 point radiuses |
|
663 area = pi*(10 * rand(N))**2 |
|
664 volume = 400 + rand(N)*450 |
|
665 scatter(x,y,s=area, marker='o', c=volume, |
|
666 alpha=0.75) |
|
667 xlabel(r'$\Delta_i$', size='x-large') |
|
668 ylabel(r'$\Delta_{i+1}$', size='x-large') |
|
669 title(r'Volume and percent change') |
|
670 grid(True) |
|
671 colorbar() |
|
672 savefig('scatter') |
|
673 \end{lstlisting} |
|
674 \end{block} |
|
675 \end{columns} |
|
676 \end{frame} |
|
677 |
|
678 \begin{frame}[fragile] \frametitle{Polar} |
|
679 \begin{columns} |
|
680 \column{0.5\textwidth} |
|
681 \hspace*{-0.5in} |
|
682 \includegraphics[height=2in, interpolate=true]{data/polar} |
|
683 \column{0.45\textwidth} |
|
684 \begin{block}{Example code} |
|
685 \tiny |
|
686 \begin{lstlisting} |
|
687 figure(figsize=(8,8)) |
|
688 ax = axes([0.1, 0.1, 0.8, 0.8], |
|
689 polar=True, |
|
690 axisbg='#d5de9c') |
|
691 r = arange(0,1,0.001) |
|
692 theta = 2*2*pi*r |
|
693 polar(theta, r, color='#ee8d18', lw=3) |
|
694 # the radius of the grid labels |
|
695 setp(ax.thetagridlabels, y=1.075) |
|
696 title(r'$\theta=4\pi r$', fontsize=20) |
|
697 \end{lstlisting} |
|
698 |
|
699 \end{block} |
|
700 \end{columns} |
|
701 \end{frame} |
|
702 |
|
703 \begin{frame}[fragile] \frametitle{Contours} |
|
704 \begin{columns} |
|
705 \column{0.45\textwidth} |
|
706 \hspace*{-0.5in} |
|
707 \includegraphics[height=2in, interpolate=true]{data/contour} |
|
708 \column{0.525\textwidth} |
|
709 \begin{block}{Example code} |
|
710 \tiny |
|
711 \begin{lstlisting} |
|
712 x = arange(-3.0, 3.0, 0.025) |
|
713 y = arange(-2.0, 2.0, 0.025) |
|
714 X, Y = meshgrid(x, y) |
|
715 Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) |
|
716 Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) |
|
717 # difference of Gaussians |
|
718 Z = 10.0 * (Z2 - Z1) |
|
719 im = imshow(Z, interpolation='bilinear', |
|
720 origin='lower', |
|
721 cmap=cm.gray, extent=(-3,3,-2,2)) |
|
722 levels = arange(-1.2, 1.6, 0.2) |
|
723 # label every second level |
|
724 clabel(CS, levels[1::2], inline=1, |
|
725 fmt='%1.1f', fontsize=14) |
|
726 CS = contour(Z, levels, |
|
727 origin='lower', |
|
728 linewidths=2, |
|
729 extent=(-3,3,-2,2)) |
|
730 # make a colorbar for the contour lines |
|
731 CB = colorbar(CS, shrink=0.8, extend='both') |
|
732 title('Lines with colorbar') |
|
733 hot(); flag() |
|
734 \end{lstlisting} |
|
735 \end{block} |
|
736 \end{columns} |
|
737 \end{frame} |
|
738 |
|
739 \begin{frame}[fragile] \frametitle{Velocity vectors} |
|
740 \begin{columns} |
|
741 \column{0.5\textwidth} |
|
742 \hspace*{-0.5in} |
|
743 \includegraphics[height=2in, interpolate=true]{data/quiver} |
|
744 \column{0.45\textwidth} |
|
745 \begin{block}{Example code} |
|
746 \tiny |
|
747 \begin{lstlisting} |
|
748 X,Y = meshgrid(arange(0,2*pi,.2), |
|
749 arange(0,2*pi,.2) ) |
|
750 U = cos(X) |
|
751 V = sin(Y) |
|
752 Q = quiver(X[::3, ::3], Y[::3, ::3], |
|
753 U[::3, ::3], V[::3, ::3], |
|
754 color='r', units='x', |
|
755 linewidths=(2,), |
|
756 edgecolors=('k'), |
|
757 headaxislength=5 ) |
|
758 qk = quiverkey(Q, 0.5, 0.03, 1, '1 m/s', |
|
759 fontproperties= |
|
760 {'weight': 'bold'}) |
|
761 axis([-1, 7, -1, 7]) |
|
762 title('triangular head; scale '\ |
|
763 'with x view; black edges') |
|
764 \end{lstlisting} |
|
765 \end{block} |
|
766 \end{columns} |
|
767 \end{frame} |
|
768 |
|
769 \begin{frame}[fragile] \frametitle{Maps} |
|
770 \includegraphics[height=2.3in, interpolate=true]{data/plotmap} |
|
771 \begin{center} |
|
772 \tiny |
|
773 For details see \url{http://matplotlib.sourceforge.net/screenshots/plotmap.py} |
|
774 \end{center} |
|
775 \end{frame} |
|
776 |
|
777 |
|
778 \begin{frame} |
|
779 \frametitle{More information} |
|
780 \begin{itemize} |
|
781 \item More information here: \url{http://matplotlib.sf.net} |
|
782 \item \url{http://matplotlib.sf.net/tutorial.html} |
|
783 \item \url{http://matplotlib.sf.net/screenshots.html} |
|
784 \end{itemize} |
|
785 \inctime{5} |
|
786 \end{frame} |
|
787 |
|
788 \begin{frame} |
|
789 \frametitle{Problem Set} |
|
790 \begin{itemize} |
|
791 \item[1] Write a function that plots any regular n-gon given \typ{n}. |
|
792 \item[2] Consider the logistic map, $f(x) = kx(1-x)$, plot it for |
|
793 $k=2.5, 3.5$ and $4$ in the same plot. |
|
794 \end{itemize} |
|
795 \end{frame} |
|
796 |
|
797 \begin{frame}[fragile] |
|
798 \frametitle{Problem Set} |
|
799 \begin{columns} |
|
800 \column{0.6\textwidth} |
|
801 \small{ |
|
802 \begin{itemize} |
|
803 \item[3] Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) = kx(1-x)$. Plot the successive iterates of this process as explained below. |
|
804 \end{itemize}} |
|
805 \column{0.35\textwidth} |
|
806 \hspace*{-0.5in} |
|
807 \includegraphics[height=1.6in, interpolate=true]{data/cobweb} |
|
808 \end{columns} |
|
809 \end{frame} |
|
810 |
|
811 \begin{frame} |
|
812 |
|
813 Plot the cobweb plot as follows: |
|
814 \begin{enumerate} |
|
815 \item Start at $(x_0, 0)$ ($\implies$ i=0) |
|
816 \item Draw a line to $(x_i, f(x_i))$ |
|
817 \item Set $x_{i+1} = f(x_i)$ |
|
818 \item Draw a line to $(x_{i+1}, x_{i+1})$ |
|
819 \item $(i\implies i+1)$ |
|
820 \item Repeat from 2 for as long as you want |
|
821 \end{enumerate} |
|
822 \inctime{20} |
|
823 \end{frame} |
|
824 |
|
825 \begin{frame}{Summary} |
|
826 \begin{itemize} |
|
827 \item Basics of Numpy. |
|
828 \item Array operations. |
|
829 \item Plotting in 2D. |
|
830 \end{itemize} |
|
831 \end{frame} |
|
832 \end{document} |
|