# 3.10 Waffle Chart

A waffle chart shows progress towards a target or a completion percentage. There is a grid of small cells, of which colored cells represent the data.

A chart can consist of one category or several categories. Multiple waffle charts can be put together to show a comparison between different charts.

We can use [PyWaffle](http://github.com/gyli/PyWaffle) to plot fancy waffle charts.

#### Installation

```
pip install pywaffle
```

#### Example

Let's create a dictionary (simple key-value structure) that includes randomly German Parties information.

```
# This is randomly made up! 
data = {'CDU': 45,'SPD': 34,'Die Linke': 10,'Die Grünen': 7, 'others': 4}
```

```
import matplotlib.pyplot as plt
from pywaffle import Waffle
```

```
fig = plt.figure(figsize = (14,6),
            FigureClass=Waffle,
            rows = 5,
            values = data,
            colors = ('#EC2272','yellow','#5ac9ff','#76ecbe','#fd5f00'),
            title ={'label': 'Politische Parteien in Deutschland','loc':'left'},
            labels = ["{0}({1}%)".format(k,v) for k,v in data.items()],
            legend = {'loc': 'lower left', 'bbox_to_anchor': (0, -0.4), 'fontsize': 14, 'ncol': len(data)},
            starting_location = 'NW')
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MCaTCPhTZFy1RwYqsjc%2F-MCajKONisNHYXVZaAA5%2Fdownload-1.png?alt=media\&token=7dc376c6-538d-43da-a709-ad15dbfea3a3)

Also, we can add a`icon`parameter to make it more attractive.

```
fig = plt.figure(figsize = (14,6),
            FigureClass=Waffle,
            rows = 5,
            values = data,
            colors = ('#EC2272','yellow','#5ac9ff','#76ecbe','#fd5f00'),
            title ={'label': 'Politische Parteien in Deutschland','loc':'left'},
            labels = ["{0}({1}%)".format(k,v) for k,v in data.items()],
            legend = {'loc': 'upper left', 'bbox_to_anchor': (1.05, 1), 'fontsize': 14},
            icons = 'child',
            icon_legend = True)
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MCaTCPhTZFy1RwYqsjc%2F-MCajf7OXY1FxgYnPfqE%2Fdownload-2.png?alt=media\&token=6bb26151-bce2-4416-bc06-aff8fec163ef)

The above is showing how to draw a waffle chart with a `Dictionary`. It's also possible to draw with a`DataFrame`. Here is an example, let's use the embedded dataset called "mpg".

```
import seaborn as sns
mpg = sns.load_dataset('mpg')
```

![Head of dataset "mpg"](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MCaTCPhTZFy1RwYqsjc%2F-MCakTT3UGIDzryrt6Kk%2FScreenshot%202020-07-19%20at%2013.07.17.png?alt=media\&token=bdb95bc5-9004-41ee-9129-a09f78e439f7)

Let's make a graph based on the **"cylinders**" variable. Firstly, we need to group by it and calculate the "counts". Then pass it to the figure.

```
df_cyl = mpg.groupby('cylinders').size().reset_index(name='counts_cyl')

fig = plt.figure(figsize = (14,6),
    FigureClass=Waffle,
    rows = 10,
    values= df_cyl['counts_cyl'],
    labels = ["{1}".format(n[0], n[1]) for n in df_cyl[['cylinders', 'counts_cyl']].itertuples()],
    legend = {'loc': 'upper left', 'bbox_to_anchor': (1.05, 1), 'fontsize': 12, 'title':'cylinders'},
    title = {'label': 'Vehicles by Cyl', 'loc': 'center', 'fontsize':18},
    colors = ('#EC2272','yellow','#5ac9ff','#76ecbe','#fd5f00'))
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MCaTCPhTZFy1RwYqsjc%2F-MCakghj94lamuRZgZ4a%2Fdownload.png?alt=media\&token=f98a50c5-e9f7-4d2f-a9dc-21f218ebadf2)

Similarly, we can make a graph based on the "**origin**" variable.

```
df_origin = mpg.groupby('origin').size().reset_index(name='counts_origin')
n_categories = df_origin.shape[0]

fig = plt.figure(figsize = (14,6),
    FigureClass=Waffle,
    rows = 10,
    values= df_origin['counts_origin'],
    labels = ["{1}".format(n[0], n[1]) for n in df_origin[['origin', 'counts_origin']].itertuples()],
    legend = {'loc': 'upper left', 'bbox_to_anchor': (1.05, 1), 'fontsize': 12, 'title':'cylinders'},
    title = {'label': 'Vehicles by origin', 'loc': 'center', 'fontsize':18},
    colors = ('yellow','#5ac9ff','#fd5f00'))
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MCaTCPhTZFy1RwYqsjc%2F-MCalYOT_a8tk-3tTLlS%2Fdownload-1.png?alt=media\&token=42572381-c927-42e8-8c08-276aa69bf516)
