👀
Crash Visualization
  • Welcome
  • Preface
    • Who the book is written for
    • How the book is organized
  • 1. Introduction of Data Visualization
    • 1.1 What is data visualization?
    • 1.2 Why does visualization matter?
  • 2. Tricks in Visualization
    • 2.1 Choose Appropriate Chart
    • 2.2 Features of Charts
      • 2.2.1 Table
      • 2.2.2 Column Chart
      • 2.2.3 Line Chart
      • 2.2.4 Pie Chart
      • 2.2.5 Scatter Chart
      • 2.2.6 Map Chart
    • 2.3 Misused Graph
    • 2.4 Tips in Visualization
  • 3. Matplotlib
    • 3.1 Basic Concepts
    • 3.2 Line Chart
    • 3.3 Area Chart
    • 3.4 Column Chart
    • 3.5 Histogram Chart
    • 3.6 Scatter Chart
    • 3.7 Lollipop Chart
    • 3.8 Pie Chart
    • 3.9 Venn Chart
    • 3.10 Waffle Chart
    • 3.11 Animation
  • 4. Seaborn
    • 4.1 Trends
    • 4.2 Ranking
      • 4.2.1 Barplot
      • 4.2.2 Boxplot
    • 4.3 Composition
      • 4.3.1 Stacked Chart
    • 4.4 Correlation
      • 4.4.1 Scatter Plot
      • 4.4.2 Linear Relationship
      • 4.4.3 Heatmap
      • 4.4.4 Pairplot
    • 4.5 Distribution
      • 4.5.1 Boxplot
      • 4.5.2 Violin plot
      • 4.5.3 Histogram plot
      • 4.5.4 Density plot
      • 4.5.5 Joint plot
  • 5. Bokeh
    • 5.1 Basic Plotting
    • 5.2 Data Sources
    • 5.3 Annotations
    • 5.4 Categorical Data
    • 5.5 Presentation and Layouts
    • 5.6 Linking and Interactions
    • 5.7 Network Graph
    • 5.8 Widgets
  • 6. Plotly
    • 6.1 Fundamental Concepts
      • 6.1.1 Plotly Express
      • 6.1.2 Plotly Graph Objects
    • 6.2 Advanced Charts
      • 6.2.1 Advanced Scatter Chart
      • 6.2.2 Advanced Bar Chart
      • 6.2.3 Advanced Pie Chart
      • 6.2.4 Advanced Heatmap
      • 6.2.5 Sankey Chart
      • 6.2.6 Tables
    • 6.3 Statistical Charts
      • 6.3.1 Common Statistical Charts
      • 6.3.2 Dendrograms
      • 6.3.3 Radar Chart
      • 6.3.4 Polar Chart
      • 6.3.5 Streamline Chart
    • 6.4 Financial Charts
      • 6.4.1 Funnel Chart
      • 6.4.2 Candlestick Chart
      • 6.4.3 Waterfall Chart
  • Support
    • Donation
Powered by GitBook
On this page

Was this helpful?

  1. 3. Matplotlib

3.10 Waffle Chart

Previous3.9 Venn ChartNext3.11 Animation

Last updated 4 years ago

Was this helpful?

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 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')

Also, we can add aiconparameter 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)

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

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

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'))

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'))
PyWaffle
Head of dataset "mpg"