MAT PLOT LIB PACKAGE 
CW Stacked Area Plot
TimevaryingCompositeData
In [36]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use(['dark_background'])
import seaborn as sns
sns.set(color_codes = True)
In [37]:
import json
import urllib.request
In [38]:
url = 'https://api.covid19india.org/states_daily.json'
urllib.request.urlretrieve(url, 'data.json')

with open('data.json') as f:
    data = json.load(f)
data = data['states_daily']
df = pd.json_normalize(data)

Data Preparation

In [39]:
df.drop('dateymd', inplace=True, axis=1)
In [40]:
df.drop('tt', inplace = True, axis=1)
In [41]:
df.head(2)
Out[41]:
an ap ar as br ch ct date dd dl ... rj sk status tg tn tr un up ut wb
0 0 1 0 0 0 0 0 14-Mar-20 0 7 ... 3 0 Confirmed 1 1 0 0 12 0 0
1 0 0 0 0 0 0 0 14-Mar-20 0 1 ... 1 0 Recovered 0 0 0 0 4 0 0

2 rows × 40 columns

In [42]:
df_ = df[['mh', 'date', 'status']]
In [43]:
df_.head(2)
Out[43]:
mh date status
0 14 14-Mar-20 Confirmed
1 0 14-Mar-20 Recovered
In [44]:
df_['mh'] = pd.to_numeric(df_['mh'])
df_['date'] = pd.to_datetime(df_['date'])
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  """Entry point for launching an IPython kernel.
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:2: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  
In [45]:
df_ = df_.pivot_table(index='date', columns='status', values='mh')
In [46]:
df_.head()
Out[46]:
status Confirmed Deceased Recovered
date
2020-03-14 14 0 0
2020-03-15 18 0 0
2020-03-16 6 0 0
2020-03-17 3 1 0
2020-03-18 3 0 0

Area Plots

In [68]:
df_.plot()
Out[68]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fe6a4a15910>
In [67]:
df_.plot.area();
In [73]:
plt.stackplot(df_.index, df_.Confirmed, df_.Recovered, df_.Deceased)
Out[73]:
[<matplotlib.collections.PolyCollection at 0x7fe6a3083390>,
 <matplotlib.collections.PolyCollection at 0x7fe6a2ff0ad0>,
 <matplotlib.collections.PolyCollection at 0x7fe6a2ff0dd0>]
In [56]:
fig = plt.gcf()
fig.set_size_inches(15, 6)
plt.stackplot(df_.index, df_.Confirmed, df_.Recovered, df_.Deceased,
              colors = ['orange', 'green', 'red'])
Out[56]:
[<matplotlib.collections.PolyCollection at 0x7fe6a4dd6350>,
 <matplotlib.collections.PolyCollection at 0x7fe6a4e02e90>,
 <matplotlib.collections.PolyCollection at 0x7fe6a4d9f1d0>]
In [72]:
fig = plt.gcf()
fig.set_size_inches(15, 6)
plt.stackplot(df_.index, df_.Confirmed, df_.Recovered, df_.Deceased,
              labels = ['Confirmed', 'Recovered', "Deceased"],
              colors = ['orange', 'green', 'red'])
plt.legend()
Out[72]:
<matplotlib.legend.Legend at 0x7fe6a490ec90>
In [71]:
fig = plt.gcf()
fig.set_size_inches(15, 6)
plt.stackplot(df_.index, df_.Confirmed/df_.sum(axis=1), 
              df_.Recovered/df_.sum(axis=1), 
              df_.Deceased/df_.sum(axis=1),
              labels = ['Confirmed', 'Recovered', "Deceased"],
              colors = ['orange', 'green', 'red']);
plt.legend()
Out[71]:
<matplotlib.legend.Legend at 0x7fe6a4931850>
In [65]:
df__ = df_[df_ < 0] = 0
In [70]:
df_.head()
Out[70]:
status Confirmed Deceased Recovered
date
2020-03-14 14 0 0
2020-03-15 18 0 0
2020-03-16 6 0 0
2020-03-17 3 1 0
2020-03-18 3 0 0

write a function to draw stacked area plot by state

In [76]:
def plot_stackedarea_bystate(state):
    df_ = df[[state, 'date', 'status']]
    df_[state] = pd.to_numeric(df_[state])
    df_['date'] = pd.to_datetime(df_['date'])
    df_ = df_.pivot_table(values = state, columns = 'status', index='date')
    fig = plt.gcf()
    fig.set_size_inches(15, 6)
    plt.stackplot(df_.index, df_.Confirmed/df_.sum(axis=1), 
              df_.Recovered/df_.sum(axis=1), 
              df_.Deceased/df_.sum(axis=1),
              labels = ['Confirmed', 'Recovered', "Deceased"],
              colors = ['orange', 'green', 'red']);
    plt.legend()
In [78]:
df_.head()
Out[78]:
status Confirmed Deceased Recovered
date
2020-03-14 14 0 0
2020-03-15 18 0 0
2020-03-16 6 0 0
2020-03-17 3 1 0
2020-03-18 3 0 0
In [79]:
plot_stackedarea_bystate('tn')
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:3: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  This is separate from the ipykernel package so we can avoid doing imports until
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  after removing the cwd from sys.path.
In [80]:
plot_stackedarea_bystate('wb')
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:3: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  This is separate from the ipykernel package so we can avoid doing imports until
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  after removing the cwd from sys.path.
In [82]:
plot_stackedarea_bystate('dl')
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:3: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  This is separate from the ipykernel package so we can avoid doing imports until
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  after removing the cwd from sys.path.
In [83]:
!pip install nbconvert
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /usr/local/lib/python3.7/dist-packages (from packaging->bleach->nbconvert) (3.0.7)
In [ ]:
%shell jupyter nbconvert --to html /content/dvp2_barplots.ipynb