Initial commit.
[python.git] / covid19.py
1 #!/usr/bin/env python
2
3 import os, time, math
4 import numpy, csv
5 import matplotlib.pyplot as plt
6 import matplotlib.dates as mdates
7 import urllib.request
8
9 url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv'
10
11 file = 'time_series_19-covid-Confirmed.csv'
12
13 ######################################################################
14
15 if not os.path.isfile(file) or os.path.getmtime(file) < time.time() - 86400:
16     print('Retrieving file')
17     urllib.request.urlretrieve(url, file)
18
19 ######################################################################
20
21 with open(file, newline='') as csvfile:
22     reader = csv.reader(csvfile, delimiter=',')
23     times = []
24     nb_cases = {}
25     time_col = 5
26     for row_nb, row in enumerate(reader):
27         for col_nb, field in enumerate(row):
28             if row_nb >= 1 and col_nb == 1:
29                 country = field
30                 if not country in nb_cases:
31                     nb_cases[country] = numpy.zeros(len(times))
32                 # print(country)
33             if row_nb == 0 and col_nb >= time_col:
34                 times.append(time.mktime(time.strptime(field, '%m/%d/%y')))
35             if row_nb == 1 and col_nb == time_col:
36                 nb_cases['World'] = numpy.zeros(len(times))
37             if row_nb >= 1:
38                 if col_nb >= time_col:
39                     nb_cases['World'][col_nb - time_col] += int(field)
40                     nb_cases[country][col_nb - time_col] += int(field)
41
42 ######################################################################
43
44 fig = plt.figure()
45 ax = fig.add_subplot(1, 1, 1)
46
47 ax.grid(color='gray', linestyle='-', linewidth=0.25)
48
49 ax.set_title('Nb. of COVID-19 cases')
50 ax.set_xlabel('Date', labelpad = 10)
51 ax.set_yscale('log')
52
53 myFmt = mdates.DateFormatter('%b %d')
54 ax.xaxis.set_major_formatter(myFmt)
55 dates = mdates.epoch2num(times)
56
57 for label, color in [ ('World', 'blue'),
58                       ('Switzerland', 'red'),
59                       ('France', 'green'),
60                       ('South Korea', 'gray'),
61                       ('Mainland China', 'orange') ]:
62     ax.plot(dates, nb_cases[label], color = color, label = label)
63
64 # ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), frameon = False)
65 ax.legend(frameon = False)
66
67 plt.show()
68 # fig.savefig('covid19.svg')
69
70 ######################################################################