How to Build Waffle Charts in Python

How to Build Waffle Charts in Python

Tools to be used

For this tutorial, we will use:

  1. Pandas Library
  2. Matplotlib Library
  3. Jupyter Notebook environment.

Develop a Waffle Chart Without Waffle Package

  1. Import the necessary packages:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
df = pd.DataFrame({
'country': ['Argentina', 'Brazil', 'Cuba', 'Peru'],
'number': [212, 334, 1500, 130]
})
Image for post
total = sum(df['number'])
proportions = [(float(value) / total) for value in df['number']]#Output:
[0.0974264705882353,
0.15349264705882354,
0.6893382352941176,
0.05974264705882353]
width = 40
height=10
total= width * height
tiles_per_category = [round(proportion * total) for proportion in proportions]#Output:
[39, 61, 276, 24]
waffle = np.zeros((height, width))
category_index = 0
tile_index = 0
for col in range(width):
for row in range(height):
tile_index += 1
if tile_index > sum(tiles_per_category[0:category_index]):
category_index += 1
waffle[row, col] = category_index
fig = plt.figure()
colormap = plt.cm.coolwarm
plt.matshow(waffle, cmap=colormap)
plt.colorbar()
Image for post
fig = plt.figure()
colormap = plt.cm.coolwarm
plt.matshow(waffle, cmap=colormap)
ax = plt.gca()
ax.set_xticks(np.arange(-0.5, (width), 1), minor=True)
ax.set_yticks(np.arange(-0.5, (height), 1), minor=True)
ax.grid(which='minor', color='w', linestyle='-', linewidth=2)plt.xticks([])
plt.yticks([])
plt.colorbar()
Image for post
fig = plt.figure()
colormap = plt.cm.coolwarm
plt.matshow(waffle, cmap=colormap)
ax = plt.gca()
ax.set_xticks(np.arange(-0.5, (width), 1), minor=True)
ax.set_yticks(np.arange(-0.5, (height), 1), minor=True)
ax.grid(which='minor', color='w', linestyle='-', linewidth=2)plt.xticks([])
plt.yticks([])
values = df['number']
categories = df['country']
value_sign = ''
values_cumsum = np.cumsum(values)
total_values = values_cumsum[len(values_cumsum) - 1]
legend_handles = []
for i, category in enumerate(categories):
if value_sign == '%':
label_str = category + ' (' + str(values[i]) + value_sign + ')'
else:
label_str = category + ' (' + value_sign + str(values[i]) + ')'
color_val = colormap(float(values_cumsum[i]) / total_values)
legend_handles.append(mpatches.Patch(color=color_val, label=label_str))

plt.legend(handles=legend_handles, loc = ‘lower center’, ncol=len(categories),
bbox_to_anchor=(0., 0.2, 0.95, 0.1)) #positioning legends
plt.colorbar()

Image for post

Develop Waffle Chart Using Waffle Class

You need to install the pywaffle package using this command

pip install pywaffle
from pywaffle import Waffle
fig = plt.figure(
FigureClass=Waffle,
rows=10,
values=list(df.number/5),
labels=list(df.country),
figsize=(12, 8),
legend={'bbox_to_anchor': (0.5, 0.5)}
)
Image for post

 

#matplotlib #python #DataVisualization #wafflecharts #DataScience 

Leave a Reply

Close Menu