Difference makes the DIFFERENCE
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
plt.style.use(['dark_background'])
import urllib.request
import json
import seaborn as sns
sns.set(color_codes=True)
p = sns.load_dataset('penguins')
p.head()
p.groupby('species')['species'].count()
c = p.groupby('species')['species'].count()
plt.pie(c);
plt.show()
plt.pie(c, labels=c.index);
plt.show()
plt.pie(c, labels=c.index, autopct="%.2f%%");
plt.show()
plt.pie(c, labels=c.index, autopct="%.2f%%",
explode=[0, 1, 0]);
plt.show()
plt.pie(c, labels=c.index, autopct="%.2f%%",
explode=[0, 1, 0], startangle=180);
plt.show()
plt.pie(c, labels=c.index, autopct="%.2f%%",
explode=[0, 1, 0], startangle=180, shadow=True);
plt.show()
plt.pie(np.random.randint(0, 10, 10));
plt.show()
plt.pie(np.random.randint(0, 10, 10), wedgeprops=dict(width=0.3));
plt.show()
cmap = plt.get_cmap('Accent')
my_colours = cmap(np.arange(10))
plt.pie(np.random.randint(0, 10, 10),
wedgeprops=dict(width=0.3),
colors=my_colours);
plt.show()
plt.pie(c, labels=c.index, autopct="%.2f%%", wedgeprops=dict(width=0.3));
plt.show()
c_i = p.groupby('island')['island'].count()
plt.pie(c_i, labels=c_i.index, autopct="%.2f%%", wedgeprops=dict(width=0.3));
plt.show()
c = pd.crosstab(p.species, p.island)
c = c.T
c
plt.pie(c.sum(axis=1), labels=c.index,
radius = 1, wedgeprops=dict(width=0.3));
plt.pie(c.sum(axis=1), labels=c.index,
radius = 1, wedgeprops=dict(width=0.3));
plt.pie(c.values.flatten(), radius=0.7,
wedgeprops=dict(width=0.3));
cmap = plt.get_cmap('tab20c')
outer_colors = cmap(np.array([0, 4, 8]))
inner_colors = cmap(np.array([1, 2, 3, 5, 6, 7, 9, 10, 11]))
plt.pie(c.sum(axis=1), labels=c.index,
radius = 1, wedgeprops=dict(width=0.3),
colors=outer_colors);
plt.pie(c.values.flatten(), radius=0.7,
wedgeprops=dict(width=0.3),
colors=inner_colors);
plt.pie(c.sum(axis=1), labels=c.index,
radius = 1, wedgeprops=dict(width=0.3),
colors=outer_colors);
plt.pie(c.values.flatten(), radius=0.7,
labels = ['A', '', 'G', 'A', 'C', '', 'A', '', ''],
wedgeprops=dict(width=0.3),
colors=inner_colors);
plt.pie(c.sum(axis=1), labels=c.index,
radius = 1, wedgeprops=dict(width=0.3),
colors=outer_colors);
plt.pie(c.values.flatten(), radius=0.7,
labels = ['A', '', 'G', 'A', 'C', '', 'A', '', ''],
wedgeprops=dict(width=0.3),
colors=inner_colors,
labeldistance=0.75);
cmap = plt.get_cmap('tab20b')
outer_colors = cmap(np.array([0, 4, 8]))
inner_colors = cmap(np.array([1, 2, 3, 5, 6, 7, 9, 10, 11]))
plt.pie(c.sum(axis=1), labels=c.index,
radius = 1, wedgeprops=dict(width=0.3),
colors=outer_colors);
plt.pie(c.values.flatten(), radius=0.7,
labels = ['A', '', 'G', 'A', 'C', '', 'A', '', ''],
wedgeprops=dict(width=0.3),
colors=inner_colors,
labeldistance=0.75, textprops=dict(color='w'));
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)
df.head()
df_ = df.tail(3)
df_.drop('date', axis=1, inplace=True)
df_.set_index('status', inplace=True)
df_ = df_.T
df_ = df_.apply(pd.to_numeric)
df_.drop('tt', inplace=True)
df_.head()
plt.bar(df_.index, df_.Confirmed);
plt.bar(df_.index, df_.Confirmed);
plt.xticks(rotation=90);
plt.bar(df_.index, df_.Confirmed);
plt.bar(df_.index, df_.Recovered, bottom=df_.Confirmed);
plt.xticks(rotation=90);
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);
fig = plt.gcf();
fig.set_size_inches(15, 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);
fig = plt.gcf();
fig.set_size_inches(15, 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);
fig = plt.gcf();
fig.set_size_inches(15, 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");
fig = plt.gcf();
fig.set_size_inches(15, 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 > 1000:
x = i
plt.text(x, y, str(y), ha="center");
df_.head()
df_['Total'] = 0
df_['Total'] = df_.sum(axis = 1)
df_.head()
df_['ConfirmedFraction'] = df_['Confirmed'] / df_['Total']
df_['RecoveredFraction'] = df_['Recovered'] / df_['Total']
df_['DeceasedFraction'] = df_['Deceased'] / df_['Total']
df_.head()
fig = plt.gcf();
fig.set_size_inches(15, 6);
plt.bar(df_.index, df_.ConfirmedFraction, color='Orange');
plt.bar(df_.index, df_.RecoveredFraction, bottom=df_.ConfirmedFraction, color='Green');
plt.bar(df_.index, df_.DeceasedFraction, bottom=df_.ConfirmedFraction + df_.RecoveredFraction, color='Red');
plt.xticks(rotation=90);
df_ = df_.sort_values('ConfirmedFraction', ascending=False)
fig = plt.gcf();
fig.set_size_inches(15, 6);
plt.bar(df_.index, df_.ConfirmedFraction, color='Orange');
plt.bar(df_.index, df_.RecoveredFraction, bottom=df_.ConfirmedFraction, color='Green');
plt.bar(df_.index, df_.DeceasedFraction, bottom=df_.ConfirmedFraction + df_.RecoveredFraction, color='Red');
plt.xticks(rotation=90);
df_ = df_.sort_values('Total', ascending=False)
fig = plt.gcf();
fig.set_size_inches(15, 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 > 1000:
x = i
plt.text(x, y, str(y), ha="center");
df_ = df_.sort_values('Total', ascending=False)
fig = plt.gcf();
fig.set_size_inches(15, 6);
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);
df.head()
df_ = df[['mh', 'date', 'status']]
df_.head()
df_['mh'] = pd.to_numeric(df_['mh'])
df_['date'] = pd.to_datetime(df_['date'])
df_.head()
date | confirmed | recoverd | deceased |
2020-03-14 | 14 | 0 | 0
2020-03-14 | 18 | 0 | 0
pivot
df_ = df_.pivot_table(values="mh", columns="status", index="date")
df_.head()
df_.plot.area();
plt.stackplot(df_.index, df_.Confirmed, df_.Recovered, df_.Deceased);
fig = plt.gcf();
fig.set_size_inches(15, 6);
plt.stackplot(df_.index, df_.Confirmed, df_.Recovered, df_.Deceased);
fig = plt.gcf();
fig.set_size_inches(15, 6);
plt.stackplot(df_.index, df_.Confirmed, df_.Recovered, df_.Deceased,
colors=['orange', 'green', 'red']);
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']);
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();
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();
def plot_stacked_area_by_state(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();
plot_stacked_area_by_state('tn')
plot_stacked_area_by_state('wb')