Skip to content

Instantly share code, notes, and snippets.

@michalskop
Last active March 25, 2020 01:27
Show Gist options
  • Save michalskop/dc2114b3bdda277ef9553faf46c5a41e to your computer and use it in GitHub Desktop.
Save michalskop/dc2114b3bdda277ef9553faf46c5a41e to your computer and use it in GitHub Desktop.
CZ+DE: Covid19 by regions

Covid-19: Comparison of Czech and German regions.

I joined several Czech regions to make them of similar size as the German states.

"""CZ - DE matric chart."""
# import csv
import datetime
import pandas
import plotly.subplots as subplots
import plotly.graph_objects as go
# import requests
path = "/home/michal/dev/coronavirus/regions/"
supregions = [
{
'regions': ['Praha', 'Středočeský'],
'name': 'Prague and Central Bohemia'
},
{
'regions': ['Karlovarský', 'Plzeňský', 'Jihočeský', 'Pardubický', 'Královéhradecký', 'Liberecký', 'Ústecký', 'Vysočina'],
'name': 'Bohemia w/o Prague and Centr.B.'
},
{
'regions': ['Jihomoravský', 'Zlínský', 'Olomoucký', 'Moravskoslezský'],
'name': 'Moravia and Silesia'
}
]
# read data CZ
infected_cz = pandas.read_csv(path + "cz_regions_infected.csv")
basic_info_cz = pandas.read_csv(path + 'cz_regions.csv')
data_cz = pandas.merge(infected_cz, basic_info_cz, on='name')
names_cz = sorted(basic_info_cz["name"].tolist())
series_cz = []
for name in names_cz:
series_cz.append(data_cz[(data_cz['name'] == name)])
dates_cz = []
for row in series_cz[0]['date']:
date = datetime.datetime.strptime(row, "%Y-%m-%d")
dates_cz.append(date)
# join data CZ
i = 0
for sr in supregions:
selected = data_cz[data_cz['name'].isin(sr['regions'])]
df = selected.groupby(['date']).sum()
df = df.reset_index()
df['name'] = [sr['name']] * df.shape[0]
if i == 0:
data_cz_transf = df
else:
data_cz_transf = pandas.concat([data_cz_transf, df])
i += 1
# read data
infected_de = pandas.read_csv(path + "de_regions_infected.csv")
basic_info_de = pandas.read_csv(path + 'de_regions.csv')
data_de = pandas.merge(infected_de, basic_info_de, on='name')
names_de = sorted(basic_info_de["name"].tolist())
series_de = []
for name in names_de:
series_de.append(data_de[(data_de['name'] == name)])
dates_de = []
for row in series_de[-1]['date']:
date = datetime.datetime.strptime(row, "%Y-%m-%d")
dates_de.append(date)
# join CZ + DE
data = pandas.concat([data_cz_transf[['date', 'value', 'population', 'name']], data_de[['date', 'value', 'population', 'name']]])
names_cz_transf = []
for sr in supregions:
names_cz_transf.append(sr['name'])
names = names_cz_transf + names_de
series = []
k = 0
for name in names:
series.append(data[(data['name'] == name)])
k = 0
nnames = []
for name in names:
if k < 3:
prefix = 'CZ: '
else:
prefix = 'DE: '
nnames.append(prefix + name)
k += 1
# charting
y_max = 60
colors = {
'primary': '#ce4414',
'secondary': '#9c948a',
'success': '#2f973e',
'info': '#138496',
'warning': '#ecaa1b',
'danger': '#c7291e',
'dark': '#772953',
'light': '#e9ecef',
'text-light': 'rgba(0, 0, 0, 0.9)',
'text-dark': '#fff',
'text-muted': '#868e96',
'text-primary': '#E95420',
'text-secondary': '#AEA79F',
'text-warning': '#EFB73E',
'text-danger': '#DF382C',
'text-success': '#38B44A',
'text-info': '#17a2b8'
}
dims = [4, 5]
fig = subplots.make_subplots(dims[0], dims[1], shared_yaxes=True, subplot_titles=nnames)
# fig.get_subplot(3,5)[0]['domain'] = (0.5, 0.5)
fig.layout.annotations
i = 1
j = 1
for s in series:
for ss in series:
traceg = go.Scatter(
x=ss['date'].tolist(),
y=(ss['value'] / ss['population'] * 100000).tolist(),
name=ss['name'].tolist()[0],
mode='lines',
line={
'color': colors['text-secondary'],
'width': 2
}
)
fig.add_trace(traceg, row=i, col=j)
trace = go.Scatter(
x=s['date'].tolist(),
y=(s['value'] / s['population'] * 100000).tolist(),
name=s['name'].tolist()[0],
mode='lines',
line={
'color': colors['dark'],
'width': 8
}
)
# trace.textfont = {'color': colors['primary']}
fig.add_trace(trace, row=i, col=j)
fig.update_yaxes(
range=[0, y_max],
color=colors['text-secondary'],
tickfont={'size': 30},
gridwidth=1,
gridcolor=colors['light'],
row=i,
col=j
)
fig.update_xaxes(
tickformat="%d/3",
# tickformat="%d.m.",
color=colors['text-secondary'],
tickfont={'size': 30},
nticks=4,
gridwidth=2,
gridcolor=colors['light'],
)
j += 1
if j == (dims[1] + 1):
j = 1
i += 1
fig.update_layout(
height=1500,
width=2000,
# title_text="Covid 19 - Potvrzené případy na 100 000 obyvatel - Kraje ČR",
showlegend=False,
margin={
't': 200
},
title={
'font': {
'size': 35,
'color': colors['dark']
},
'text': "<b>Covid 19 - Cumulative number of confirmed cases per 100,000 citizens - Czech and German regions</b>"
},
font={"family": "Ubuntu", "color": colors['primary']},
template="plotly_white"
)
# fig.show()
# fig.write_image(path + "de_regions.png")
fig.update_annotations(
font={'size': 20}
)
credit = go.layout.Annotation(
text='Updated: 24/3/2020<br>Author: Michal Škop<br>@skopmichal<br>Data Source: Covid19CZ,<br>MZČR, Robert Koch Institut<br>CC-BY',
align='left',
font={'size': 20, 'color': colors['text-secondary']},
showarrow=False,
xref='paper',
yref='paper',
x=1,
y=0
)
fig.add_annotation(credit)
# fig.show()
fig.write_image(path + "cz_de_regions.png")
fig.update_layout(
height=1000,
width=2000,
)
fig.write_image(path + "cz_de_regions_twitter.png")
View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment