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}
|
|
161 |
\end{frame}
|
|
162 |
|
|
163 |
\begin{frame}
|
|
164 |
\frametitle{Basic concepts}
|
|
165 |
\begin{itemize}
|
|
166 |
\item fixed size (\typ{arr.size});
|
|
167 |
\item Same type (\typ{arr.dtype}) of data
|
|
168 |
\item arbitrary dimensionality
|
|
169 |
\item \typ{arr.shape}: size in each dimension
|
|
170 |
\item \alert{Note:} \typ{len(arr) != arr.size} in general
|
|
171 |
\item \alert{Note:} By default array operations are performed
|
|
172 |
\alert{elementwise}
|
|
173 |
\item Indices, slicing: just like lists
|
|
174 |
\end{itemize}
|
|
175 |
\end{frame}
|
|
176 |
|
|
177 |
|
|
178 |
\begin{frame}[fragile]
|
|
179 |
\frametitle{More examples of \num}
|
|
180 |
\vspace*{-8pt}
|
|
181 |
\begin{lstlisting}
|
|
182 |
>>> x = array([1., 2, 3, 4])
|
|
183 |
>>> size(x)
|
|
184 |
4
|
|
185 |
>>> x.dtype # What is a.dtype?
|
|
186 |
dtype('float64')
|
|
187 |
>>> x.shape
|
|
188 |
(4,)
|
|
189 |
>>> print rank(x), x.itemsize
|
|
190 |
1 8
|
|
191 |
>>> x[0] = 10
|
|
192 |
>>> print x[0], x[-1]
|
|
193 |
10.0 4.0
|
|
194 |
\end{lstlisting}
|
|
195 |
|
|
196 |
\inctime{10}
|
|
197 |
\end{frame}
|
|
198 |
|
|
199 |
\begin{frame}
|
|
200 |
{More Numpy}
|
|
201 |
|
|
202 |
\begin{itemize}
|
|
203 |
\item Multi-dimensional arrays
|
|
204 |
\item Random number generation
|
|
205 |
\end{itemize}
|
|
206 |
|
|
207 |
\end{frame}
|
|
208 |
|
|
209 |
\begin{frame}[fragile]
|
|
210 |
\frametitle{Multi-dimensional arrays}
|
|
211 |
\begin{lstlisting}
|
|
212 |
>>> a = array([[ 0, 1, 2, 3],
|
|
213 |
... [10,11,12,13]])
|
|
214 |
>>> a.shape # (rows, columns)
|
|
215 |
(2, 4)
|
|
216 |
# Accessing and setting values
|
|
217 |
>>> a[1,3]
|
|
218 |
13
|
|
219 |
>>> a[1,3] = -1
|
|
220 |
>>> a[1] # The second row
|
|
221 |
array([10,11,12,-1])
|
|
222 |
|
|
223 |
\end{lstlisting}
|
|
224 |
\end{frame}
|
|
225 |
|
|
226 |
\begin{frame}[fragile]
|
|
227 |
\frametitle{Slicing arrays}
|
|
228 |
\begin{lstlisting}
|
|
229 |
>>> a = array([[1,2,3], [4,5,6],
|
|
230 |
[7,8,9]])
|
|
231 |
>>> a[0,1:3]
|
|
232 |
array([2, 3])
|
|
233 |
>>> a[1:,1:]
|
|
234 |
array([[5, 6],
|
|
235 |
[8, 9]])
|
|
236 |
>>> a[:,2]
|
|
237 |
array([3, 6, 9])
|
|
238 |
\end{lstlisting}
|
|
239 |
\end{frame}
|
|
240 |
\begin{frame}[fragile]
|
|
241 |
\frametitle{Striding arrays}
|
|
242 |
\begin{lstlisting}
|
|
243 |
>>> a[0::2,0::2]
|
|
244 |
array([[1, 3],
|
|
245 |
[7, 9]])
|
|
246 |
# Slices are references to the
|
|
247 |
# same memory!
|
|
248 |
\end{lstlisting}
|
|
249 |
\end{frame}
|
|
250 |
|
|
251 |
\subsection{Array creation }
|
|
252 |
\begin{frame}[fragile]
|
|
253 |
\frametitle{Array creation functions}
|
|
254 |
\begin{itemize}
|
|
255 |
\item \typ{array(object, dtype=None, \ldots)}
|
|
256 |
\item \typ{arange(start, stop=None, step=1 \ldots)}
|
|
257 |
\item \typ{linspace(start, stop, num=50, \ldots)}
|
|
258 |
\item \typ{ones(shape, dtype=None, \ldots)}
|
|
259 |
\item \typ{zeros(shape, dtype=float,\ldots)}
|
|
260 |
\item \typ{identity(n)}
|
|
261 |
\item \typ{empty(shape, dtype=float,\ldots)}
|
|
262 |
\item \typ{ones\_like(x)},
|
|
263 |
\item \typ{zeros\_like(x)}, \typ{empty\_like(x)}
|
|
264 |
\end{itemize}
|
|
265 |
\end{frame}
|
|
266 |
|
|
267 |
\subsection{Array math}
|
|
268 |
\begin{frame}[fragile]
|
|
269 |
\frametitle{Array math}
|
|
270 |
\begin{itemize}
|
|
271 |
\item Basic \alert{elementwise} math (given two arrays \typ{a, b}):
|
|
272 |
\typ{+, -, *, /, \%}
|
|
273 |
\item Inplace operators: \typ{a += b}, or \typ{add(a, b,
|
|
274 |
a)} etc.
|
|
275 |
\item Logical operations: \typ{equal (==)}, \typ{not\_equal (!=)},
|
|
276 |
\typ{less (<)}, \typ{greater (>)} etc.
|
|
277 |
\item Trig and other functions: \typ{sin(x), arcsin(x), sinh(x),
|
|
278 |
exp(x), sqrt(x)} etc.
|
|
279 |
\item \typ{sum(x, axis=0), product(x, axis=0)}
|
|
280 |
\item \typ{dot(a, bp)}
|
|
281 |
\end{itemize}
|
|
282 |
\end{frame}
|
|
283 |
|
|
284 |
|
|
285 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
286 |
\section{2D Plotting}
|
|
287 |
\subsection{Getting Started}
|
|
288 |
|
|
289 |
\begin{frame}
|
|
290 |
{IPython's \typ{pylab} mode}
|
|
291 |
\begin{itemize}
|
|
292 |
\item \typ{pylab}: convenient 2D plotting interface to MPL
|
|
293 |
\item Immediate use: \typ{ipython -pylab}
|
|
294 |
\item Imports all of pylab for you!
|
|
295 |
\item Allows for interactive plotting
|
|
296 |
\end{itemize}
|
|
297 |
\end{frame}
|
|
298 |
|
|
299 |
\begin{frame}[fragile]
|
|
300 |
\frametitle{Basic 2D plotting}
|
|
301 |
|
|
302 |
\begin{lstlisting}
|
|
303 |
>>> x = linspace(0, 2*pi, 1000)
|
|
304 |
>>> plot(x, sin(x))
|
|
305 |
>>> plot(x, sin(x), 'ro')
|
|
306 |
>>> xlabel(r'$\chi$', color='g')
|
|
307 |
# LaTeX markup!
|
|
308 |
>>> ylabel(r'sin($\chi$)', color='r')
|
|
309 |
>>> title('Simple figure', fontsize=20)
|
|
310 |
>>> savefig('/tmp/test.eps')
|
|
311 |
\end{lstlisting}
|
|
312 |
\begin{itemize}
|
|
313 |
\item Also: PNG, PDF, PS, EPS, SVG, PDF
|
|
314 |
\end{itemize}
|
|
315 |
\end{frame}
|
|
316 |
|
|
317 |
\subsection{Playing with lines}
|
|
318 |
\begin{frame}[fragile]
|
|
319 |
\frametitle{Basic plotting \ldots}
|
|
320 |
\begin{lstlisting}
|
|
321 |
# Set properties of objects:
|
|
322 |
>>> l, = plot(x, sin(x))
|
|
323 |
# Why "l,"?
|
|
324 |
>>> setp(l, linewidth=2.0, color='r')
|
|
325 |
>>> l.set_linewidth(2.0)
|
|
326 |
>>> draw() # Redraw.
|
|
327 |
>>> setp(l) # Print properties
|
|
328 |
>>> clf() # Clear figure.
|
|
329 |
>>> close() # Close figure.
|
|
330 |
\end{lstlisting}
|
|
331 |
\end{frame}
|
|
332 |
|
|
333 |
\begin{frame}[fragile]
|
|
334 |
\frametitle{Multiple figures}
|
|
335 |
|
|
336 |
\begin{lstlisting}
|
|
337 |
>>> figure(1)
|
|
338 |
>>> plot(x, sin(x))
|
|
339 |
>>> figure(2)
|
|
340 |
>>> plot(x, tanh(x))
|
|
341 |
>>> figure(1)
|
|
342 |
>>> title('Easy as 1,2,3')
|
|
343 |
\end{lstlisting}
|
|
344 |
|
|
345 |
\end{frame}
|
|
346 |
|
|
347 |
\subsection{Handling Text}
|
|
348 |
|
|
349 |
\begin{frame}[fragile]
|
|
350 |
\frametitle{Legends and Annotation}
|
|
351 |
\begin{lstlisting}
|
|
352 |
>>> plot(x, cos(5*x), 'r--',
|
|
353 |
label='cosine')
|
|
354 |
>>> plot(x, sin(5*x), 'g--',
|
|
355 |
label='sine')
|
|
356 |
>>> legend()
|
|
357 |
# Or use:
|
|
358 |
>>> legend(['cosine', 'sine'])
|
|
359 |
# Annotation:
|
|
360 |
>>> text(1,0, '(1,0)')
|
|
361 |
\end{lstlisting}
|
|
362 |
\end{frame}
|
|
363 |
|
|
364 |
\begin{frame}[fragile]
|
|
365 |
\frametitle{More commands \ldots}
|
|
366 |
\begin{lstlisting}
|
|
367 |
# semilog, loglog
|
|
368 |
>>> x = 10.**(-arange(100)*0.1)
|
|
369 |
>>> semilogx(x, x)
|
|
370 |
>>> semilogy(x, x)
|
|
371 |
>>> loglog(x, x)
|
|
372 |
>>> loglog(x, x*x)
|
|
373 |
\end{lstlisting}
|
|
374 |
\end{frame}
|
|
375 |
|
|
376 |
\begin{frame}[fragile]
|
|
377 |
\frametitle{More plots \ldots}
|
|
378 |
\begin{lstlisting}
|
|
379 |
>>> clf()
|
|
380 |
>>> t = arange(0.1, 4, 0.1)
|
|
381 |
>>> s = exp(-t)
|
|
382 |
>>> e = 0.1*abs(randn(len(s)))
|
|
383 |
>>> errorbar(t, s, e)
|
|
384 |
# Scatter plots
|
|
385 |
>>> clf()
|
|
386 |
>>> t = randn(len(e))
|
|
387 |
>>> scatter(t, e, c=s)
|
|
388 |
\end{lstlisting}
|
|
389 |
\end{frame}
|
|
390 |
|
|
391 |
\begin{frame}[fragile]
|
|
392 |
\frametitle{Note: \typ{pylab} in Python scripts}
|
|
393 |
\begin{lstlisting}
|
|
394 |
import pylab
|
|
395 |
x = pylab.linspace(0, 20, 1000)
|
|
396 |
pylab.plot(x, pylab.sin(x))
|
|
397 |
|
|
398 |
# Can also use:
|
|
399 |
from pylab import linspace, sin, plot
|
|
400 |
\end{lstlisting}
|
|
401 |
\end{frame}
|
|
402 |
|
|
403 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
404 |
\subsection{Types of Plots}
|
|
405 |
\begin{frame}[fragile]
|
|
406 |
\frametitle{X-Y plot}
|
|
407 |
\begin{columns}
|
|
408 |
\column{0.5\textwidth}
|
|
409 |
\hspace*{-0.5in}
|
|
410 |
\includegraphics[height=2in, interpolate=true]{data/xyplot}
|
|
411 |
\column{0.45\textwidth}
|
|
412 |
\begin{block}{Example code}
|
|
413 |
\tiny
|
|
414 |
\begin{lstlisting}
|
|
415 |
t1 = arange(0.0, 5.0, 0.1)
|
|
416 |
t2 = arange(0.0, 5.0, 0.02)
|
|
417 |
t3 = arange(0.0, 2.0, 0.01)
|
|
418 |
subplot(211)
|
|
419 |
plot(t1, cos(2*pi*t1)*exp(-t1), 'bo',
|
|
420 |
t2, cos(2*pi*t2)*exp(-t2), 'k')
|
|
421 |
grid(True)
|
|
422 |
title('A tale of 2 subplots')
|
|
423 |
ylabel('Damped')
|
|
424 |
subplot(212)
|
|
425 |
plot(t3, cos(2*pi*t3), 'r--')
|
|
426 |
grid(True)
|
|
427 |
xlabel('time (s)')
|
|
428 |
ylabel('Undamped')
|
|
429 |
\end{lstlisting}
|
|
430 |
\end{block}
|
|
431 |
\end{columns}
|
|
432 |
\end{frame}
|
|
433 |
|
|
434 |
\begin{frame}[fragile] \frametitle{Semi-log and log-log plots}
|
|
435 |
\begin{columns}
|
|
436 |
\column{0.5\textwidth}
|
|
437 |
\hspace*{-0.5in}
|
|
438 |
\includegraphics[height=2in, interpolate=true]{data/log}
|
|
439 |
\column{0.45\textwidth}
|
|
440 |
\begin{block}{Example code}
|
|
441 |
\tiny
|
|
442 |
\begin{lstlisting}
|
|
443 |
dt = 0.01
|
|
444 |
t = arange(dt, 20.0, dt)
|
|
445 |
subplot(311)
|
|
446 |
semilogy(t, exp(-t/5.0))
|
|
447 |
ylabel('semilogy')
|
|
448 |
grid(True)
|
|
449 |
subplot(312)
|
|
450 |
semilogx(t, sin(2*pi*t))
|
|
451 |
ylabel('semilogx')
|
|
452 |
grid(True)
|
|
453 |
# minor grid on too
|
|
454 |
gca().xaxis.grid(True, which='minor')
|
|
455 |
subplot(313)
|
|
456 |
loglog(t, 20*exp(-t/10.0), basex=4)
|
|
457 |
grid(True)
|
|
458 |
ylabel('loglog base 4 on x')
|
|
459 |
\end{lstlisting}
|
|
460 |
\end{block}
|
|
461 |
\end{columns}
|
|
462 |
\end{frame}
|
|
463 |
|
|
464 |
\begin{frame}[fragile] \frametitle{Errorbar}
|
|
465 |
\begin{columns}
|
|
466 |
\column{0.5\textwidth}
|
|
467 |
\hspace*{-0.5in}
|
|
468 |
\includegraphics[height=2in, interpolate=true]{data/errorbar}
|
|
469 |
\column{0.45\textwidth}
|
|
470 |
\begin{block}{Example code}
|
|
471 |
\tiny
|
|
472 |
\begin{lstlisting}
|
|
473 |
t = arange(0.1, 4, 0.1)
|
|
474 |
s = exp(-t)
|
|
475 |
e = 0.1*abs(randn(len(s)))
|
|
476 |
f = 0.1*abs(randn(len(s)))
|
|
477 |
g = 2*e
|
|
478 |
h = 2*f
|
|
479 |
errorbar(t, s, [e,g], f, fmt='o')
|
|
480 |
xlabel('Distance (m)')
|
|
481 |
ylabel('Height (m)')
|
|
482 |
title('Mean and standard error '\
|
|
483 |
'as a function of distance')
|
|
484 |
\end{lstlisting}
|
|
485 |
\end{block}
|
|
486 |
\end{columns}
|
|
487 |
\end{frame}
|
|
488 |
|
|
489 |
\begin{frame}[fragile] \frametitle{Histogram}
|
|
490 |
\begin{columns}
|
|
491 |
\column{0.5\textwidth}
|
|
492 |
\hspace*{-0.5in}
|
|
493 |
\includegraphics[height=2in, interpolate=true]{data/histogram}
|
|
494 |
\column{0.45\textwidth}
|
|
495 |
\begin{block}{Example code}
|
|
496 |
\tiny
|
|
497 |
\begin{lstlisting}
|
|
498 |
mu, sigma = 100, 15
|
|
499 |
x = mu + sigma*randn(10000)
|
|
500 |
# the histogram of the data
|
|
501 |
n, bins, patches = hist(x, 100, normed=1)
|
|
502 |
# add a 'best fit' line
|
|
503 |
y = normpdf( bins, mu, sigma)
|
|
504 |
l = plot(bins, y, 'r--', linewidth=2)
|
|
505 |
xlim(40, 160)
|
|
506 |
xlabel('Smarts')
|
|
507 |
ylabel('P')
|
|
508 |
title(r'$\rm{IQ:}\/ \mu=100,\/ \sigma=15$')
|
|
509 |
\end{lstlisting}
|
|
510 |
\end{block}
|
|
511 |
\end{columns}
|
|
512 |
\end{frame}
|
|
513 |
|
|
514 |
\begin{frame}[fragile] \frametitle{Bar charts}
|
|
515 |
\begin{columns}
|
|
516 |
\column{0.5\textwidth}
|
|
517 |
\hspace*{-0.5in}
|
|
518 |
\includegraphics[height=2in, interpolate=true]{data/barchart}
|
|
519 |
\column{0.45\textwidth}
|
|
520 |
\begin{block}{Example code}
|
|
521 |
\tiny
|
|
522 |
\begin{lstlisting}
|
|
523 |
N = 5
|
|
524 |
menMeans = (20, 35, 30, 35, 27)
|
|
525 |
menStd = ( 2, 3, 4, 1, 2)
|
|
526 |
# the x locations for the groups
|
|
527 |
ind = arange(N)
|
|
528 |
# the width of the bars
|
|
529 |
width = 0.35
|
|
530 |
p1 = bar(ind, menMeans, width,
|
|
531 |
color='r', yerr=menStd)
|
|
532 |
womenMeans = (25, 32, 34, 20, 25)
|
|
533 |
womenStd = ( 3, 5, 2, 3, 3)
|
|
534 |
p2 = bar(ind+width, womenMeans, width,
|
|
535 |
color='y', yerr=womenStd)
|
|
536 |
ylabel('Scores')
|
|
537 |
title('Scores by group and gender')
|
|
538 |
xticks(ind+width,
|
|
539 |
('G1', 'G2', 'G3', 'G4', 'G5'))
|
|
540 |
xlim(-width,len(ind))
|
|
541 |
yticks(arange(0,41,10))
|
|
542 |
legend((p1[0], p2[0]),
|
|
543 |
('Men', 'Women'), shadow=True)
|
|
544 |
\end{lstlisting}
|
|
545 |
\end{block}
|
|
546 |
\end{columns}
|
|
547 |
\end{frame}
|
|
548 |
|
|
549 |
\begin{frame}[fragile] \frametitle{Pie charts}
|
|
550 |
\begin{columns}
|
|
551 |
\column{0.5\textwidth}
|
|
552 |
\hspace*{-0.4in}
|
|
553 |
\includegraphics[height=2.0in, interpolate=true]{data/piechart}
|
|
554 |
\column{0.45\textwidth}
|
|
555 |
\begin{block}{Example code}
|
|
556 |
\tiny
|
|
557 |
\begin{lstlisting}
|
|
558 |
# make a square figure and axes
|
|
559 |
figure(1, figsize=(8,8))
|
|
560 |
ax = axes([0.1, 0.1, 0.8, 0.8])
|
|
561 |
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
|
|
562 |
fracs = [15,30,45, 10]
|
|
563 |
explode=(0, 0.05, 0, 0)
|
|
564 |
pie(fracs, explode=explode, labels=labels,
|
|
565 |
autopct='%1.1f%%', shadow=True)
|
|
566 |
title('Raining Hogs and Dogs',
|
|
567 |
bbox={'facecolor':'0.8', 'pad':5})
|
|
568 |
\end{lstlisting}
|
|
569 |
\end{block}
|
|
570 |
\end{columns}
|
|
571 |
\end{frame}
|
|
572 |
|
|
573 |
\begin{frame}[fragile] \frametitle{Scatter plots}
|
|
574 |
\begin{columns}
|
|
575 |
\column{0.5\textwidth}
|
|
576 |
\hspace*{-0.4in}
|
|
577 |
\includegraphics[height=2in, interpolate=true]{data/scatter}
|
|
578 |
\column{0.45\textwidth}
|
|
579 |
\begin{block}{Example code}
|
|
580 |
\tiny
|
|
581 |
\begin{lstlisting}
|
|
582 |
N = 30
|
|
583 |
x = 0.9*rand(N)
|
|
584 |
y = 0.9*rand(N)
|
|
585 |
# 0 to 10 point radiuses
|
|
586 |
area = pi*(10 * rand(N))**2
|
|
587 |
volume = 400 + rand(N)*450
|
|
588 |
scatter(x,y,s=area, marker='o', c=volume,
|
|
589 |
alpha=0.75)
|
|
590 |
xlabel(r'$\Delta_i$', size='x-large')
|
|
591 |
ylabel(r'$\Delta_{i+1}$', size='x-large')
|
|
592 |
title(r'Volume and percent change')
|
|
593 |
grid(True)
|
|
594 |
colorbar()
|
|
595 |
savefig('scatter')
|
|
596 |
\end{lstlisting}
|
|
597 |
\end{block}
|
|
598 |
\end{columns}
|
|
599 |
\end{frame}
|
|
600 |
|
|
601 |
\begin{frame}[fragile] \frametitle{Polar}
|
|
602 |
\begin{columns}
|
|
603 |
\column{0.5\textwidth}
|
|
604 |
\hspace*{-0.5in}
|
|
605 |
\includegraphics[height=2in, interpolate=true]{data/polar}
|
|
606 |
\column{0.45\textwidth}
|
|
607 |
\begin{block}{Example code}
|
|
608 |
\tiny
|
|
609 |
\begin{lstlisting}
|
|
610 |
figure(figsize=(8,8))
|
|
611 |
ax = axes([0.1, 0.1, 0.8, 0.8],
|
|
612 |
polar=True,
|
|
613 |
axisbg='#d5de9c')
|
|
614 |
r = arange(0,1,0.001)
|
|
615 |
theta = 2*2*pi*r
|
|
616 |
polar(theta, r, color='#ee8d18', lw=3)
|
|
617 |
# the radius of the grid labels
|
|
618 |
setp(ax.thetagridlabels, y=1.075)
|
|
619 |
title(r"$\theta=4\pi r", fontsize=20)
|
|
620 |
\end{lstlisting}
|
|
621 |
\end{block}
|
|
622 |
\end{columns}
|
|
623 |
\end{frame}
|
|
624 |
|
|
625 |
\begin{frame}[fragile] \frametitle{Contours}
|
|
626 |
\begin{columns}
|
|
627 |
\column{0.45\textwidth}
|
|
628 |
\hspace*{-0.5in}
|
|
629 |
\includegraphics[height=2in, interpolate=true]{data/contour}
|
|
630 |
\column{0.525\textwidth}
|
|
631 |
\begin{block}{Example code}
|
|
632 |
\tiny
|
|
633 |
\begin{lstlisting}
|
|
634 |
x = arange(-3.0, 3.0, 0.025)
|
|
635 |
y = arange(-2.0, 2.0, 0.025)
|
|
636 |
X, Y = meshgrid(x, y)
|
|
637 |
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
|
|
638 |
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
|
|
639 |
# difference of Gaussians
|
|
640 |
Z = 10.0 * (Z2 - Z1)
|
|
641 |
im = imshow(Z, interpolation='bilinear',
|
|
642 |
origin='lower',
|
|
643 |
cmap=cm.gray, extent=(-3,3,-2,2))
|
|
644 |
levels = arange(-1.2, 1.6, 0.2)
|
|
645 |
# label every second level
|
|
646 |
clabel(CS, levels[1::2], inline=1,
|
|
647 |
fmt='%1.1f', fontsize=14)
|
|
648 |
CS = contour(Z, levels,
|
|
649 |
origin='lower',
|
|
650 |
linewidths=2,
|
|
651 |
extent=(-3,3,-2,2))
|
|
652 |
# make a colorbar for the contour lines
|
|
653 |
CB = colorbar(CS, shrink=0.8, extend='both')
|
|
654 |
title('Lines with colorbar')
|
|
655 |
hot(); flag()
|
|
656 |
\end{lstlisting}
|
|
657 |
\end{block}
|
|
658 |
\end{columns}
|
|
659 |
\end{frame}
|
|
660 |
|
|
661 |
\begin{frame}[fragile] \frametitle{Velocity vectors}
|
|
662 |
\begin{columns}
|
|
663 |
\column{0.5\textwidth}
|
|
664 |
\hspace*{-0.5in}
|
|
665 |
\includegraphics[height=2in, interpolate=true]{data/quiver}
|
|
666 |
\column{0.45\textwidth}
|
|
667 |
\begin{block}{Example code}
|
|
668 |
\tiny
|
|
669 |
\begin{lstlisting}
|
|
670 |
X,Y = meshgrid(arange(0,2*pi,.2),
|
|
671 |
arange(0,2*pi,.2) )
|
|
672 |
U = cos(X)
|
|
673 |
V = sin(Y)
|
|
674 |
Q = quiver(X[::3, ::3], Y[::3, ::3],
|
|
675 |
U[::3, ::3], V[::3, ::3],
|
|
676 |
color='r', units='x',
|
|
677 |
linewidths=(2,),
|
|
678 |
edgecolors=('k'),
|
|
679 |
headaxislength=5 )
|
|
680 |
qk = quiverkey(Q, 0.5, 0.03, 1, '1 m/s',
|
|
681 |
fontproperties=
|
|
682 |
{'weight': 'bold'})
|
|
683 |
axis([-1, 7, -1, 7])
|
|
684 |
title('triangular head; scale '\
|
|
685 |
'with x view; black edges')
|
|
686 |
\end{lstlisting}
|
|
687 |
\end{block}
|
|
688 |
\end{columns}
|
|
689 |
\end{frame}
|
|
690 |
|
|
691 |
\begin{frame}[fragile] \frametitle{Maps}
|
|
692 |
\includegraphics[height=2.5in, interpolate=true]{data/plotmap}
|
|
693 |
\begin{center}
|
|
694 |
\tiny
|
|
695 |
For details see \url{http://matplotlib.sourceforge.net/screenshots/plotmap.py}
|
|
696 |
\end{center}
|
|
697 |
\end{frame}
|
|
698 |
|
|
699 |
|
|
700 |
\begin{frame}
|
|
701 |
\frametitle{More information}
|
|
702 |
\begin{itemize}
|
|
703 |
\item More information here: \url{http://matplotlib.sf.net}
|
|
704 |
\item \url{http://matplotlib.sf.net/tutorial.html}
|
|
705 |
\item \url{http://matplotlib.sf.net/screenshots.html}
|
|
706 |
\end{itemize}
|
|
707 |
|
|
708 |
\inctime{25}
|
|
709 |
\end{frame}
|
|
710 |
|
|
711 |
\begin{frame}
|
|
712 |
\frametitle{Problem set 1.0}
|
|
713 |
\begin{enumerate}
|
|
714 |
\item Write a function that plots any n-gon given \typ{n}.
|
|
715 |
\item Consider the logistic map, $f(x) = kx(1-x)$, plot it for
|
|
716 |
$k=2.5, 3.5$ and $4$
|
|
717 |
\end{enumerate}
|
|
718 |
\end{frame}
|
|
719 |
|
|
720 |
\begin{frame}
|
|
721 |
\frametitle{Problem set 1.1}
|
|
722 |
\begin{enumerate}
|
|
723 |
\item Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) =
|
|
724 |
kx(1-x)$. Plot the successive iterates of this process.
|
|
725 |
\item Plot this using a cobweb plot as follows:
|
|
726 |
\begin{enumerate}
|
|
727 |
\item Start at $(x_0, 0)$
|
|
728 |
\item Draw line to $(x_i, f(x_i))$;
|
|
729 |
\item Set $x_{i+1} = f(x_i)$
|
|
730 |
\item Draw line to $(x_i, x_i)$
|
|
731 |
\item Repeat from 2 for as long as you want
|
|
732 |
\end{enumerate}
|
|
733 |
\end{enumerate}
|
|
734 |
\end{frame}
|
|
735 |
|
|
736 |
\begin{frame}
|
|
737 |
\frametitle{Problem set 1.2}
|
|
738 |
\begin{enumerate}
|
|
739 |
|
|
740 |
\item Plot the Koch snowflake. Write a function to generate the
|
|
741 |
necessary points given the two points constituting a line.
|
|
742 |
\pause
|
|
743 |
\begin{enumerate}
|
|
744 |
\item Split the line into 4 segments.
|
|
745 |
\item The first and last segments are trivial.
|
|
746 |
\item To rotate the point you can use complex numbers,
|
|
747 |
recall that $z e^{j \theta}$ rotates a point $z$ in 2D
|
|
748 |
by $\theta$.
|
|
749 |
\item Do this for all line segments till everything is
|
|
750 |
done.
|
|
751 |
\end{enumerate}
|
|
752 |
\item Show rate of convergence for a first and second order finite
|
|
753 |
difference of sin(x)
|
|
754 |
\end{enumerate}
|
|
755 |
\inctime{30}
|
|
756 |
\end{frame}
|
|
757 |
\end{document}
|