Last active
March 25, 2020 16:38
-
-
Save matteo-peltarion/d0ca932d1972d28f856fe5ea3b70e62c to your computer and use it in GitHub Desktop.
COVID-19 analysis code 04 - plot europe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Utility function to retrieve data from a single country | |
def get_country_df(world_df, country_name): | |
# Some countries have several Provinces/States, must aggregate | |
country_df = world_df[world_df['Country/Region'] == country_name] \ | |
.groupby(["Country/Region", "Date"]) \ | |
.sum() \ | |
.sort_values(by='Date') | |
# Restore columns | |
country_df['Country/Region'] = [i[0] for i in country_df.index] | |
country_df['Date'] = [i[1] for i in country_df.index] | |
return country_df | |
# Plot | |
ax = plt.gca() | |
sns.set_style("whitegrid", {'grid.linestyle': ':'}) | |
ax.yaxis.set_major_locator(ticker.MultipleLocator(Y_GRID_TICK/5)) | |
ax.xaxis.set_major_locator(ticker.MultipleLocator(3)) | |
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y')) | |
countries_to_plot = [ | |
"Spain", | |
"France", | |
"Germany", | |
] | |
for cc in countries_to_plot: | |
c_df = get_country_df(world_df, cc) | |
c_df = c_df[c_df['Date'] > datetime(2020, 2,14).date()] | |
c_df.plot(x='Date', y=["Confirmed"], figsize=(20,10), ax=ax, marker='o', alpha=0.2) | |
pass | |
italy_delayed = get_country_df(world_df, "Italy") | |
italy_delayed = italy_delayed.iloc[:-9,:] | |
spain_df = get_country_df(world_df, "Spain") | |
italy_delayed['Date'] = list(spain_df[spain_df['Date'] >= datetime(2020, 2, 9).date()]['Date']) | |
# Update date dynamically | |
end_date_skandinavia = (datetime.today() - timedelta(days=6)).date().strftime("%m-%d-%Y") | |
# end_date_skandinavia = (datetime.today() - timedelta(days=20)).date().strftime("%m-%d-%Y") | |
norway_anticipated = get_country_df(world_df, "Norway") | |
norway_anticipated['Date'] = pd.date_range(end=end_date_skandinavia, periods=len(norway_anticipated)) | |
sweden_anticipated = get_country_df(world_df, "Sweden") | |
sweden_anticipated['Date'] = pd.date_range(end=end_date_skandinavia, periods=len(sweden_anticipated)) | |
# Plot adjusted countries | |
italy_delayed = italy_delayed[italy_delayed['Date'] > datetime(2020, 2,14).date()] | |
italy_delayed.plot(x='Date', y=["Confirmed"], figsize=(20,10), ax=ax, marker='x', ls="--") | |
norway_anticipated = norway_anticipated[norway_anticipated['Date'] > "2020-02-14"] | |
sweden_anticipated = sweden_anticipated[sweden_anticipated['Date'] > "2020-02-14"] | |
norway_anticipated.plot(x='Date', y=["Confirmed"], figsize=(20,10), ax=ax, marker='x') | |
sweden_anticipated.plot(x='Date', y=["Confirmed"], figsize=(20,10), ax=ax, marker='x') | |
ax.legend(countries_to_plot + ['Italy (delayed)', 'Norway (anticipated)', 'Sweden (anticipated)']) | |
ax.set_ylabel("# of confirmed cases") | |
plt.title("Date-aligned data"); | |
# Uncomment if you want to save the image | |
# plt.savefig("europe_aligned_dates.png"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment