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