Generate Waffle chart using pyWaffle in Python

Generate Waffle chart using pyWaffle in Python

To generate a Waffle Chart in Python, you can use the pywaffle library, which is an extension of Matplotlib. The Waffle Chart is useful for displaying progress towards goals or the composition of something.

First, if you haven't installed the pywaffle package yet, you need to do so using pip:

pip install pywaffle 

After installation, you can create a Waffle Chart like so:

import matplotlib.pyplot as plt from pywaffle import Waffle # Example data data = {'Category A': 30, 'Category B': 20, 'Category C': 50} fig = plt.figure( FigureClass=Waffle, rows=5, # Number of rows values=data, colors=["#232066", "#983D3D", "#DCB732"], # Optional colors for each category title={'label': 'My Waffle Chart', 'loc': 'center'}, # Optional title labels=["{0} ({1})".format(k, v) for k, v in data.items()], # Optional labels legend={'loc': 'lower left', 'bbox_to_anchor': (0, -0.4), 'ncol': len(data), 'framealpha': 0} ) plt.show() 

This script will display a Waffle Chart with 5 rows and the number of columns will be determined automatically to fit the proportions of the values in data. The colors parameter is optional and allows you to specify the color of each category in the chart. The labels parameter is also optional and can be used to display the key and value pairs from the data dictionary on the chart. The legend is positioned below the chart using the legend parameter.

Make sure that the sum of values in data is a multiple of the product of rows and the columns (which is automatically inferred if not provided), otherwise pywaffle will throw an error because it cannot accurately represent the data.


More Tags

tsc stacked-chart procedure windows-server-2012 inline playlist roguelike uibutton xls ubuntu-15.10

More Programming Guides

Other Guides

More Programming Examples