Browse Source

Tornado plot with list of countries

main
parent
commit
ec1598419e
  1. BIN
      tornado/python_outputs/tornado_countries.pdf
  2. BIN
      tornado/python_outputs/tornado_countries_2.pdf
  3. BIN
      tornado/tornado.pdf
  4. 17
      tornado/tornado.tex
  5. 83
      tornado/tornado_plot.py

BIN
tornado/python_outputs/tornado_countries.pdf

BIN
tornado/python_outputs/tornado_countries_2.pdf

BIN
tornado/tornado.pdf

17
tornado/tornado.tex

@ -1,17 +0,0 @@
\documentclass{article}
\usepackage{geometry}
\geometry{a5paper, portrait}
\usepackage{pgf}
\begin{document}
\begin{figure}[ht]
\centering
\includegraphics{tornado_plot.pdf}
\caption{Tornado plot}
\end{figure}
\end{document}

83
tornado/tornado_plot.py

@ -12,14 +12,6 @@ import numpy as np
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
# matplotlib.use("pgf")
# matplotlib.rcParams.update({
# "pgf.texsystem": "pdflatex",
# 'font.family': 'serif',
# 'text.usetex': True,
# 'pgf.rcfonts': False,
# })
# %% Function to convert sizes in cm for figure size
def cm2inch(*tupl):
inch = 2.54
@ -28,50 +20,63 @@ def cm2inch(*tupl):
else:
return tuple(i/inch for i in tupl)
# %%
# %% Countries
r = requests.get('https://disease.sh/v3/covid-19/historical/fr?lastdays=all')
x = r.json()
countries = {
'cn': {'name': 'China'},
'it': {'name': 'Italy'},
'fr': {'name': 'France'},
'de': {'name': 'Germany'},
'us': {'name': 'USA'},
'uk': {'name': 'UK'}
}
#%%
df = pd.DataFrame(x['timeline'])
df.index = pd.to_datetime(df.index)
#df = df.reset_index()
# %% Data
for country_code, country_data in countries.items():
request_url = 'https://disease.sh/v3/covid-19/historical/' + country_code + '?lastdays=all'
r = requests.get(request_url)
x = r.json()
df = pd.DataFrame(x['timeline'])
df.index = pd.to_datetime(df.index)
# %%
df['daily_deaths'] = df['deaths'].diff().abs() # dirty trick to prevent negative outliers
df['daily_deaths_avg'] = df['daily_deaths'].rolling(7).mean()
df['death_change'] = df['daily_deaths_avg'].diff()
#df.fillna(value=0, inplace=True)
# Process data
df['daily_deaths'] = df['deaths'].diff().abs() # .abs() dirty trick to prevent negative outliers
df['daily_deaths_avg'] = df['daily_deaths'].rolling(7).mean()
df['death_change'] = df['daily_deaths_avg'].diff()
# %%
df = df.resample('4H').asfreq()
# Smoothing
df = df.resample('4H').asfreq()
df = df.interpolate(method='spline', order=5)
# %%
df = df.interpolate(method='spline', order=5)
country_data['dataframe'] = df
# %%
# df['daily_deaths_avg'] = df['daily_deaths'].rolling(14).mean()
# df['death_change'] = df['daily_deaths_avg'].diff()
# %% Plotting
# %%
fig, ax = plt.subplots(figsize=cm2inch(15,15))
ax.plot(df['death_change'], df['daily_deaths_avg'], lw=1)
#plt.plot(df['death_change'], df['daily_deaths_avg'], 'ob') # dots for debugging
ax.axvline(x=0, c='black', lw=1, ls=':')
for country_code, country_data in countries.items():
df = country_data['dataframe']
line, = ax.plot(df['death_change'], df['daily_deaths_avg'], lw=0.5, label=country_data['name'])
#plt.plot(df['death_change'], df['daily_deaths_avg'], 'ob') # dots for debugging
df['month'] = df.index.month
df['month_change'] = df['month'].diff()
dates = df['month_change'] == 1
for index, row in df[dates].iterrows():
date_text = row.name.strftime(format='%d %b')
ax.annotate(date_text,
(row['death_change'], row['daily_deaths_avg']),
color=line.get_color())
plt.axvline(x=0, c='black', lw=1, ls=':')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.legend(loc='upper center', ncol=3, bbox_to_anchor=(0.5,1.15))
plt.savefig('tornado_plot.pdf')
# %%
#df.plot(x='death_change', y='daily_deaths_avg')
#%%
#df.plot()
#df.plot(x='death_change', y='tornado')
plt.show()
#plt.savefig('tornado_plot.pdf')
# %%
Loading…
Cancel
Save