MAT-PLOT-LIB PACKAGE
CW - Stacked Bar Plot
dvp2_barplots
In [ ]:
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 [ ]:
import json
import urllib.request

The following url gets the latest dataset from the portal, hence the graphs and the data may not look at a later point of time

In [ ]:
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)
In [ ]:
df
Out[ ]:
an ap ar as br ch ct date dateymd dd ... sk status tg tn tr tt un up ut wb
0 0 1 0 0 0 0 0 14-Mar-20 2020-03-14 0 ... 0 Confirmed 1 1 0 81 0 12 0 0
1 0 0 0 0 0 0 0 14-Mar-20 2020-03-14 0 ... 0 Recovered 0 0 0 9 0 4 0 0
2 0 0 0 0 0 0 0 14-Mar-20 2020-03-14 0 ... 0 Deceased 0 0 0 2 0 0 0 0
3 0 0 0 0 0 0 0 15-Mar-20 2020-03-15 0 ... 0 Confirmed 2 0 0 27 0 1 0 0
4 0 0 0 0 0 0 0 15-Mar-20 2020-03-15 0 ... 0 Recovered 1 0 0 4 0 0 0 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1558 2 1835 255 857 38 1 114 15-Aug-21 2021-08-15 0 ... 213 Recovered 582 1842 253 35936 0 36 31 709
1559 0 16 0 10 0 0 1 15-Aug-21 2021-08-15 0 ... 0 Deceased 1 23 4 421 0 1 0 12
1560 1 909 165 758 14 2 68 16-Aug-21 2021-08-16 0 ... 20 Confirmed 405 1851 52 24696 0 17 18 502
1561 0 1543 249 1014 42 3 224 16-Aug-21 2021-08-16 0 ... 147 Recovered 577 1911 223 36871 0 36 54 691
1562 0 13 0 10 0 0 1 16-Aug-21 2021-08-16 0 ... 0 Deceased 3 28 1 438 0 1 1 9

1563 rows × 42 columns

In [ ]:
df.head()
Out[ ]:
an ap ar as br ch ct date dateymd dd ... sk status tg tn tr tt un up ut wb
0 0 1 0 0 0 0 0 14-Mar-20 2020-03-14 0 ... 0 Confirmed 1 1 0 81 0 12 0 0
1 0 0 0 0 0 0 0 14-Mar-20 2020-03-14 0 ... 0 Recovered 0 0 0 9 0 4 0 0
2 0 0 0 0 0 0 0 14-Mar-20 2020-03-14 0 ... 0 Deceased 0 0 0 2 0 0 0 0
3 0 0 0 0 0 0 0 15-Mar-20 2020-03-15 0 ... 0 Confirmed 2 0 0 27 0 1 0 0
4 0 0 0 0 0 0 0 15-Mar-20 2020-03-15 0 ... 0 Recovered 1 0 0 4 0 0 0 0

5 rows × 42 columns

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

5 rows × 38 columns

In [ ]:
df = df.apply(pd.to_numeric)
In [ ]:
df.head()
Out[ ]:
an ap ar as br ch ct dd dl dn ... py rj sk tg tn tr un up ut wb
status
Confirmed 0 1 0 0 0 0 0 0 7 0 ... 0 3 0 1 1 0 0 12 0 0
Recovered 0 0 0 0 0 0 0 0 1 0 ... 0 1 0 0 0 0 0 4 0 0
Deceased 0 0 0 0 0 0 0 0 1 0 ... 0 0 0 0 0 0 0 0 0 0
Confirmed 0 0 0 0 0 0 0 0 0 0 ... 0 1 0 2 0 0 0 1 0 0
Recovered 0 0 0 0 0 0 0 0 1 0 ... 0 2 0 1 0 0 0 0 0 0

5 rows × 38 columns

In [ ]:
df.info()
<class 'pandas.core.frame.DataFrame'>
Index: 1563 entries, Confirmed to Deceased
Data columns (total 38 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   an      1563 non-null   int64
 1   ap      1563 non-null   int64
 2   ar      1563 non-null   int64
 3   as      1563 non-null   int64
 4   br      1563 non-null   int64
 5   ch      1563 non-null   int64
 6   ct      1563 non-null   int64
 7   dd      1563 non-null   int64
 8   dl      1563 non-null   int64
 9   dn      1563 non-null   int64
 10  ga      1563 non-null   int64
 11  gj      1563 non-null   int64
 12  hp      1563 non-null   int64
 13  hr      1563 non-null   int64
 14  jh      1563 non-null   int64
 15  jk      1563 non-null   int64
 16  ka      1563 non-null   int64
 17  kl      1563 non-null   int64
 18  la      1563 non-null   int64
 19  ld      1563 non-null   int64
 20  mh      1563 non-null   int64
 21  ml      1563 non-null   int64
 22  mn      1563 non-null   int64
 23  mp      1563 non-null   int64
 24  mz      1563 non-null   int64
 25  nl      1563 non-null   int64
 26  or      1563 non-null   int64
 27  pb      1563 non-null   int64
 28  py      1563 non-null   int64
 29  rj      1563 non-null   int64
 30  sk      1563 non-null   int64
 31  tg      1563 non-null   int64
 32  tn      1563 non-null   int64
 33  tr      1563 non-null   int64
 34  un      1563 non-null   int64
 35  up      1563 non-null   int64
 36  ut      1563 non-null   int64
 37  wb      1563 non-null   int64
dtypes: int64(38)
memory usage: 476.2+ KB
In [ ]:
df.describe()
Out[ ]:
an ap ar as br ch ct dd dl dn ... py rj sk tg tn tr un up ut wb
count 1563.000000 1563.000000 1563.000000 1563.000000 1563.000000 1563.000000 1563.000000 1563.0 1563.00000 1563.000000 ... 1563.000000 1563.000000 1563.000000 1563.000000 1563.000000 1563.000000 1563.000000 1563.000000 1563.000000 1563.000000
mean 9.650672 2541.262956 64.741523 737.148433 928.202815 79.346769 1283.742802 0.0 1838.62380 13.472809 ... 155.964171 1220.555342 35.265515 830.433141 3301.915547 103.016635 0.000000 2186.507358 434.306462 1963.082534
std 20.176102 4638.046025 115.615337 1292.233591 2357.802382 163.126656 3018.266896 0.0 4174.90242 37.659159 ... 329.881331 3160.328725 71.229907 1427.129346 6492.713631 184.743920 101.076453 5765.398843 1171.604525 3989.287449
min 0.000000 0.000000 0.000000 -1.000000 -2837.000000 0.000000 0.000000 0.0 0.00000 0.000000 ... 0.000000 0.000000 -1.000000 0.000000 0.000000 -2.000000 -1295.000000 -3.000000 -374.000000 -19.000000
25% 0.000000 21.000000 0.000000 2.000000 5.000000 1.000000 7.000000 0.0 32.00000 0.000000 ... 1.000000 12.000000 0.000000 7.000000 49.000000 0.000000 0.000000 25.000000 3.000000 34.500000
50% 1.000000 128.000000 1.000000 42.000000 86.000000 8.000000 156.000000 0.0 218.00000 0.000000 ... 22.000000 139.000000 2.000000 181.000000 673.000000 5.000000 0.000000 167.000000 51.000000 295.000000
75% 11.000000 2756.000000 81.000000 1057.000000 683.000000 77.500000 1135.000000 0.0 1678.50000 8.000000 ... 134.500000 1146.000000 29.000000 1175.000000 3857.000000 121.000000 0.000000 1773.000000 329.000000 2637.000000
max 149.000000 24819.000000 566.000000 6573.000000 15853.000000 963.000000 18746.000000 0.0 28395.00000 359.000000 ... 2049.000000 29459.000000 420.000000 17183.000000 36184.000000 1066.000000 1043.000000 40852.000000 9642.000000 20846.000000

8 rows × 38 columns

In [ ]:
df_ = df.tail(3)
In [ ]:
df_
Out[ ]:
an ap ar as br ch ct dd dl dn ... py rj sk tg tn tr un up ut wb
status
Confirmed 1 909 165 758 14 2 68 0 27 2 ... 49 11 20 405 1851 52 0 17 18 502
Recovered 0 1543 249 1014 42 3 224 0 73 0 ... 86 44 147 577 1911 223 0 36 54 691
Deceased 0 13 0 10 0 0 1 0 0 0 ... 0 0 0 3 28 1 0 1 1 9

3 rows × 38 columns

In [ ]:
df_ = df_.T
In [ ]:
df_
Out[ ]:
status Confirmed Recovered Deceased
an 1 0 0
ap 909 1543 13
ar 165 249 0
as 758 1014 10
br 14 42 0
ch 2 3 0
ct 68 224 1
dd 0 0 0
dl 27 73 0
dn 2 0 0
ga 62 96 5
gj 14 13 0
hp 276 334 3
hr 22 18 2
jh 35 13 0
jk 77 135 1
ka 1065 1486 28
kl 12294 18542 142
la 4 9 0
ld 6 6 0
mh 4145 5811 100
ml 352 649 8
mn 275 546 6
mp 7 16 1
mz 249 954 3
nl 44 111 4
or 868 1167 66
pb 30 48 2
py 49 86 0
rj 11 44 0
sk 20 147 0
tg 405 577 3
tn 1851 1911 28
tr 52 223 1
un 0 0 0
up 17 36 1
ut 18 54 1
wb 502 691 9
In [ ]:
df_.head()
Out[ ]:
status Confirmed Recovered Deceased
an 1 0 0
ap 909 1543 13
ar 165 249 0
as 758 1014 10
br 14 42 0
In [ ]:
plt.bar(df_.index, df_.Confirmed)
Out[ ]:
<BarContainer object of 38 artists>
In [ ]:
plt.bar(df_.index, df_.Deceased)
Out[ ]:
<BarContainer object of 38 artists>
In [ ]:
plt.bar(df_.index, df_.Recovered)
Out[ ]:
<BarContainer object of 38 artists>
In [ ]:
plt.bar(df_.index, df_.Confirmed)
plt.xticks(rotation=90);

Stacked Bar

In [ ]:
plt.bar(df_.index, df_.Confirmed)
plt.bar(df_.index, df_.Recovered, bottom=df_.Confirmed)
plt.bar(df_.index, df_.Deceased, bottom=df_.Confirmed + df_.Recovered)
plt.xticks(rotation=90);
In [ ]:
fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.Confirmed)
plt.bar(df_.index, df_.Recovered, bottom=df_.Confirmed)
plt.bar(df_.index, df_.Deceased, bottom=df_.Confirmed + df_.Recovered)
plt.xticks(rotation=90);
In [ ]:
df_sum = df_.sum()
In [ ]:
df_sum
Out[ ]:
status
Confirmed    24696
Recovered    36871
Deceased       438
dtype: int64
In [ ]:
fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.Confirmed, color = 'Orange')
plt.bar(df_.index, df_.Recovered, bottom=df_.Confirmed, color = "Green")
plt.bar(df_.index, df_.Deceased, bottom=df_.Confirmed + df_.Recovered, color = "Red")
plt.xticks(rotation=90);
In [ ]:
for i, val in enumerate(df_.index): print(i, val)
0 an
1 ap
2 ar
3 as
4 br
5 ch
6 ct
7 dd
8 dl
9 dn
10 ga
11 gj
12 hp
13 hr
14 jh
15 jk
16 ka
17 kl
18 la
19 ld
20 mh
21 ml
22 mn
23 mp
24 mz
25 nl
26 or
27 pb
28 py
29 rj
30 sk
31 tg
32 tn
33 tr
34 un
35 up
36 ut
37 wb
In [ ]:
fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.Confirmed, color = 'Orange')
plt.bar(df_.index, df_.Recovered, bottom=df_.Confirmed, color = "Green")
plt.bar(df_.index, df_.Deceased, bottom=df_.Confirmed + df_.Recovered, color = "Red")
plt.xticks(rotation=90);

for i, val in enumerate(df_.index):
    y = df_.loc[val].sum()
    x = i
    plt.text(x, y, str(y))
In [ ]:
fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.Confirmed, color = 'Orange')
plt.bar(df_.index, df_.Recovered, bottom=df_.Confirmed, color = "Green")
plt.bar(df_.index, df_.Deceased, bottom=df_.Confirmed + df_.Recovered, color = "Red")
plt.xticks(rotation=90);

for i, val in enumerate(df_.index):
    y = df_.loc[val].sum() + 100
    x = i
    plt.text(x, y, str(y), ha='center');
In [ ]:
fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.Confirmed, color = 'Orange')
plt.bar(df_.index, df_.Recovered, bottom=df_.Confirmed, color = "Green")
plt.bar(df_.index, df_.Deceased, bottom=df_.Confirmed + df_.Recovered, color = "Red")
plt.xticks(rotation=90);

for i, val in enumerate(df_.index):
    y = df_.loc[val].sum() + 100
    if y > 500:
        x = i
        plt.text(x, y, str(y), ha='center');

Relative Stacked bar plot with MatPlotLib Library

In [ ]:
df_
Out[ ]:
status Confirmed Recovered Deceased
an 1 0 0
ap 909 1543 13
ar 165 249 0
as 758 1014 10
br 14 42 0
ch 2 3 0
ct 68 224 1
dd 0 0 0
dl 27 73 0
dn 2 0 0
ga 62 96 5
gj 14 13 0
hp 276 334 3
hr 22 18 2
jh 35 13 0
jk 77 135 1
ka 1065 1486 28
kl 12294 18542 142
la 4 9 0
ld 6 6 0
mh 4145 5811 100
ml 352 649 8
mn 275 546 6
mp 7 16 1
mz 249 954 3
nl 44 111 4
or 868 1167 66
pb 30 48 2
py 49 86 0
rj 11 44 0
sk 20 147 0
tg 405 577 3
tn 1851 1911 28
tr 52 223 1
un 0 0 0
up 17 36 1
ut 18 54 1
wb 502 691 9
In [ ]:
df_['Total'] = 0
In [ ]:
df_
Out[ ]:
status Confirmed Recovered Deceased Total
an 1 0 0 0
ap 909 1543 13 0
ar 165 249 0 0
as 758 1014 10 0
br 14 42 0 0
ch 2 3 0 0
ct 68 224 1 0
dd 0 0 0 0
dl 27 73 0 0
dn 2 0 0 0
ga 62 96 5 0
gj 14 13 0 0
hp 276 334 3 0
hr 22 18 2 0
jh 35 13 0 0
jk 77 135 1 0
ka 1065 1486 28 0
kl 12294 18542 142 0
la 4 9 0 0
ld 6 6 0 0
mh 4145 5811 100 0
ml 352 649 8 0
mn 275 546 6 0
mp 7 16 1 0
mz 249 954 3 0
nl 44 111 4 0
or 868 1167 66 0
pb 30 48 2 0
py 49 86 0 0
rj 11 44 0 0
sk 20 147 0 0
tg 405 577 3 0
tn 1851 1911 28 0
tr 52 223 1 0
un 0 0 0 0
up 17 36 1 0
ut 18 54 1 0
wb 502 691 9 0
In [ ]:
df_.head(2)
Out[ ]:
status Confirmed Recovered Deceased Total
an 1 0 0 0
ap 909 1543 13 0
In [ ]:
# df_["Total"] = df_.Confirmed + df_.Recovered + df_.Deceased
# or use the following simple code
df_.Total = df_.sum(axis = 1)
In [ ]:
df_.head()
Out[ ]:
status Confirmed Recovered Deceased Total
an 1 0 0 1
ap 909 1543 13 2465
ar 165 249 0 414
as 758 1014 10 1782
br 14 42 0 56
In [ ]:
df_['ConFrac'] = df_.Confirmed / df_.Total
df_['RecFrac'] = df_.Recovered / df_.Total
df_['DecFrac'] = df_.Deceased / df_.Total
In [ ]:
df_.head(2)
Out[ ]:
status Confirmed Recovered Deceased Total ConfFrac ConFrac RecFrac DecFrac
an 1 0 0 1 1.000000 1.000000 0.000000 0.000000
ap 909 1543 13 2465 0.368763 0.368763 0.625963 0.005274
In [ ]:
df_.drop('ConfFrac', inplace=True, axis=1)
In [ ]:
df_.head()
Out[ ]:
status Confirmed Recovered Deceased Total ConFrac RecFrac DecFrac
an 1 0 0 1 1.000000 0.000000 0.000000
ap 909 1543 13 2465 0.368763 0.625963 0.005274
ar 165 249 0 414 0.398551 0.601449 0.000000
as 758 1014 10 1782 0.425365 0.569024 0.005612
br 14 42 0 56 0.250000 0.750000 0.000000
In [ ]:
fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.ConFrac, color = 'Orange')
plt.bar(df_.index, df_.RecFrac, bottom=df_.ConFrac, color = "Green")
plt.bar(df_.index, df_.DecFrac, bottom=df_.ConFrac + df_.RecFrac, color = "Red")
plt.xticks(rotation=90);
In [ ]:
df_ = df_.sort_values('ConFrac', ascending=False)
In [ ]:
fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.ConFrac, color = 'Orange')
plt.bar(df_.index, df_.RecFrac, bottom=df_.ConFrac, color = "Green")
plt.bar(df_.index, df_.DecFrac, bottom=df_.ConFrac + df_.RecFrac, color = "Red")
plt.xticks(rotation=90);
In [ ]:
df_ = df_.sort_values('Total', ascending=False)

fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.Confirmed, color = 'Orange')
plt.bar(df_.index, df_.Recovered, bottom=df_.Confirmed, color = "Green")
plt.bar(df_.index, df_.Deceased, bottom=df_.Confirmed + df_.Recovered, color = "Red")
plt.xticks(rotation=90);

for i, val in enumerate(df_.index):
    y = df_.loc[val, "Total"] + 100
    if y > 500:
        x = i
        plt.text(x, y, str(y), ha='center');
In [ ]:
df_ = df_.sort_values('Total', ascending=False)

fig = plt.gcf()
fig.set_size_inches(16, 16)
plt.barh(df_.index, df_.Confirmed, color = 'Orange')
plt.barh(df_.index, df_.Recovered, left=df_.Confirmed, color = "Green")
plt.barh(df_.index, df_.Deceased, left=df_.Confirmed + df_.Recovered, color = "Red")
plt.xticks(rotation=90);
In [ ]: