Getting set up

Installing pyWaffle

We’ll be using the pyWaffle library, which you can install with pip.

pip install pywaffle

Importing into your Jupyter Notebook

You’ll need to import all of these to make it work!

import pandas as pd
import matplotlib.pyplot as plt
from pywaffle import Waffle

Type 1: Using columns for drawing

Sometimes you’ll just want to use columns right out of your dataframe.

df = pd.DataFrame({
    'crime_type': ['felony', 'misdemeanor', 'violation'],
    'number_of_cases': [54, 12, 8]
})
df
crime_type number_of_cases
0 felony 54
1 misdemeanor 12
2 violation 8
fig = plt.figure(
    FigureClass=Waffle, 
    rows=5,
    values=df.number_of_cases,
    labels=list(df.crime_type)
)
/Users/jonathansoma/.pyenv/versions/3.6.5/lib/python3.6/site-packages/matplotlib/figure.py:2267: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
  warnings.warn("This figure includes Axes that are not compatible "

png

Type 2: Using pandas calculations for a waffle chart

First we’ll read in/create our dataset…

df = pd.DataFrame({
    'District': ['District 12', 'District 23', 'District 32', 'District 45', 'District 65', 'District 67', 'District 33'],
    'party': ['Republican', 'Republican', 'Republican', 'Republican', 'Democrat', 'Democrat', 'Democrat']
})
df
District party
0 District 12 Republican
1 District 23 Republican
2 District 32 Republican
3 District 45 Republican
4 District 65 Democrat
5 District 67 Democrat
6 District 33 Democrat

We can’t use this raw data for our waffle chart, because we need a list of numbers for it! In order to chart the number of Democrats and Republicans, we need to first get a value counts…

calculated = df.party.value_counts()
calculated
Republican    4
Democrat      3
Name: party, dtype: int64

…then you’ll feed the index and values into your waffle chart separately. The index (on the left) will be your labels, the values (on the right) will be the number of each square to display. If you don’t use list() with the labels part it won’t work.

fig = plt.figure(
    FigureClass=Waffle, 
    rows=2, 
    values=list(calculated.values),
    labels=list(calculated.index)
)
/Users/jonathansoma/.pyenv/versions/3.6.5/lib/python3.6/site-packages/matplotlib/figure.py:2267: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
  warnings.warn("This figure includes Axes that are not compatible "

png

Hot tips: Customizing your graphic

You can use figsize=, legend=, and other options, too.

df = pd.DataFrame({
    'crime_type': ['felony', 'misdemeanor', 'violation'],
    'number_of_cases': [54, 12, 8]
})
df
crime_type number_of_cases
0 felony 54
1 misdemeanor 12
2 violation 8
fig = plt.figure(
    FigureClass=Waffle, 
    rows=7,
    values=df.number_of_cases,
    labels=list(df.crime_type),
    figsize=(10, 5),
    legend={'loc': 'upper left', 'bbox_to_anchor': (1.1, 1)}
)
/Users/jonathansoma/.pyenv/versions/3.6.5/lib/python3.6/site-packages/matplotlib/figure.py:2267: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
  warnings.warn("This figure includes Axes that are not compatible "

png