COVID Time Series

Data from Johns Hopkins University repository

DATA IS FLAWED FOR SOME COUNTRIES

We are waiting for the team to solve this issue.

https://github.com/CSSEGISandData/COVID-19/issues/650

Code

In [1]:
%matplotlib inline

import matplotlib
import matplotlib.pyplot as plt

import pandas as pd
from datetime import datetime
import plotly.graph_objects as go
In [ ]:
 
In [2]:
def plot_file(file, title, limit=100):
    df = pd.read_csv(file)
    #drop unused columns
    df = df.drop(columns=['Province/State', 'Lat', 'Long'])
    #sum province/state from the same country
    #aggregate per country and transpose the table
    per_country = df.groupby('Country/Region').sum().transpose()
    #convert string date to date obj
    per_country.index = per_country.index.map(lambda x: datetime.strptime(x, '%m/%d/%y'))
    per_country.sort_index(inplace=True)
    keep_countries = per_country.columns[per_country.max() >= limit]
    per_country = per_country[keep_countries]

    #plot
    fig = go.Figure()
    for country in per_country.columns:
        fig.add_trace(go.Scatter(x=per_country.index, y=per_country[country],
                        mode='lines+markers',
                        hovertemplate = f"<b>{country}</b><br><br>" +
                                    "%{yaxis.title.text}: %{y:,.0f}<br>" +
                                    "%{xaxis.title.text}: %{x}<br>" +
                                    "<extra></extra>",
                        name=country))
    
    updatemenus = list([
        dict(active=1,
             buttons=list([
                dict(label='Log Scale',
                     method='update',
                     args=[{'visible': [True] * len(per_country.columns)},
                           {'title': f'{title} (Log scale)',
                            'yaxis': {'type': 'log', 'title': 'Number of Cases'}}]),
                dict(label='Linear Scale',
                     method='update',
                     args=[{'visible': [True] * len(per_country.columns)},
                           {'title': f'{title} (Linear scale)',
                            'yaxis': {'type': 'linear', 'title': 'Number of Cases'}}])
                ]),
            )
    ])
    
    #customize layout
    fig.update_layout(title=f'{title} (Linear scale)',
                      xaxis_title='Date',
                      yaxis_title='Number of Cases',
                      autosize=True,
                      height=600,
                      updatemenus=updatemenus,
                      annotations = [dict(xref='paper',
                                          yref='paper',
                                          x=0.5, y=1.1,
                                          showarrow=False,
                                          text = f'Only countries with more than {limit} {title}<br>at any given point in time')])
    fig.show()
In [ ]:
 

 .

Instructions / Details

 Confirmed

In [3]:
plot_file(
    'https://github.com/CSSEGISandData/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv',
    'Confirmed Cases', limit=100)

 Recovered

Deaths

In [4]:
plot_file(
    'https://github.com/CSSEGISandData/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv', 
     'Deaths', limit=20)
In [ ]: