The Waffle library is a Python package used for creating waffle charts—a grid-based visualization technique to represent parts of a whole. It is particularly useful for visualizing percentages or proportions in a visually engaging way.
Installation:
pip install pywaffle
Basic Usage
Waffle charts are created using the PyWaffle library in combination with matplotlib. Below is an example of how to create a simple waffle chart.
Key Features:
- Grid Customization: Specify the number of rows and columns to adjust the grid size.
- Labels and Legends: Easily add category labels and legends to the chart.
- Theming: Integrates with
matplotlibfor seamless style customization.
code:
from pywaffle import Waffle
import matplotlib.pyplot as plt
# Data to visualize
data = {‘Category A’: 50, ‘Category B’: 30, ‘Category C’: 20}
# Create waffle chart
fig = plt.figure(
FigureClass=Waffle,
plots={
111: {
‘values’: data,
‘labels’: [“{} ({})”.format(k, v) for k, v in data.items()],
‘legend’: {‘loc’: ‘upper left’, ‘bbox_to_anchor’: (1, 1)},
‘title’: {‘label’: ‘Waffle Chart Example’, ‘loc’: ‘center’}
},
},
rows=10, # Number of rows in the waffle chart
figsize=(8, 5)
)
plt.show()
Advanced Usage:
You can create more complex waffle charts with grouped categories, multiple subplots, and more advanced styles. Below is an example:
code:
from pywaffle import Waffle
import matplotlib.pyplot as plt
data = {
‘Group 1’: 40,
‘Group 2’: 35,
‘Group 3’: 25
}
fig = plt.figure(
FigureClass=Waffle,
plots={
111: {
‘values’: data,
‘colors’: [‘#FF5733’, ‘#33FF57’, ‘#3357FF’],
‘labels’: [f”{k} ({v}%)” for k, v in data.items()],
‘legend’: {‘loc’: ‘upper left’, ‘bbox_to_anchor’: (1, 1)},
},
},
rows=5,
figsize=(10, 5)
)
plt.show()