Now using py files, for automation
[covid19.git] / uk_deaths.md
1 ---
2 jupyter:
3 jupytext:
4 formats: ipynb,md
5 text_representation:
6 extension: .md
7 format_name: markdown
8 format_version: '1.2'
9 jupytext_version: 1.9.1
10 kernelspec:
11 display_name: Python 3
12 language: python
13 name: python3
14 ---
15
16 <!-- #region Collapsed="false" -->
17 Data from:
18
19 * [Office of National Statistics](https://www.ons.gov.uk/peoplepopulationandcommunity/birthsdeathsandmarriages/deaths/datasets/weeklyprovisionalfiguresondeathsregisteredinenglandandwales) (Endland and Wales) Weeks start on a Saturday.
20 * [Northern Ireland Statistics and Research Agency](https://www.nisra.gov.uk/publications/weekly-deaths) (Northern Ireland). Weeks start on a Saturday. Note that the week numbers don't match the England and Wales data.
21 * [National Records of Scotland](https://www.nrscotland.gov.uk/statistics-and-data/statistics/statistics-by-theme/vital-events/general-publications/weekly-and-monthly-data-on-births-and-deaths/weekly-data-on-births-and-deaths) (Scotland). Note that Scotland uses ISO8601 week numbers, which start on a Monday.
22
23 <!-- #endregion -->
24
25 ```python Collapsed="false"
26 import itertools
27 import collections
28 import json
29 import pandas as pd
30 import numpy as np
31 from scipy.stats import gmean
32
33 import matplotlib as mpl
34 import matplotlib.pyplot as plt
35 %matplotlib inline
36 ```
37
38 ```python Collapsed="false"
39 england_wales_filename = 'uk-deaths-data/publishedweek532020.xlsx'
40 ```
41
42 ```python Collapsed="false"
43 raw_data_2015 = pd.read_csv('uk-deaths-data/Weekly_Deaths_NI_2015.csv',
44 parse_dates=[1, 2], dayfirst=True,
45 index_col=0,
46 header=[0, 1]
47 )
48 dh15i = raw_data_2015.iloc[:, [2]]
49 dh15i.columns = ['total_2015']
50 # dh15i.head()
51 ```
52
53 ```python Collapsed="false"
54 raw_data_2016 = pd.read_csv('uk-deaths-data/Weekly_Deaths_NI_2016.csv',
55 parse_dates=[1, 2], dayfirst=True,
56 index_col=0,
57 header=[0, 1]
58 )
59 dh16i = raw_data_2016.iloc[:, [2]]
60 dh16i.columns = ['total_2016']
61 # dh16i.head()
62 ```
63
64 ```python Collapsed="false"
65 raw_data_2017 = pd.read_csv('uk-deaths-data/Weekly_Deaths_NI_2017.csv',
66 parse_dates=[1, 2], dayfirst=True,
67 index_col=0,
68 header=[0, 1]
69 )
70 dh17i = raw_data_2017.iloc[:, [2]]
71 dh17i.columns = ['total_2017']
72 # dh17i.head()
73 ```
74
75 ```python Collapsed="false"
76 raw_data_2018 = pd.read_csv('uk-deaths-data/Weekly_Deaths_NI_2018.csv',
77 parse_dates=[1, 2], dayfirst=True,
78 index_col=0,
79 header=[0, 1]
80 )
81 dh18i = raw_data_2018.iloc[:, [2]]
82 dh18i.columns = ['total_2018']
83 # dh18i.head()
84 ```
85
86 ```python Collapsed="false"
87 raw_data_2019 = pd.read_csv('uk-deaths-data/Weekly_Deaths_NI_2019.csv',
88 parse_dates=[1, 2], dayfirst=True,
89 index_col=0,
90 header=[0, 1]
91 )
92 dh19i = raw_data_2019.iloc[:, [2]]
93 dh19i.columns = ['total_2019']
94 # dh19i.head()
95 ```
96
97 ```python Collapsed="false"
98 raw_data_2020_i = pd.read_csv('uk-deaths-data/Weekly_Deaths_NI_2020.csv',
99 parse_dates=[1], dayfirst=True,
100 index_col=0,
101 header=[0, 1]
102 )
103 deaths_headlines_i = raw_data_2020_i.iloc[:, [1]]
104 deaths_headlines_i.columns = ['total_2020']
105 deaths_headlines_i.tail()
106 ```
107
108 ```python Collapsed="false"
109 raw_data_2019
110 ```
111
112 ```python Collapsed="false"
113
114 ```
115
116 ```python Collapsed="false"
117 raw_data_s = pd.read_csv('uk-deaths-data/weekly-deaths-scotland.csv',
118 index_col=0,
119 header=0,
120 skiprows=2
121 )
122 # raw_data_s
123 ```
124
125 ```python Collapsed="false"
126 deaths_headlines_s = raw_data_s[reversed('2015 2016 2017 2018 2019 2020'.split())]
127 deaths_headlines_s.columns = ['total_' + c for c in deaths_headlines_s.columns]
128 deaths_headlines_s.reset_index(drop=True, inplace=True)
129 deaths_headlines_s.index = deaths_headlines_s.index + 1
130 deaths_headlines_s
131 ```
132
133 ```python Collapsed="false"
134
135 ```
136
137 ```python Collapsed="false"
138 eng_xls = pd.read_excel(england_wales_filename,
139 sheet_name="Weekly figures 2020",
140 skiprows=[0, 1, 2, 3],
141 header=0,
142 index_col=[1]
143 ).iloc[:91].T
144 eng_xls
145 ```
146
147 ```python Collapsed="false"
148 # eng_xls_columns
149 ```
150
151 ```python Collapsed="false"
152 eng_xls_columns = list(eng_xls.columns)
153
154 for i, c in enumerate(eng_xls_columns):
155 # print(i, c, type(c), isinstance(c, float))
156 if isinstance(c, float) and np.isnan(c):
157 if eng_xls.iloc[0].iloc[i] is not pd.NaT:
158 eng_xls_columns[i] = eng_xls.iloc[0].iloc[i]
159
160 # np.isnan(eng_xls_columns[0])
161 # eng_xls_columns
162
163 eng_xls.columns = eng_xls_columns
164 # eng_xls.columns
165 ```
166
167 ```python Collapsed="false"
168 eng_xls['Total deaths, all ages']
169 ```
170
171 ```python Collapsed="false"
172 eng_xls['Wales'].iloc[1:]
173 ```
174
175 ```python Collapsed="false"
176 # raw_data_2020 = pd.read_csv('uk-deaths-data/publishedweek272020.csv',
177 # parse_dates=[1], dayfirst=True,
178 # index_col=0,
179 # header=[0, 1])
180 ```
181
182 ```python Collapsed="false"
183
184 ```
185
186 ```python Collapsed="false"
187 # raw_data_2020.head()
188 ```
189
190 ```python Collapsed="false"
191 # raw_data_2020['W92000004', 'Wales']
192 ```
193
194 ```python Collapsed="false"
195 raw_data_2019 = pd.read_csv('uk-deaths-data/publishedweek522019.csv',
196 parse_dates=[1], dayfirst=True,
197 index_col=0,
198 header=[0, 1])
199 # raw_data_2019.head()
200 ```
201
202 ```python Collapsed="false"
203 raw_data_2018 = pd.read_csv('uk-deaths-data/publishedweek522018.csv',
204 parse_dates=[1], dayfirst=True,
205 index_col=0,
206 header=[0, 1])
207 # raw_data_2018.head()
208 ```
209
210 ```python Collapsed="false"
211 raw_data_2017 = pd.read_csv('uk-deaths-data/publishedweek522017.csv',
212 parse_dates=[1], dayfirst=True,
213 index_col=0,
214 header=[0, 1])
215 # raw_data_2017.head()
216 ```
217
218 ```python Collapsed="false"
219 raw_data_2016 = pd.read_csv('uk-deaths-data/publishedweek522016.csv',
220 parse_dates=[1], dayfirst=True,
221 index_col=0,
222 header=[0, 1])
223 # raw_data_2016.head()
224 ```
225
226 ```python Collapsed="false"
227 raw_data_2015 = pd.read_csv('uk-deaths-data/publishedweek2015.csv',
228 parse_dates=[1], dayfirst=True,
229 index_col=0,
230 header=[0, 1])
231 # raw_data_2015.head()
232 ```
233
234 ```python Collapsed="false"
235 dhw = eng_xls['Wales'].iloc[1:]
236 dhe = eng_xls['Total deaths, all ages'].iloc[1:] - dhw
237 deaths_headlines_e = pd.DataFrame({'total_2020': dhe.dropna()})
238 deaths_headlines_w = pd.DataFrame({'total_2020': dhw.dropna()})
239 ```
240
241 ```python Collapsed="false"
242 # deaths_headlines_e = raw_data_2020.iloc[:, [1]].copy()
243 # deaths_headlines_e.columns = ['total_2020']
244 # deaths_headlines_w = raw_data_2020['W92000004'].copy()
245 # deaths_headlines_e.columns = ['total_2020']
246 # deaths_headlines_w.columns = ['total_2020']
247 # deaths_headlines_e.total_2020 -= deaths_headlines_w.total_2020
248 # deaths_headlines_e.head()
249 # deaths_headlines_e
250 ```
251
252 ```python Collapsed="false"
253 dh19e = raw_data_2019.iloc[:, [1]]
254 dh19w = raw_data_2019['W92000004']
255 dh19e.columns = ['total_2019']
256 dh19w.columns = ['total_2019']
257 dh19e.total_2019 -= dh19w.total_2019
258 dh19e.tail()
259 ```
260
261 ```python Collapsed="false"
262 dh19w.head()
263 ```
264
265 ```python Collapsed="false"
266 dh18e = raw_data_2018.iloc[:, [1]]
267 dh18w = raw_data_2018['W92000004']
268 dh18e.columns = ['total_2018']
269 dh18w.columns = ['total_2018']
270 dh18e.total_2018 -= dh18w.total_2018
271 # dh18e.head()
272 ```
273
274 ```python Collapsed="false"
275 dh17e = raw_data_2017.iloc[:, [1]]
276 dh17w = raw_data_2017['W92000004']
277 dh17e.columns = ['total_2017']
278 dh17w.columns = ['total_2017']
279 dh17e.total_2017 -= dh17w.total_2017
280 # dh17e.head()
281 ```
282
283 ```python Collapsed="false"
284 dh16e = raw_data_2016.iloc[:, [1]]
285 dh16w = raw_data_2016['W92000004']
286 dh16e.columns = ['total_2016']
287 dh16w.columns = ['total_2016']
288 dh16e.total_2016 -= dh16w.total_2016
289 # dh16e.head()
290 ```
291
292 ```python Collapsed="false"
293 dh15e = raw_data_2015.iloc[:, [1]]
294 dh15w = raw_data_2015['W92000004']
295 dh15e.columns = ['total_2015']
296 dh15w.columns = ['total_2015']
297 dh15e.total_2015 -= dh15w.total_2015
298 # dh15e.head()
299 ```
300
301 ```python Collapsed="false"
302 # dh18 = raw_data_2018.iloc[:, [1, 2]]
303 # dh18.columns = ['total_2018', 'total_previous']
304 # # dh18.head()
305 ```
306
307 ```python Collapsed="false"
308 deaths_headlines_e = deaths_headlines_e.merge(dh19e['total_2019'], how='outer', left_index=True, right_index=True)
309 deaths_headlines_e = deaths_headlines_e.merge(dh18e['total_2018'], how='outer', left_index=True, right_index=True)
310 deaths_headlines_e = deaths_headlines_e.merge(dh17e['total_2017'], how='outer', left_index=True, right_index=True)
311 deaths_headlines_e = deaths_headlines_e.merge(dh16e['total_2016'], how='outer', left_index=True, right_index=True)
312 # deaths_headlines = deaths_headlines.merge(dh15['total_2015'], how='outer', left_index=True, right_index=True)
313 deaths_headlines_e = deaths_headlines_e.merge(dh15e['total_2015'], how='left', left_index=True, right_index=True)
314 deaths_headlines_e
315 ```
316
317 ```python Collapsed="false"
318 deaths_headlines_s = raw_data_s[reversed('2015 2016 2017 2018 2019 2020'.split())]
319 deaths_headlines_s.columns = ['total_' + c for c in deaths_headlines_s.columns]
320 deaths_headlines_s.reset_index(drop=True, inplace=True)
321 deaths_headlines_s.index = deaths_headlines_s.index + 1
322 deaths_headlines_s = deaths_headlines_s.loc[1:52]
323 deaths_headlines_s
324 ```
325
326 <!-- #region Collapsed="false" -->
327 # Correction for missing data
328 <!-- #endregion -->
329
330 ```python Collapsed="false"
331 # deaths_headlines_s.loc[20, 'total_2020'] = 1000
332 # deaths_headlines_s
333 ```
334
335 ```python Collapsed="false"
336 deaths_headlines_w = deaths_headlines_w.merge(dh19w['total_2019'], how='outer', left_index=True, right_index=True)
337 deaths_headlines_w = deaths_headlines_w.merge(dh18w['total_2018'], how='outer', left_index=True, right_index=True)
338 deaths_headlines_w = deaths_headlines_w.merge(dh17w['total_2017'], how='outer', left_index=True, right_index=True)
339 deaths_headlines_w = deaths_headlines_w.merge(dh16w['total_2016'], how='outer', left_index=True, right_index=True)
340 # deaths_headlines = deaths_headlines.merge(dh15['total_2015'], how='outer', left_index=True, right_index=True)
341 deaths_headlines_w = deaths_headlines_w.merge(dh15w['total_2015'], how='left', left_index=True, right_index=True)
342 deaths_headlines_w
343 ```
344
345 ```python Collapsed="false"
346 deaths_headlines_i = deaths_headlines_i.merge(dh19i['total_2019'], how='outer', left_index=True, right_index=True)
347 deaths_headlines_i = deaths_headlines_i.merge(dh18i['total_2018'], how='outer', left_index=True, right_index=True)
348 deaths_headlines_i = deaths_headlines_i.merge(dh17i['total_2017'], how='outer', left_index=True, right_index=True)
349 deaths_headlines_i = deaths_headlines_i.merge(dh16i['total_2016'], how='outer', left_index=True, right_index=True)
350 deaths_headlines_i = deaths_headlines_i.merge(dh15i['total_2015'], how='left', left_index=True, right_index=True)
351 deaths_headlines_i
352 ```
353
354 ```python Collapsed="false"
355 deaths_headlines_s
356 ```
357
358 ```python Collapsed="false"
359 deaths_headlines = deaths_headlines_e + deaths_headlines_w + deaths_headlines_i + deaths_headlines_s
360 deaths_headlines
361 ```
362
363 ```python Collapsed="false"
364 deaths_headlines_e['previous_mean'] = deaths_headlines_e['total_2019 total_2018 total_2017 total_2016 total_2015'.split()].apply(np.mean, axis=1)
365 deaths_headlines_w['previous_mean'] = deaths_headlines_w['total_2019 total_2018 total_2017 total_2016 total_2015'.split()].apply(np.mean, axis=1)
366 deaths_headlines_s['previous_mean'] = deaths_headlines_s['total_2019 total_2018 total_2017 total_2016 total_2015'.split()].apply(np.mean, axis=1)
367 deaths_headlines_i['previous_mean'] = deaths_headlines_i['total_2019 total_2018 total_2017 total_2016 total_2015'.split()].apply(np.mean, axis=1)
368 deaths_headlines['previous_mean'] = deaths_headlines['total_2019 total_2018 total_2017 total_2016 total_2015'.split()].apply(np.mean, axis=1)
369 deaths_headlines
370 ```
371
372 ```python Collapsed="false"
373 deaths_headlines['total_2020 total_2019 total_2018 total_2017 total_2016 total_2015'.split()].plot(figsize=(14, 8))
374 ```
375
376 ```python Collapsed="false"
377 deaths_headlines[['total_2020', 'previous_mean']].plot(figsize=(10, 8))
378 ```
379
380 ```python Collapsed="false"
381 deaths_headlines_i.plot()
382 ```
383
384 ```python Collapsed="false"
385 # Radar plot code taken from example at https://stackoverflow.com/questions/42878485/getting-matplotlib-radar-plot-with-pandas#
386
387 dhna = deaths_headlines.dropna()
388
389 fig = plt.figure(figsize=(10, 10))
390 ax = fig.add_subplot(111, projection="polar")
391
392 theta = np.roll(
393 np.flip(
394 np.arange(len(dhna))/float(len(dhna))*2.*np.pi),
395 14)
396 # l15, = ax.plot(theta, deaths_headlines['total_2015'], color="#b56363", label="2015") # 0
397 # l16, = ax.plot(theta, deaths_headlines['total_2016'], color="#a4b563", label="2016") # 72
398 # l17, = ax.plot(theta, deaths_headlines['total_2017'], color="#63b584", label="2017") # 144
399 # l18, = ax.plot(theta, deaths_headlines['total_2018'], color="#6384b5", label="2018") # 216
400 # l19, = ax.plot(theta, deaths_headlines['total_2019'], color="#a4635b", label="2019") # 288
401 l15, = ax.plot(theta, dhna['total_2015'], color="#e47d7d", label="2015") # 0
402 l16, = ax.plot(theta, dhna['total_2016'], color="#afc169", label="2016") # 72 , d0e47d
403 l17, = ax.plot(theta, dhna['total_2017'], color="#7de4a6", label="2017") # 144
404 l18, = ax.plot(theta, dhna['total_2018'], color="#7da6e4", label="2018") # 216
405 l19, = ax.plot(theta, dhna['total_2019'], color="#d07de4", label="2019") # 288
406
407 lmean, = ax.plot(theta, dhna['previous_mean'], color="black", linestyle='dashed', label="mean")
408
409 l20, = ax.plot(theta, dhna['total_2020'], color="red", label="2020")
410
411 # deaths_headlines.total_2019.plot(ax=ax)
412
413 def _closeline(line):
414 x, y = line.get_data()
415 x = np.concatenate((x, [x[0]]))
416 y = np.concatenate((y, [y[0]]))
417 line.set_data(x, y)
418
419 [_closeline(l) for l in [l19, l18, l17, l16, l15, lmean]]
420
421
422 ax.set_xticks(theta)
423 ax.set_xticklabels(dhna.index)
424 plt.legend()
425 plt.title("Deaths by week over years, all UK")
426 plt.savefig('deaths-radar.png')
427 plt.show()
428 ```
429
430 <!-- #region Collapsed="false" -->
431 # Excess deaths calculation
432 <!-- #endregion -->
433
434 ```python Collapsed="false"
435 # raw_data_2020.loc[12, 'Week ended']
436 ```
437
438 ```python Collapsed="false"
439 eng_xls.loc[12, 'Week ended']
440 ```
441
442 ```python Collapsed="false"
443 # raw_data_2020.iloc[-1]['Week ended']
444 ```
445
446 ```python Collapsed="false"
447 deaths_headlines_e.total_2020.dropna().last_valid_index()
448 ```
449
450 ```python Collapsed="false"
451 eng_xls.loc[deaths_headlines_e.total_2020.dropna().last_valid_index(), 'Week ended']
452 ```
453
454 ```python Collapsed="false"
455 eng_xls.loc[27, 'Week ended']
456 ```
457
458 ```python Collapsed="false"
459 # raw_data_2020.loc[12].droplevel(1)['Week ended']
460 ```
461
462 ```python Collapsed="false"
463 # raw_data_2020.iloc[-1].droplevel(1)['Week ended']
464 ```
465
466 ```python Collapsed="false"
467 (deaths_headlines.loc[12:].total_2020 - deaths_headlines.loc[12:].previous_mean).sum()
468 ```
469
470 ```python Collapsed="false"
471 (deaths_headlines.loc[12:27].total_2020 - deaths_headlines.loc[12:27].previous_mean).sum()
472 ```
473
474 ```python Collapsed="false"
475 deaths_headlines.previous_mean.sum()
476 ```
477
478 ```python Collapsed="false"
479 # excess_death_data = {
480 # 'start_date': str(eng_xls.loc[12, 'Week ended']),
481 # 'end_date': str(eng_xls.loc[deaths_headlines_e.total_2020.dropna().last_valid_index(), 'Week ended']),
482 # 'excess_deaths': (deaths_headlines.loc[12:].total_2020 - deaths_headlines.loc[12:].previous_mean).sum()
483 # }
484
485 # with open('excess_deaths.json', 'w') as f:
486 # json.dump(excess_death_data, f)
487 ```
488
489 ```python Collapsed="false"
490 # excess_death_data = {
491 # 'start_date': str(eng_xls.loc[12, 'Week ended']),
492 # 'end_date': str(eng_xls.loc[27, 'Week ended']),
493 # 'excess_deaths': (deaths_headlines.loc[12:27].total_2020 - deaths_headlines.loc[12:27].previous_mean).sum()
494 # }
495
496 # with open('excess_deaths.json', 'w') as f:
497 # json.dump(excess_death_data, f)
498 ```
499
500 ```python Collapsed="false"
501 # excess_death_data = {
502 # 'start_date': str(raw_data_2020.loc[12].droplevel(1)['Week ended']),
503 # 'end_date': str(raw_data_2020.iloc[-1].droplevel(1)['Week ended']),
504 # 'excess_deaths': (deaths_headlines.loc[12:].total_2020 - deaths_headlines.loc[12:].previous_mean).sum()
505 # }
506
507 # with open('excess_deaths.json', 'w') as f:
508 # json.dump(excess_death_data, f)
509 ```
510
511 ```python Collapsed="false"
512 eng_xls['Week ended']
513 ```
514
515 ```python Collapsed="false"
516 # raw_data_2020.droplevel(1, axis='columns')['Week ended']
517 ```
518
519 ```python Collapsed="false"
520 deaths_by_week = deaths_headlines.merge(eng_xls['Week ended'], left_index=True, right_index=True)
521 deaths_by_week.rename(columns={'Week ended': 'week_ended'}, inplace=True)
522 deaths_by_week.to_csv('deaths_by_week.csv', header=True, index=False)
523 ```
524
525 ```python Collapsed="false"
526 # deaths_by_week = deaths_headlines.merge(raw_data_2020.droplevel(1, axis='columns')['Week ended'], left_index=True, right_index=True)
527 # deaths_by_week.rename(columns={'Week ended': 'week_ended'}, inplace=True)
528 # deaths_by_week.to_csv('deaths_by_week.csv', header=True, index=False)
529 ```
530
531 <!-- #region Collapsed="false" -->
532 # Plots for UK nations
533 <!-- #endregion -->
534
535 ```python Collapsed="false"
536 # Radar plot code taken from example at https://stackoverflow.com/questions/42878485/getting-matplotlib-radar-plot-with-pandas#
537
538 fig = plt.figure(figsize=(10, 10))
539 ax = fig.add_subplot(111, projection="polar")
540
541 theta = np.roll(
542 np.flip(
543 np.arange(len(deaths_headlines_e))/float(len(deaths_headlines_e))*2.*np.pi),
544 14)
545 l15, = ax.plot(theta, deaths_headlines_e['total_2015'], color="#e47d7d", label="2015") # 0
546 l16, = ax.plot(theta, deaths_headlines_e['total_2016'], color="#afc169", label="2016") # 72 , d0e47d
547 l17, = ax.plot(theta, deaths_headlines_e['total_2017'], color="#7de4a6", label="2017") # 144
548 l18, = ax.plot(theta, deaths_headlines_e['total_2018'], color="#7da6e4", label="2018") # 216
549 l19, = ax.plot(theta, deaths_headlines_e['total_2019'], color="#d07de4", label="2019") # 288
550
551 lmean, = ax.plot(theta, deaths_headlines_e['previous_mean'], color="black", linestyle='dashed', label="mean")
552
553 l20, = ax.plot(theta, deaths_headlines_e['total_2020'], color="red", label="2020")
554
555 # deaths_headlines.total_2019.plot(ax=ax)
556
557 def _closeline(line):
558 x, y = line.get_data()
559 x = np.concatenate((x, [x[0]]))
560 y = np.concatenate((y, [y[0]]))
561 line.set_data(x, y)
562
563 [_closeline(l) for l in [l19, l18, l17, l16, l15, lmean]]
564
565
566 ax.set_xticks(theta)
567 ax.set_xticklabels(deaths_headlines_e.index)
568 plt.legend()
569 plt.title("Deaths by week over years, England")
570 plt.savefig('deaths-radar_england.png')
571 plt.show()
572 ```
573
574 ```python Collapsed="false"
575 # Radar plot code taken from example at https://stackoverflow.com/questions/42878485/getting-matplotlib-radar-plot-with-pandas#
576
577 fig = plt.figure(figsize=(10, 10))
578 ax = fig.add_subplot(111, projection="polar")
579
580 theta = np.roll(
581 np.flip(
582 np.arange(len(deaths_headlines_w))/float(len(deaths_headlines_w))*2.*np.pi),
583 14)
584 l15, = ax.plot(theta, deaths_headlines_w['total_2015'], color="#e47d7d", label="2015") # 0
585 l16, = ax.plot(theta, deaths_headlines_w['total_2016'], color="#afc169", label="2016") # 72 , d0e47d
586 l17, = ax.plot(theta, deaths_headlines_w['total_2017'], color="#7de4a6", label="2017") # 144
587 l18, = ax.plot(theta, deaths_headlines_w['total_2018'], color="#7da6e4", label="2018") # 216
588 l19, = ax.plot(theta, deaths_headlines_w['total_2019'], color="#d07de4", label="2019") # 288
589
590 lmean, = ax.plot(theta, deaths_headlines_w['previous_mean'], color="black", linestyle='dashed', label="mean")
591
592 l20, = ax.plot(theta, deaths_headlines_w['total_2020'], color="red", label="2020")
593
594
595 def _closeline(line):
596 x, y = line.get_data()
597 x = np.concatenate((x, [x[0]]))
598 y = np.concatenate((y, [y[0]]))
599 line.set_data(x, y)
600
601 [_closeline(l) for l in [l19, l18, l17, l16, l15, lmean]]
602
603
604 ax.set_xticks(theta)
605 ax.set_xticklabels(deaths_headlines_w.index)
606 plt.legend()
607 plt.title("Deaths by week over years, Wales")
608 plt.savefig('deaths-radar_wales.png')
609 plt.show()
610 ```
611
612 ```python Collapsed="false"
613 # Radar plot code taken from example at https://stackoverflow.com/questions/42878485/getting-matplotlib-radar-plot-with-pandas#
614
615 fig = plt.figure(figsize=(10, 10))
616 ax = fig.add_subplot(111, projection="polar")
617
618 theta = np.roll(
619 np.flip(
620 np.arange(len(deaths_headlines_s))/float(len(deaths_headlines_s))*2.*np.pi),
621 14)
622 l15, = ax.plot(theta, deaths_headlines_s['total_2015'], color="#e47d7d", label="2015") # 0
623 l16, = ax.plot(theta, deaths_headlines_s['total_2016'], color="#afc169", label="2016") # 72 , d0e47d
624 l17, = ax.plot(theta, deaths_headlines_s['total_2017'], color="#7de4a6", label="2017") # 144
625 l18, = ax.plot(theta, deaths_headlines_s['total_2018'], color="#7da6e4", label="2018") # 216
626 l19, = ax.plot(theta, deaths_headlines_s['total_2019'], color="#d07de4", label="2019") # 288
627
628 lmean, = ax.plot(theta, deaths_headlines_s['previous_mean'], color="black", linestyle='dashed', label="mean")
629
630 l20, = ax.plot(theta, deaths_headlines_s['total_2020'], color="red", label="2020")
631
632
633 def _closeline(line):
634 x, y = line.get_data()
635 x = np.concatenate((x, [x[0]]))
636 y = np.concatenate((y, [y[0]]))
637 line.set_data(x, y)
638
639 [_closeline(l) for l in [l19, l18, l17, l16, l15, lmean]]
640
641
642 ax.set_xticks(theta)
643 ax.set_xticklabels(deaths_headlines_s.index)
644 plt.legend()
645 plt.title("Deaths by week over years, Scotland")
646 plt.savefig('deaths-radar_scotland.png')
647 plt.show()
648 ```
649
650 ```python Collapsed="false"
651 # Radar plot code taken from example at https://stackoverflow.com/questions/42878485/getting-matplotlib-radar-plot-with-pandas#
652
653 fig = plt.figure(figsize=(10, 10))
654 ax = fig.add_subplot(111, projection="polar")
655
656 theta = np.roll(
657 np.flip(
658 np.arange(len(deaths_headlines_i))/float(len(deaths_headlines_i))*2.*np.pi),
659 14)
660 l15, = ax.plot(theta, deaths_headlines_i['total_2015'], color="#e47d7d", label="2015") # 0
661 l16, = ax.plot(theta, deaths_headlines_i['total_2016'], color="#afc169", label="2016") # 72 , d0e47d
662 l17, = ax.plot(theta, deaths_headlines_i['total_2017'], color="#7de4a6", label="2017") # 144
663 l18, = ax.plot(theta, deaths_headlines_i['total_2018'], color="#7da6e4", label="2018") # 216
664 l19, = ax.plot(theta, deaths_headlines_i['total_2019'], color="#d07de4", label="2019") # 288
665
666 lmean, = ax.plot(theta, deaths_headlines_i['previous_mean'], color="black", linestyle='dashed', label="mean")
667
668 l20, = ax.plot(theta, deaths_headlines_i['total_2020'], color="red", label="2020")
669
670
671 def _closeline(line):
672 x, y = line.get_data()
673 x = np.concatenate((x, [x[0]]))
674 y = np.concatenate((y, [y[0]]))
675 line.set_data(x, y)
676
677 [_closeline(l) for l in [l19, l18, l17, l16, l15, lmean]]
678
679
680 ax.set_xticks(theta)
681 ax.set_xticklabels(deaths_headlines_i.index)
682 plt.legend()
683 plt.title("Deaths by week over years, Northern Ireland")
684 plt.savefig('deaths-radar_northern_ireland.png')
685 plt.show()
686 ```
687
688 ```python Collapsed="false"
689 # list(raw_data_2020.columns)
690 ```
691
692 ```python Collapsed="false"
693 # deaths_headlines_e = raw_data_2020.iloc[:, [1]].copy()
694 # deaths_headlines_e.columns = ['total_2020']
695 # deaths_headlines_w = raw_data_2020['W92000004'].copy()
696 # deaths_headlines_e.columns = ['total_2020']
697 # deaths_headlines_w.columns = ['total_2020']
698 # deaths_headlines_e.total_2020 -= deaths_headlines_w.total_2020
699 # deaths_headlines_e.head()
700 # deaths_headlines_e
701 ```
702
703 ```python Collapsed="false"
704
705 ```
706
707 ```python Collapsed="false"
708
709 ```