Waffle Chart using pyWaffle in Python
In this tutorial, we will learn to create Waffle Chart using the PyWaffle library in Python.
Much like waffles are a delightful treat with a distinctive grid pattern, a Waffle chart also presents data attractively with a grid structure. Each little cell of the grid represents a category or a part of data. What makes it even more engaging is- they are colored. This makes the data so much easier to understand particularly for non-technical audiences.
Let us understand Waffle Chart with an example.
During a presentation by the cafeteria’s sales team, they showcased their top-selling drinks using a diagram with 100 small colored squares that depicted the sales of all drinks. Each square represented a drink category and 1% of the total sales. For instance, if the color brown was used to represent coffee and 20 squares were colored brown, it would indicate that 20% of the sales made were for coffee.
Now let us code this example in Python.
The PyWaffle library in Python
Waffle charts can be plotted using an open-source Python package, PyWaffle. For official documentation, click here.
Pre-requisites
To proceed, make sure you have Python 3.5+ and Matplotlib installed.
Installation
To install, simply use the command
pip install pywaffle
Importing libraries
Next, import the necessary libraries
import matplotlib.pyplot as plt from pywaffle import Waffle
Plotting the Waffle Chart
Now let us plot a basic 10-row, 10-column Waffle Chart.
data = {'Coffee': 20, 'Soda': 15, 'Juice': 50, 'Tea': 15} fig = plt.figure( FigureClass=Waffle, rows=10, columns=10, values=data, colors=["#d11141", "#00aedb", "#00b159", "#f37735"], title={'label': 'Demo Sales', 'loc': 'left', 'fontdict': {'fontsize': 20}}, labels=[f"{k} ({v}%)" for k, v in data.items()], legend={'loc': 'upper left', 'bbox_to_anchor': (1, 1)}, ) plt.show()
The data dictionary contains the sales percentages for each drink. These are used in the values parameter while plotting the chart. The number of rows times the number of columns (rows x columns) should match the sum of values in the data dictionary. In cases where they do not match, value scaling or auto-sizing is applied.
For colors, we have used a list of bright colors. It is important to note that the length of the list or tuple of colors should be equal to the length of the values. Additionally, a title and legend are included for clarity. The label for legend loops through each item in the dictionary and formats them.
The output looks like this-
What is Value Scaling and Auto-sizing?
To fit the values in the Chart, we apply a rounding rule that adjusts all values according to the rule. There are three rounding rules: ceil, floor, and nearest. The floor is the preferred rule as it prevents block overflowing.
Note: Whenever the sum of values exceeds the chart size, blocks of the last group may not show in the chart. This is known as block overflowing and usually occurs with ceil and nearest rounding rules.
For Auto-sizing, remove either the rows or columns property. The total number of blocks would then be equal to the sum of the values.
Playing with Waffle Chart
Now that we have successfully created our basic Waffle Chart, let us advance our learning.
Block Spacing and Arrangement
To adjust the spacing between blocks, we use the interval_ratio_x
and interval_ratio_y
parameters. These are the ratios of the height or width of the blocks to the spacing between them.
We can change the direction of the plotting by setting the vertical parameter to true. Additionally, we can specify the starting location of the chart by choosing one of four corners: NW, SW, NE, or SE. It is also possible to customize the arrangement style for each category by using the block_arranging_style
parameter.
plt.figure( FigureClass=Waffle, columns=10, values=[15, 19, 14, 21], interval_ratio_x=1, #adjusted spacing between blocks interval_ratio_y=0.5, starting_location='SE', #chose starting location of the chart block_arranging_style='new-line', #arrangement style vertical=True ) plt.show()
We utilized the new-line arrangement style which forces each category to begin on a new line. Alternatively, the snake arrangement style arranges the chart in a snake-like pattern, starting from the lower left corner and moving up to the top left before continuing on to the first block of the second column and so forth.
Note: In order to use the new-line arrangement, you can either pass only the column and set the vertical parameter to true, or pass only the row and set the vertical parameter to false.
Chart Design
Colors play an important role in our visual perception of things. In our basic example, we manually assigned colours to each category. However, we can also use a color palette by using the cmap_name
parameter. There are various color maps available, including Set1, Set2, Set3, Pastel1, Pastel2, tab10, tab20, tab20b, tab20c, Paired, Accent, and Dark2.
To modify the chart’s background color, we can utilize the facecolor parameter.
You can change the shape of the blocks using the block_aspect_ratio
parameter. But if you find simple blocks monotonous, Pywaffle also accepts Unicode characters. To do this, use the characters parameter.
Additionally, we can enhance the chart’s look with the help of the methods present in its parent class. In the example below, we used the add_artist
and text methods to add additional objects to the chart.
fig = plt.figure( FigureClass=Waffle, rows=10, columns=10, values=[50, 26, 24], characters="\U00002615", #Unicode character cmap_name="Dark2", #colormap facecolor="black", #background colour ) #adding object rect = plt.Rectangle((0.07, 0.15), 0.55, 0.74, fill=False, ec="white") fig.add_artist(rect, clip=False) #adding text fig.text( x=0.07, y=0.35, s="Codespeedy", rotation=20, fontsize=40, color='grey', alpha=0.5 ) plt.show()
Plotting Chart with Dataset
There may be instances where you may need to create a chart from a given data. Let’s try an example. We will use a vega dataset to visualize the count of disasters across the years 1900 to 2017. To start, import the data and generate a dictionary with the count of each disaster for our Waffle Chart.
disasters = pd.read_csv('https://raw.githubusercontent.com/vega/vega-datasets/main/data/disasters.csv') disaster_count = dict(disasters['Entity'].value_counts())
Next, plot the graph using the count dictionary as values.
plt.figure( FigureClass=Waffle, figsize=(10, 5), rows=20, values=disaster_count, labels=[f"{k} ({v})" for k, v in disaster_count.items()], legend={'loc': 'upper left', 'bbox_to_anchor': (1, 1)} ) plt.show()
Subplots
At times, we may need to compare various sets of data. In such cases, it’s necessary to create multiple Waffle Charts and display them on a single figure. For instance, let’s consider the same example of sales of drinks at a cafeteria chain. But this time having branches at different locations.
To begin, we create a dataframe.
data = pd.DataFrame( { 'labels': ['Coffee', 'Soda', 'Juice', 'Tea'], 'Seattle': [100, 50, 12, 30], 'New York': [150, 56, 20, 45], 'Chicago': [95, 77, 56, 12], }, ).set_index('labels')
In order to merge multiple plots, we need to create a dictionary of all the subplots and then pass it in the plots parameter. Each subplot is assigned a key in the dictionary which indicates the number of rows, the number of columns, and the index of the dictionary. Here, the numbers 311, 312, and 313 are the keys of the dictionary.
plt.figure( FigureClass=Waffle, plots={ 311: { 'values': data['Seattle'], 'labels': [f"{k} ({v})" for k, v in data['Seattle'].items()], 'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.05, 1), 'fontsize': 8}, 'title': {'label': 'Sales in Seattle', 'loc': 'left', 'fontsize': 12} }, 312: { 'values': data['New York'], 'labels': [f"{k} ({v})" for k, v in data['New York'].items()], 'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.2, 1), 'fontsize': 8}, 'title': {'label': 'Sales in New York', 'loc': 'left', 'fontsize': 12} }, 313: { 'values': data['Chicago'], 'labels': [f"{k} ({v})" for k, v in data['Chicago'].items()], 'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.3, 1), 'fontsize': 8}, 'title': {'label': 'Sales in Chicago', 'loc': 'left', 'fontsize': 12} }, }, rows=10 ) plt.show()
Conclusion
Waffle charts are often used in place of a pie chart since the brain tends to comprehend waffle charts better. They provide a clear overview of the data at a glance. They are also frequently used to track progress towards a goal. This article provided a tutorial on creating a basic Waffle chart and ways to customize it. Now you can try out other examples and create visually appealing charts. Happy Learning!
Leave a Reply