Update.
[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
48 ax.yaxis.grid(color='gray', linestyle='-', linewidth=0.25)
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 key, color, label, delta in [
58         ('World', 'blue', 'World', 0),
59         ('Switzerland', 'red', 'Switzerland', 14),
60         ('France', 'lightgreen', 'France', 11),
61         ('US', 'black', 'USA', 14),
62         ('Korea, South', 'gray', 'S. Korea', 0),
63         ('Italy', 'purple', 'Italy', 3),
64         ('China', 'orange', 'China', 0)
65 ]:
66     delta = 0
67     ax.plot(dates[:dates.shape[0]-delta], nb_cases[key][delta:], color = color, label = label, linewidth=2)
68
69 # ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), frameon = False)
70 ax.legend(frameon = False)
71
72 plt.show()
73 # fig.savefig('covid19.svg')
74
75 ######################################################################