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…
- Create a new Python file, and import the following packages:
import numpy as np import matplotlib.pyplot as plt
- Define the labels and values:
# Labels and corresponding values in counter clockwise direction data = {'Apple': 26, 'Mango': 17, 'Pineapple': 21, 'Banana': 29, 'Strawberry': 11}
- Define the colors for visualization:
# List of corresponding colors colors = ['orange', 'lightgreen', 'lightblue', 'gold', 'cyan']
- 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 a section explode = (0, 0, 0, 0, 0)
- Plot the pie chart. Note that if you use Python 3,...