Drawing pie charts

Let's see how to plot pie charts. This is useful when you want to visualize the percentages of a set of labels in a group.

How to do it…

  1. Create a new Python file, and import the following packages:
    import numpy as np
    import matplotlib.pyplot as plt 
  2. Define the labels and values:
    # Labels and corresponding values in counter clockwise direction
    data = {'Apple': 26, 
            'Mango': 17,
            'Pineapple': 21, 
            'Banana': 29, 
            'Strawberry': 11}
  3. Define the colors for visualization:
    # List of corresponding colors
    colors = ['orange', 'lightgreen', 'lightblue', 'gold', 'cyan']
  4. Define a variable to highlight a section of the pie chart by separating it from the rest. If you don't want to highlight any section, set all the values to 0:
    # Needed if we want to highlight ...

Get Python Machine Learning Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.