How to create a waffle chart in Python

Waffle chart

A waffle chart is an alternative to a pie chart. It is used to visualize progress towards a goal, percentage of task completion or profit, or part-to-whole relationship. The waffle chart is also known by the name Grid Plot.

A waffle chart is represented by a square or rectangular grid of cells, each corresponding to 11 unit or 1%1\%. The cells are represented with colors to differentiate between categories. A waffle chart can include multiple categories. Different waffle charts can also be combined as subplots to show relationships among different datasets.

Here’s an example of a sample waffle chart organized as a grid of 55x1010 cells.

A simple waffle chart
A simple waffle chart

Waffle charts in Python

Python uses PyWaffle, an open-source package based on matplotlib, to create waffle charts. The figure constructor takes rows, columns, and data as parameters.

The following code creates a waffle chart corresponding to the simple waffle chart depicted above.

import matplotlib.pyplot as plt
from pywaffle import Waffle
fig = plt.figure(
FigureClass=Waffle,
rows=5,
columns=10,
values=[30, 16, 4],
title = {"label": "Sample Waffle chart", "loc": "Center", "size": 15}
)

Here’s an explanation of the code above.

  • Lines 1-2: We import the relevant libraries.
  • Line 4: We initialize the figure constructor (matplotlib.pyplot.figure) of the type Waffle to create the waffle chart.
  • Line 5: We set FigureClass to Waffle. It describes the type of the chart.
  • Lines 6-7: We define the number of rows and columns. If only one of them is given, then a default waffle chart of 5050 cells is created.
  • Line 8: The parameter values takes the values to be plotted on the chart, it could be a list or a DataFrame. The values in the list represent the categories in the code.

Waffle Chart Customization

We can customize the waffle chart to add legends, titles, and labels. We can also customize colormaps, background color, icon types, and orientation of plotting in a waffle chart.

A relevant example is shown below where a small data has been represented in a customized manner.

import matplotlib.pyplot as plt
from pywaffle import Waffle
fig = plt.figure(
FigureClass=Waffle,
rows=10,
columns=10,
values={'Infant': 20, 'Toddler': 16, 'Preschool': 17},
legend={'loc': 'upper left', 'bbox_to_anchor': (1, 1)},
cmap_name = 'Set2',
facecolor = 'whitesmoke',
title = {"label": "Age distribution at daycare", "loc": "Center", "size": 15},
icons = 'child',
vertical=False
)
  • Line 8: The argument values represents the dataset.
  • Line 9: The argument legend describes the legend of the chart.
  • Line 10: The argument cmap_name specifies the colormap for the chart.
  • Line 11: The argument facecolor refers to the background color of the chart.
  • Line 12: The argument title describes the title of the chart.
  • Line 13: The argument icons refers to the icon type of the cells.
  • Line 14: The argument vertical states the direction of plotting. Waffle chart, by default, plots data column-wise, but setting the argument vertical to true will plot the data row-wise.

The output of this code is given below.

A customized waffle chart
A customized waffle chart

Merits and limitations

Compared to pie charts, waffle charts are aesthetically pleasing, straightforward, quantitatively detailed, and provide an easier at-a-glance understanding. Waffle charts are used for flattening datasets that add up to 100%100\%, where each cell represents one unit or one percent instead of an angle or area, which is usually the case for pie charts.

In contrast, the downside to the easier interpretation of waffle charts is that the inclusion of too many cells tends to make it complicated. And the lack of spaces between the cells of the grid does not allow individualized labeling of cells, which makes it unsuited for complicated data visualizations.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved