Visualization - Waffle Chart
IBM Data Science Specialization: Waffle Chart
%%capture
!pip3 install xlrd
!pip3 install pywaffle
from pywaffle import Waffle
import matplotlib.pyplot as plt
import pandas as pd
df_can = pd.read_excel(
'./data/ibm/canada.xlsx',
sheet_name='Canada by Citizenship',
skiprows=range(20),
skipfooter=2
)
df_can.columns = list(map(lambda x: str(x), df_can.columns))
drops = [
'AREA',
'REG',
'DEV',
'Type',
'Coverage'
]
df_can.drop(columns=drops, inplace=True)
columns = {
'OdName': 'Country',
'AreaName': 'Continent',
'RegName': 'Region'
}
df_can.rename(columns=columns, inplace=True)
df_can.set_index('Country', inplace=True)
df_can['Total'] = df_can.sum(axis=1)
df_dsn = df_can.loc[['Denmark', 'Norway', 'Sweden'], 'Total'].reset_index()
totals = dict(zip(df_dsn.Country, df_dsn.Total / 100))
fig = plt.figure(
FigureClass=Waffle,
rows=10,
legend={'loc': 'upper left', 'bbox_to_anchor': (1, 1)},
values=totals,
figsize=(9, 5)
)
plt.show()
fig = plt.figure(
FigureClass=Waffle,
rows=10,
legend={'loc': 'upper left', 'bbox_to_anchor': (1, 1)},
values=totals,
icons='child',
icon_legend=True,
figsize=(9, 5)
)
plt.show()
In the charts as above, 1 block = 100 individuals.