Coronavirus Time Series Line and Bar Chart

Preamble

In [4]:
import numpy as np                   # for multi-dimensional containers 
import pandas as pd                  # for DataFrames
import plotly.graph_objects as go    # for data visualisation
import plotly.io as pio              # to set shahin plot layout
from plotly.subplots import make_subplots

pio.templates['shahin'] = pio.to_templated(go.Figure().update_layout(
    legend=dict(orientation="h",y=1.1, x=.5, xanchor='center'),
    margin=dict(t=0,r=0,b=0,l=0))).layout.template
pio.templates.default = 'shahin'

Introduction

In this section, we're going to use daily confirmed cases data for COVID-19 in the UK made available at arcgis.com to create a time series plot. Our goal will be to visualise the number of new cases and cumulative cases over time.

Terms of use taken from the data source

No special restrictions or limitations on using the item’s content have been provided.

Visualising the Table

The first step is to read the Excel data into a pandas.DataFrame and display the first five samples.

In [2]:
data = pd.read_excel('https://www.arcgis.com/sharing/rest/content/items/e5fd11150d274bebaaf8fe2a7a2bda11/data')
data.head()
Out[2]:
DateVal CMODateCount CumCases DailyDeaths CumDeaths
0 2020-01-31 2 2 NaN NaN
1 2020-02-01 0 2 NaN NaN
2 2020-02-02 0 2 NaN NaN
3 2020-02-03 0 2 NaN NaN
4 2020-02-04 0 2 NaN NaN

This data looks ready to plot. We have our dates in a column named DateVal, the new daily cases in a column named CMODateCount, and the daily cumulative cases in a column named CumCases. For this plot, we'll enable a secondary y-axis so that we can present our cumulative cases as a line, and our new cases with bars.

In [3]:
from plotly.subplots import make_subplots

fig = make_subplots(specs=[[{"secondary_y": True}]])

fig.add_trace(go.Scatter(x=data.DateVal, y=data.CumCases,
                         mode='lines+markers',
                         name='Total Cases',
                         line_color='crimson'),
                         secondary_y=True)

fig.add_trace(go.Bar(x=data.DateVal, y=data.CMODateCount,
                     name='New Cases',
                     marker_color='darkslategray'),
                     secondary_y=False)

fig.show()