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