-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec17.tex
executable file
·435 lines (324 loc) · 9.72 KB
/
lec17.tex
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
\documentclass{beamer}
\usepackage{listings}
\usepackage{color}
\usepackage{hyperref}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{upquote}
% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{10} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{10} % for normal
% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{shadecolor}{rgb}{1, 0.9, 0.3}
\usepackage{listings}
% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\ttm,
otherkeywords={self}, % Add keywords here
keywordstyle=\ttb\color{deepblue},
emph={MyClass,__init__}, % Custom highlighting
emphstyle=\ttb\color{deepred}, % Custom highlighting style
stringstyle=\color{deepgreen},
frame=tb, % Any extra options here
showstringspaces=false, %
upquote=True,
columns=fullflexible,
basicstyle=\ttfamily
}}
% Python environment
\lstnewenvironment{code}[1][]
{
%\begin{small}
\pythonstyle
\lstset{#1}
%\end{small}
}
{}
\begin{document}
\begin{frame}
\frametitle{CS24420 \& MA25220 \& MT25220 \& MX35220 \& CSM0120}
\begin{center}
\begin{huge}
Lecture 17: More Plotting
\end{huge}
\bigskip
Amanda Clare ([email protected])
\end{center}
\end{frame}
\begin{frame}[fragile]
\frametitle{Until the end of term}
\begin{itemize}
\item \textbf{Tues 6th:} Plotting, and prac with worksheet 8. Sign off worksheet 6 deadline.
\item \textbf{Fri 9th:} Mathematical algorithms hack.
\item \textbf{Tues 13th:} Revision class. Prac, and sign off worksheet 7 deadline.
\item \textbf{Fri 16th:} Optional extra practical. Sign off worksheet 8 deadline.
\end{itemize}
January: Extra material on Databases and Python (using Sqlite).
\end{frame}
\begin{frame}[fragile]
\frametitle{Writing data to files}
\begin{code}
with open("foo.txt", "w") as output_file:
output_file.write("whatever we want to write\n")
for i in range(100):
output_file.write(i)
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Error bars}
\includegraphics[width=10cm]{errorbars.png}
\end{frame}
\begin{frame}[fragile]
\frametitle{Error bars}
We can plot error bars for each y value:
\begin{code}
x = [1, 2, 3, 4, 5]
y = [10, 12, 15, 22, 28]
errors = [2.2, 1.5, 1.8, 2.8, 1.4]
plt.errorbar(x, y, yerr=errors)
plt.axis([0, 6, 0, 40])
plt.ylabel('the y axis name')
plt.xlabel('some x axis')
plt.show()
\end{code}
Could also plot xerr (error in the x position).
\end{frame}
\begin{frame}[fragile]
\frametitle{Error bars by themselves}
\includegraphics[width=10cm]{justerrors.png}
\end{frame}
\begin{frame}[fragile]
\frametitle{Error bars by themselves}
\begin{code}
x = [1, 2, 3, 4, 5]
y = [10, 12, 15, 22, 28]
errors = [2.2, 1.5, 1.8, 2.8, 1.4]
plt.errorbar(x, y, yerr=errors, fmt="none")
plt.axis([0, 6, 0, 40])
plt.ylabel('the y axis name')
plt.xlabel('some x axis')
plt.show()
\end{code}
In Python 2 this would have been \texttt{fmt=None}.
\end{frame}
\begin{frame}[fragile]
\frametitle{Error bars in colour}
\includegraphics[width=10cm]{errorscolour.png}
\end{frame}
\begin{frame}[fragile]
\frametitle{Error bars in colo\textcolor{red}{u}r}
Error bars in a different colo\textcolor{red}{u}r.
\begin{code}
x = [1, 2, 3, 4, 5]
y = [10, 12, 15, 22, 28]
errors = [2.2, 1.5, 1.8, 2.8, 1.4]
plt.plot(x, y, 'b-')
plt.plot(x, y, 'bo')
plt.errorbar(x, y, yerr=errors, fmt="none", ecolor="r")
plt.axis([0, 6, 0, 40])
plt.ylabel('the y axis name')
plt.xlabel('some x axis')
plt.show()
\end{code}
\end{frame}
% datetime plots
\begin{frame}[fragile]
\frametitle{Plotting datetime values}
Lets say we have a list of datetime objects. We can plot this against
a list of y values:
\begin{code}
plt.plot(dts, y, 'r-')
# Will tilt the date labels on the x axis
plt.gcf().autofmt_xdate()
\end{code}
\bigskip
\bigskip
\includegraphics[width=7cm]{datetime.png}
\end{frame}
\begin{frame}[fragile]
\frametitle{Plotting tides}
Downloaded tide heights for the past month for Castletownbere on the south west coast of Ireland from \url{http://data.marine.ie/Dataset/Details/20932} as a CSV file:
\bigskip
\begin{footnotesize}
\begin{verbatim}
station_id,time,Water_Level,Water_Level_LAT,Water_Level_OD_Malin,QC_Flag,longitude,latitude,altitude
,UTC,m,m,m,,degrees_east,degrees_north,m
Castletownbere,2016-11-06T00:00:00Z,1.968,1.968,-0.52,0.0,-9.9034,51.6496,0.0
Castletownbere,2016-11-06T00:15:00Z,1.888,1.888,-0.6,0.0,-9.9034,51.6496,0.0
Castletownbere,2016-11-06T00:30:00Z,1.808,1.808,-0.68,0.0,-9.9034,51.6496,0.0
Castletownbere,2016-11-06T00:45:00Z,1.758,1.758,-0.73,0.0,-9.9034,51.6496,0.0
\end{verbatim}
\end{footnotesize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Plotting tides}
Code here for your notes but also provided as tides.py.
\begin{code}
import matplotlib.pyplot as plt
import csv
import datetime as dt
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Plotting tides}
\begin{code}
def get_data_from_file(filename, dates, heights):
format = "%Y-%m-%dT%H:%M:00Z"
with open(filename,'r') as f:
csvreader = csv.reader(f)
next(csvreader) # throw away header line
next(csvreader) # throw away second header line
for row in csvreader:
thedatetime = row[1]
height = float(row[2])
d_o = dt.datetime.strptime(thedatetime, format)
dates.append(d_o)
heights.append(height)
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Plotting tides}
\begin{code}
def plot_tides(dates, heights):
plt.plot(dates, heights, 'c-')
plt.ylabel("Height in m")
plt.gcf().autofmt_xdate()
plt.savefig("tides.png")
if __name__ == "__main__":
dates = []
heights = []
get_data_from_file(
"IrishNationalTideGaugeNetwork_6b1b_95a2_06e9.csv",
dates, heights)
plot_tides(dates, heights)
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Plotting tides}
\includegraphics[width=12cm]{tides.png}
\end{frame}
%plotting loglog
\begin{frame}[fragile]
\frametitle{Plotting a relationship}
Linear relationship
\[ y = mx + c \]
Exponential relationship
\[ y = Ax^n \]
For example:
\[y = 5x +6\]
\[y = 5x ^3 \]
We can generate data from these equations and plot them.
\end{frame}
\begin{frame}[fragile]
\frametitle{Functions to generate data}
\begin{code}
def gen_linear(m, c):
"""Generates data for y = mx + c"""
x = np.linspace(0,10)
y = m * x + c
return (x, y)
def gen_exponential(a, n):
"""Generates data for y = ax^n"""
x = np.linspace(0,10)
y = a * x ** n
return (x, y)
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Plot it!}
\begin{code}
if __name__ == "__main__":
(x, y) = gen_linear(5, 3)
plt.plot(x, y, 'b-')
plt.plot(x, y, 'bv')
(x, y) = gen_exponential(5, 3)
plt.plot(x, y, 'r-')
plt.plot(x, y, 'rv')
plt.savefig("linexp.png")
plt.show()
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Plot it!}
\includegraphics[width=12cm]{linexp.png}
\end{frame}
\begin{frame}[fragile]
\frametitle{Exponential relationship}
If we have an exponential relationship between variables x and y then when we take logs of both sides we will get a linear relationship.
\[ y = Ax^n \]
\[log(y) = log(Ax^n) \]
\[ log(y) = log(A) + log(x^n) \]
\[ log(y) = log(A) + n log(x) \]
So plotting this data on log-transformed axes should show a straight line.
\end{frame}
\begin{frame}[fragile]
\frametitle{Plot exponential relationships on a loglog scale}
\begin{code}
def gen_exponential(a, n):
"""Generates data for y = ax^n"""
x = np.linspace(0,10)
y = a * x ** n
return (x, y)
if __name__ == "__main__":
(x, y) = gen_exponential(5, 3)
plt.loglog(x, y, 'r-')
plt.loglog(x, y, 'rv')
plt.savefig("loglog.png")
plt.show()
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Plots with logarithmic axes}
\includegraphics[width=10cm]{loglog.png}
\end{frame}
\begin{frame}[fragile]
\frametitle{Richter scale}
The Richter scale for measuring earthquakes is a logarithmic scale.
\bigskip
Using data from the Wikipedia description of the comparison between the Richter scale and the equivalent mass of TNT that would be needed for some events:
\url{https://en.wikipedia.org/wiki/Richter_magnitude_scale}
\bigskip
\[ log( \textrm{amount of TNT}) \propto \textrm{Richter magnitude}\]
\end{frame}
\begin{frame}[fragile]
\frametitle{Plot with semi-logarithmic axes}
\includegraphics[width=11cm]{richter.png}
\end{frame}
\begin{frame}[fragile]
\frametitle{xkcd}
\includegraphics[width=8cm]{log_scale_xkcd.png}
\bigskip
Also see \url{https://xkcd.com/482/}: Universe on a log scale.
\end{frame}
\begin{frame}[fragile]
\frametitle{When using graphs in a report:}
\begin{itemize}
\item Label both axes of every graph (description and units).
\item Caption the graph in full (not just 3 words). Do this even if you
feel you are repeating what you said in the body of the report. ``Figure 1. A graph of tide heights over the course of a month beginning 8th November 2016 for Castletownbere, Ireland. Data was obtained from http://data.marine.ie/. Note that the variation in height of the high and low tides over the month is approximately 1 m.''
\item If presenting a graph on screen, explain what the graph
shows.
``This is a graph of population size in London through the
Victorian era in Britain.
The x-axis shows the years from 1840 to 1900.
The y-axis shows population in hundreds.
I'm showing this graph to point out how population in major
cities rose sharply during the industrial revolution and
especially so after 1860 because of ...''
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Summary}
More plotting:
\begin{itemize}
\item Error bars
\item Dates
\item Log scale graphs
\end{itemize}
\end{frame}
\end{document}