forked from IPGP/scientific_python_cheat_sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheet.html
330 lines (318 loc) · 31.1 KB
/
sheet.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<title></title>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>
</head>
<body>
<h1 id="scientific-python-cheatsheet">Scientific Python Cheatsheet</h1>
<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-generate-toc again -->
<p><strong>Table of Contents</strong></p>
<ul>
<li><a href="#scientific-python-cheatsheet">Scientific Python Cheatsheet</a>
<ul>
<li><a href="#pure-python">Pure Python</a>
<ul>
<li><a href="#types">Types</a></li>
<li><a href="#lists">Lists</a></li>
<li><a href="#dictionaries">Dictionaries</a></li>
<li><a href="#strings">Strings</a></li>
<li><a href="#operators">Operators</a></li>
<li><a href="#control-flow">Control Flow</a></li>
<li><a href="#functions-classes-generators-decorators">Functions, Classes, Generators, Decorators</a></li>
</ul></li>
<li><a href="#numpy">NumPy</a>
<ul>
<li><a href="#array-initialization">array initialization</a></li>
<li><a href="#reading-writing-files">reading/ writing files</a></li>
<li><a href="#array-properties-and-operations">array properties and operations</a></li>
<li><a href="#indexing">indexing</a></li>
<li><a href="#boolean-arrays">boolean arrays</a></li>
<li><a href="#elementwise-operations-and-math-functions">elementwise operations and math functions</a></li>
<li><a href="#inner--outer-products">inner / outer products</a></li>
<li><a href="#interpolation-integration">interpolation, integration</a></li>
<li><a href="#fft">fft</a></li>
<li><a href="#rounding">rounding</a></li>
<li><a href="#random-variables">random variables</a></li>
</ul></li>
<li><a href="#matplotlib">Matplotlib</a>
<ul>
<li><a href="#figures-and-axes">figures and axes</a></li>
<li><a href="#figures-and-axes-properties">figures and axes properties</a></li>
<li><a href="#plotting-routines">plotting routines</a></li>
</ul></li>
</ul></li>
</ul>
<!-- markdown-toc end -->
<h2 id="pure-python">Pure Python</h2>
<h3 id="types">Types</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="dv">2</span> <span class="co"># integer</span>
b <span class="op">=</span> <span class="fl">5.0</span> <span class="co"># float</span>
c <span class="op">=</span> <span class="fl">8.3e5</span> <span class="co"># exponential</span>
d <span class="op">=</span> <span class="fl">1.5</span> <span class="op">+</span><span class="ot"> 0.5j</span> <span class="co"># complex</span>
e <span class="op">=</span> <span class="dv">4</span> <span class="op">></span> <span class="dv">5</span> <span class="co"># boolean</span>
f <span class="op">=</span> <span class="st">'word'</span> <span class="co"># string</span></code></pre></div>
<h3 id="lists">Lists</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> [<span class="st">'red'</span>, <span class="st">'blue'</span>, <span class="st">'green'</span>] <span class="co"># manually initialization</span>
b <span class="op">=</span> <span class="bu">range</span>(<span class="dv">5</span>) <span class="co"># initialization through a function</span>
c <span class="op">=</span> [nu<span class="op">**</span><span class="dv">2</span> <span class="cf">for</span> nu <span class="op">in</span> b] <span class="co"># initialize through list comprehension</span>
d <span class="op">=</span> [nu<span class="op">**</span><span class="dv">2</span> <span class="cf">for</span> nu <span class="op">in</span> b <span class="cf">if</span> b <span class="op"><</span> <span class="dv">3</span>] <span class="co"># list comprehension withcondition</span>
e <span class="op">=</span> c[<span class="dv">0</span>] <span class="co"># access element</span>
f <span class="op">=</span> e[<span class="dv">1</span>: <span class="dv">2</span>] <span class="co"># access a slice of the list</span>
g <span class="op">=</span> [<span class="st">'re'</span>, <span class="st">'bl'</span>] <span class="op">+</span> [<span class="st">'gr'</span>] <span class="co"># list concatenation</span>
h <span class="op">=</span> [<span class="st">'re'</span>] <span class="op">*</span> <span class="dv">5</span> <span class="co"># repeat a list</span>
[<span class="st">'re'</span>, <span class="st">'bl'</span>].index(<span class="st">'re'</span>) <span class="co"># returns index of 're'</span>
<span class="co">'re'</span> <span class="op">in</span> [<span class="st">'re'</span>, <span class="st">'bl'</span>] <span class="co"># true if 're' in list</span>
<span class="bu">sorted</span>([<span class="dv">3</span>, <span class="dv">2</span>, <span class="dv">1</span>]) <span class="co"># returns sorted list</span>
z <span class="op">=</span> [<span class="st">'red'</span>] <span class="op">+</span> [<span class="st">'green'</span>, <span class="st">'blue'</span>] <span class="co"># list concatenation</span></code></pre></div>
<h3 id="dictionaries">Dictionaries</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> {<span class="st">'red'</span>: <span class="st">'rouge'</span>, <span class="st">'blue'</span>: <span class="st">'bleu'</span>, <span class="st">'green'</span>: <span class="st">'vert'</span>} <span class="co"># dictionary</span>
b <span class="op">=</span> a[<span class="st">'red'</span>] <span class="co"># translate item</span>
c <span class="op">=</span> [value <span class="cf">for</span> key, value <span class="op">in</span> b.items()] <span class="co"># loop through contents</span>
d <span class="op">=</span> a.get(<span class="st">'yellow'</span>, <span class="st">'no translation found'</span>) <span class="co"># return default</span></code></pre></div>
<h3 id="strings">Strings</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="st">'red'</span> <span class="co"># assignment</span>
char <span class="op">=</span> a[<span class="dv">2</span>] <span class="co"># access individual characters</span>
<span class="co">'red '</span> <span class="op">+</span> <span class="st">'blue'</span> <span class="co"># string concatenation</span>
<span class="co">'1, 2, three'</span>.split(<span class="st">','</span>) <span class="co"># split string into list</span>
<span class="co">'.'</span>.join([<span class="st">'1'</span>, <span class="st">'2'</span>, <span class="st">'three'</span>]) <span class="co"># concatenate list into string</span></code></pre></div>
<h3 id="operators">Operators</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="dv">2</span> <span class="co"># assignment</span>
a <span class="op">+=</span> <span class="dv">1</span> (<span class="op">*=</span>, <span class="op">/=</span>) <span class="co"># change and assign</span>
<span class="dv">3</span> <span class="op">+</span> <span class="dv">2</span> <span class="co"># addition</span>
<span class="dv">3</span> <span class="op">/</span> <span class="dv">2</span> <span class="co"># integer division (python2) or float division (python3)</span>
<span class="dv">3</span> <span class="op">//</span> <span class="dv">2</span> <span class="co"># integer division</span>
<span class="dv">3</span> <span class="op">*</span> <span class="dv">2</span> <span class="co"># multiplication</span>
<span class="dv">3</span> <span class="op">**</span> <span class="dv">2</span> <span class="co"># exponent</span>
<span class="dv">3</span> <span class="op">%</span> <span class="dv">2</span> <span class="co"># remainder</span>
<span class="bu">abs</span>() <span class="co"># absolute value</span>
<span class="dv">1</span> <span class="op">==</span> <span class="dv">1</span> <span class="co"># equal</span>
<span class="dv">2</span> <span class="op">></span> <span class="dv">1</span> <span class="co"># larger</span>
<span class="dv">2</span> <span class="op"><</span> <span class="dv">1</span> <span class="co"># smaller</span>
<span class="dv">1</span> <span class="op">!=</span> <span class="dv">2</span> <span class="co"># not equal</span>
<span class="dv">1</span> <span class="op">!=</span> <span class="dv">2</span> <span class="op">and</span> <span class="dv">2</span> <span class="op"><</span> <span class="dv">3</span> <span class="co"># logical AND</span>
<span class="dv">1</span> <span class="op">!=</span> <span class="dv">2</span> <span class="op">or</span> <span class="dv">2</span> <span class="op"><</span> <span class="dv">3</span> <span class="co"># logical OR</span>
<span class="op">not</span> <span class="dv">1</span> <span class="op">==</span> <span class="dv">2</span> <span class="co"># logical NOT</span>
a <span class="op">in</span> b <span class="co"># test if a is in b</span>
a <span class="op">is</span> b <span class="co"># test if objects point to the same memory (id)</span></code></pre></div>
<h3 id="control-flow">Control Flow</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># if/elif/else</span>
a, b <span class="op">=</span> <span class="dv">1</span>, <span class="dv">2</span>
<span class="cf">if</span> a <span class="op">+</span> b <span class="op">==</span> <span class="dv">3</span>:
<span class="bu">print</span> <span class="st">'True'</span>
<span class="cf">elif</span> a <span class="op">+</span> b <span class="op">==</span> <span class="dv">1</span>:
<span class="bu">print</span> <span class="st">'False'</span>
<span class="cf">else</span>:
<span class="bu">print</span> <span class="st">'?'</span>
<span class="co"># for</span>
a <span class="op">=</span> [<span class="st">'red'</span>, <span class="st">'blue'</span>, <span class="st">'green'</span>]
<span class="cf">for</span> color <span class="op">in</span> a:
<span class="bu">print</span> color
<span class="co"># while</span>
number <span class="op">=</span> <span class="dv">1</span>
<span class="cf">while</span> number <span class="op"><</span> <span class="dv">10</span>:
<span class="bu">print</span> number
number <span class="op">+=</span> <span class="dv">1</span>
<span class="co"># break</span>
number <span class="op">=</span> <span class="dv">1</span>
<span class="cf">while</span> <span class="va">True</span>:
<span class="bu">print</span> number
number <span class="op">+=</span> <span class="dv">1</span>
<span class="cf">if</span> number <span class="op">></span> <span class="dv">10</span>:
<span class="cf">break</span>
<span class="co"># continue</span>
<span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">20</span>):
<span class="cf">if</span> i <span class="op">%</span> <span class="dv">2</span> <span class="op">==</span> <span class="dv">0</span>:
<span class="cf">continue</span>
<span class="bu">print</span> i</code></pre></div>
<h3 id="functions-classes-generators-decorators">Functions, Classes, Generators, Decorators</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># Function</span>
<span class="kw">def</span> myfunc(a1, a2):
<span class="cf">return</span> x
x <span class="op">=</span> my_function(a1,a2)
<span class="co"># Class</span>
<span class="kw">class</span> Point(<span class="bu">object</span>):
<span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, x):
<span class="va">self</span>.x <span class="op">=</span> x
<span class="kw">def</span> <span class="fu">__call__</span>(<span class="va">self</span>):
<span class="bu">print</span> <span class="va">self</span>.x
x <span class="op">=</span> Point(<span class="dv">3</span>)
<span class="co"># Generators</span>
<span class="kw">def</span> firstn(n):
num <span class="op">=</span> <span class="dv">0</span>
<span class="cf">while</span> num <span class="op"><</span> n:
<span class="cf">yield</span> num
num <span class="op">+=</span> <span class="dv">1</span>
x <span class="op">=</span> [<span class="cf">for</span> i <span class="op">in</span> firstn(<span class="dv">10</span>)]
<span class="co"># Decorators</span>
<span class="kw">class</span> myDecorator(<span class="bu">object</span>):
<span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, f):
<span class="va">self</span>.f <span class="op">=</span> f
<span class="kw">def</span> <span class="fu">__call__</span>(<span class="va">self</span>):
<span class="bu">print</span> <span class="st">"call"</span>
<span class="va">self</span>.f()
<span class="at">@myDecorator</span>
<span class="kw">def</span> my_funct():
<span class="bu">print</span> <span class="st">'func'</span>
my_func()</code></pre></div>
<h2 id="numpy">NumPy</h2>
<h3 id="array-initialization">array initialization</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">np.array([<span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>]) <span class="co"># direct initialization</span>
np.empty(<span class="dv">20</span>, dtype<span class="op">=</span>np.float32) <span class="co"># single precision array with 20 entries</span>
np.zeros(<span class="dv">200</span>) <span class="co"># initialize 200 zeros</span>
np.ones((<span class="dv">3</span>,<span class="dv">3</span>), dtype<span class="op">=</span>np.int32) <span class="co"># 3 x 3 integer matrix with ones</span>
np.eye(<span class="dv">200</span>) <span class="co"># ones on the diagonal</span>
np.zeros_like(a) <span class="co"># returns array with zeros and the shape of a</span>
np.linspace(<span class="dv">0</span>., <span class="dv">10</span>., <span class="dv">100</span>) <span class="co"># 100 points from 0 to 10</span>
np.arange(<span class="dv">0</span>, <span class="dv">100</span>, <span class="dv">2</span>) <span class="co"># points from 0 to <100 with step width 2</span>
np.logspace(<span class="op">-</span><span class="dv">5</span>, <span class="dv">2</span>, <span class="dv">100</span>) <span class="co"># 100 log-spaced points between 1e-5 and 1e2</span>
np.copy(a) <span class="co"># copy array to new memory</span></code></pre></div>
<h3 id="reading-writing-files">reading/ writing files</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">np.fromfile(fname<span class="op">/</span><span class="bu">object</span>, dtype<span class="op">=</span>np.float32, count<span class="op">=</span><span class="dv">5</span>) <span class="co"># read binary data from file</span>
np.loadtxt(fname<span class="op">/</span><span class="bu">object</span>, skiprows<span class="op">=</span><span class="dv">2</span>, delimiter<span class="op">=</span><span class="st">','</span>) <span class="co"># read ascii data from file</span></code></pre></div>
<h3 id="array-properties-and-operations">array properties and operations</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a.shape <span class="co"># a tuple with the lengths of each axis</span>
<span class="bu">len</span>(a) <span class="co"># length of axis 0</span>
a.ndim <span class="co"># number of dimensions (axes)</span>
a.sort(axis<span class="op">=</span><span class="dv">1</span>) <span class="co"># sort array along axis</span>
a.flatten() <span class="co"># collapse array to one dimension</span>
a.conj() <span class="co"># return complex conjugate</span>
a.astype(np.int16) <span class="co"># cast to integer</span>
np.argmax(a, axis<span class="op">=</span><span class="dv">2</span>) <span class="co"># return index of maximum along a given axis</span>
np.cumsum(a) <span class="co"># return cumulative sum</span>
np.<span class="bu">any</span>(a) <span class="co"># True if any element is True</span>
np.<span class="bu">all</span>(a) <span class="co"># True if all elements are True</span>
np.argsort(a, axis<span class="op">=</span><span class="dv">1</span>) <span class="co"># return sorted index array along axis</span></code></pre></div>
<h3 id="indexing">indexing</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> np.arange(<span class="dv">100</span>) <span class="co"># initialization with 0 - 99</span>
a[: <span class="dv">3</span>] <span class="op">=</span> <span class="dv">0</span> <span class="co"># set the first three indices to zero</span>
a[<span class="dv">1</span>: <span class="dv">5</span>] <span class="op">=</span> <span class="dv">1</span> <span class="co"># set indices 1-4 to 1</span>
a[start:stop:step] <span class="co"># general form of indexing/slicing</span>
a[<span class="va">None</span>, :] <span class="co"># transform to column vector</span>
a[[<span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">8</span>]] <span class="co"># return array with values of the indices</span>
a <span class="op">=</span> a.reshape(<span class="dv">10</span>, <span class="dv">10</span>) <span class="co"># transform to 10 x 10 matrix</span>
a.T <span class="co"># return transposed view</span>
np.transpose(a, (<span class="dv">2</span>, <span class="dv">1</span>, <span class="dv">0</span>)) <span class="co"># transpose array to new axis order</span>
a[a <span class="op"><</span> <span class="dv">2</span>] <span class="co"># returns array that fulfills elementwise condition</span></code></pre></div>
<h3 id="boolean-arrays">boolean arrays</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op"><</span> <span class="dv">2</span> <span class="co"># returns array with boolean values</span>
np.logical_and(a <span class="op"><</span> <span class="dv">2</span>, b <span class="op">></span> <span class="dv">10</span>) <span class="co"># elementwise logical and</span>
np.logical_or(a <span class="op"><</span> <span class="dv">2</span>, b <span class="op">></span> <span class="dv">10</span>) <span class="co"># elementwise logical or</span>
<span class="op">~</span>a <span class="co"># invert boolean array</span>
np.invert(a) <span class="co"># invert boolean array</span></code></pre></div>
<h3 id="elementwise-operations-and-math-functions">elementwise operations and math functions</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">*</span> <span class="dv">5</span> <span class="co"># multiplication with scalar</span>
a <span class="op">+</span> <span class="dv">5</span> <span class="co"># addition with scalar</span>
a <span class="op">+</span> b <span class="co"># addition with array b</span>
a <span class="op">/</span> b <span class="co"># division with b (np.NaN for division by zero)</span>
np.exp(a) <span class="co"># exponential (complex and real)</span>
np.sin(a) <span class="co"># sine</span>
np.cos(a) <span class="co"># cosine</span>
np.arctan2(y,x) <span class="co"># arctan(y/x)</span>
np.arcsin(x) <span class="co"># arcsin</span>
np.radians(a) <span class="co"># degrees to radians</span>
np.degrees(a) <span class="co"># radians to degrees</span>
np.var(a) <span class="co"># variance of array</span>
np.std(a, axis<span class="op">=</span><span class="dv">1</span>) <span class="co"># standard deviation</span></code></pre></div>
<h3 id="inner-outer-products">inner / outer products</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">np.dot(a, b) <span class="co"># inner matrix product: a_mi b_in</span>
np.einsum(<span class="st">'ijkl,klmn->ijmn'</span>, a, b) <span class="co"># einstein summation convention</span>
np.<span class="bu">sum</span>(a, axis<span class="op">=</span><span class="dv">1</span>) <span class="co"># sum over axis 1</span>
np.<span class="bu">abs</span>(a) <span class="co"># return array with absolute values</span>
a[<span class="va">None</span>, :] <span class="op">+</span> b[:, <span class="va">None</span>] <span class="co"># outer sum</span>
a[<span class="va">None</span>, :] <span class="op">*</span> b[<span class="va">None</span>, :] <span class="co"># outer product</span>
np.outer(a, b) <span class="co"># outer product</span>
np.<span class="bu">sum</span>(a <span class="op">*</span> a.T) <span class="co"># matrix norm</span></code></pre></div>
<h3 id="interpolation-integration">interpolation, integration</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">np.trapz(y, x<span class="op">=</span>x, axis<span class="op">=</span><span class="dv">1</span>) <span class="co"># integrate along axis 1</span>
np.interp(x, xp, yp) <span class="co"># interpolate function xp, yp at points x</span></code></pre></div>
<h3 id="fft">fft</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">np.fft.fft(y) <span class="co"># complex fourier transform of y</span>
np.fft.fftfreqs(<span class="bu">len</span>(y)) <span class="co"># fft frequencies for a given length</span>
np.fft.fftshift(freqs) <span class="co"># shifts zero frequency to the middle</span>
np.fft.rfft(y) <span class="co"># real fourier transform of y</span>
np.fft.rfftfreqs(<span class="bu">len</span>(y)) <span class="co"># real fft frequencies for a given length</span></code></pre></div>
<h3 id="rounding">rounding</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">np.ceil(a) <span class="co"># rounds to nearest upper int</span>
np.floor(a) <span class="co"># rounds to nearest lower int</span>
np.<span class="bu">round</span>(a) <span class="co"># rounds to neares int</span></code></pre></div>
<h3 id="random-variables">random variables</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">np.random.normal(loc<span class="op">=</span><span class="dv">0</span>, scale<span class="op">=</span><span class="dv">2</span>, size<span class="op">=</span><span class="dv">100</span>) <span class="co"># 100 normal distributed random numbers</span>
np.random.seed(<span class="dv">23032</span>) <span class="co"># resets the seed value</span>
np.random.rand(<span class="dv">200</span>) <span class="co"># 200 random numbers in [0, 1)</span>
np.random.uniform(<span class="dv">1</span>, <span class="dv">30</span>, <span class="dv">200</span>) <span class="co"># 200 random numbers in [1, 30)</span>
np.random.random_integers(<span class="dv">1</span>, <span class="dv">15</span>, <span class="dv">300</span>) <span class="co"># 300 random integers between [1, 15]</span></code></pre></div>
<h2 id="matplotlib">Matplotlib</h2>
<h3 id="figures-and-axes">figures and axes</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">fig <span class="op">=</span> plt.figure(figsize<span class="op">=</span>(<span class="dv">5</span>, <span class="dv">2</span>), facecolor<span class="op">=</span><span class="st">'black'</span>) <span class="co"># initialize figure</span>
ax <span class="op">=</span> fig.add_subplot(<span class="dv">3</span>, <span class="dv">2</span>, <span class="dv">2</span>) <span class="co"># add second subplot in a 3 x 2 grid</span>
fig, axes <span class="op">=</span> plt.subplots(<span class="dv">5</span>, <span class="dv">2</span>, figsize<span class="op">=</span>(<span class="dv">5</span>, <span class="dv">5</span>)) <span class="co"># return fig and array of axes in a 5 x 2 grid</span>
ax <span class="op">=</span> fig.add_axes([left, bottom, width, height]) <span class="co"># manually add axes at a certain position</span></code></pre></div>
<h3 id="figures-and-axes-properties">figures and axes properties</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">fig.suptitle(<span class="st">'title'</span>) <span class="co"># big figure title</span>
fig.subplots_adjust(bottom<span class="op">=</span><span class="fl">0.1</span>, right<span class="op">=</span><span class="fl">0.8</span>, top<span class="op">=</span><span class="fl">0.9</span>, wspace<span class="op">=</span><span class="fl">0.2</span>,
hspace<span class="op">=</span><span class="fl">0.5</span>) <span class="co"># adjust subplot positions</span>
fig.tight_layout(pad<span class="op">=</span><span class="fl">0.1</span>,h_pad<span class="op">=</span><span class="fl">0.5</span>, w_pad<span class="op">=</span><span class="fl">0.5</span>, rect<span class="op">=</span><span class="va">None</span>) <span class="co"># adjust</span>
subplots to fit perfectly into fig
ax.set_xlabel() <span class="co"># set xlabel</span>
ax.set_ylabel() <span class="co"># set ylabel</span>
ax.set_xlim(<span class="dv">1</span>, <span class="dv">2</span>) <span class="co"># sets x limits</span>
ax.set_ylim(<span class="dv">3</span>, <span class="dv">4</span>) <span class="co"># sets y limits</span>
ax.set_title(<span class="st">'blabla'</span>) <span class="co"># sets the axis title</span>
ax.<span class="bu">set</span>(xlabel<span class="op">=</span><span class="st">'bla'</span>) <span class="co"># set multiple parameters at once</span>
ax.legend(loc<span class="op">=</span><span class="st">'upper center'</span>) <span class="co"># activate legend</span>
ax.grid(<span class="va">True</span>, which<span class="op">=</span><span class="st">'both'</span>) <span class="co"># activate grid</span>
bbox <span class="op">=</span> ax.get_position() <span class="co"># returns the axes bounding box</span>
bbox.x0 <span class="op">+</span> bbox.width <span class="co"># bounding box parameters</span></code></pre></div>
<h3 id="plotting-routines">plotting routines</h3>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">ax.plot(x,y, <span class="st">'-o'</span>, c<span class="op">=</span><span class="st">'red'</span>, lw<span class="op">=</span><span class="dv">2</span>, label<span class="op">=</span><span class="st">'bla'</span>) <span class="co"># plots a line</span>
ax.scatter(x,y, s<span class="op">=</span><span class="dv">20</span>, c<span class="op">=</span>color) <span class="co"># scatter plot</span>
ax.pcolormesh(xx,yy,zz, shading<span class="op">=</span><span class="st">'gouraud'</span>) <span class="co"># fast colormesh function</span>
ax.colormesh(xx,yy,zz, norm<span class="op">=</span>norm) <span class="co"># slower colormesh function</span>
ax.contour(xx,yy,zz, cmap<span class="op">=</span><span class="st">'jet'</span>) <span class="co"># contour line plot</span>
ax.contourf(xx,yy,zz, vmin<span class="op">=</span><span class="dv">2</span>, vmax<span class="op">=</span><span class="dv">4</span>) <span class="co"># filled contours plot</span>
n, bins, patch <span class="op">=</span> ax.hist(x, <span class="dv">50</span>) <span class="co"># histogram</span>
ax.imshow(matrix, origin<span class="op">=</span><span class="st">'lower'</span>, extent<span class="op">=</span>(x1, x2, y1, y2)) <span class="co"># show image</span>
ax.specgram(y, FS<span class="op">=</span><span class="fl">0.1</span>, noverlap<span class="op">=</span><span class="dv">128</span>, scale<span class="op">=</span><span class="st">'linear'</span>) <span class="co"># plot a spectrogram</span></code></pre></div>
</body>
</html>