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.
import numpy as np import matplotlib.pyplot as plt
# Labels and corresponding values in counter clockwise direction data = {'Apple': 26, 'Mango': 17, 'Pineapple': 21, 'Banana': 29, 'Strawberry': 11}
# List of corresponding colors colors = ['orange', 'lightgreen', 'lightblue', 'gold', 'cyan']
0
:# Needed if we want to highlight a section explode = (0, 0, 0, 0, 0)
list(data.values())
in the following function call:# Plot the pie chart plt.pie(data.values(), explode=explode, labels=data.keys(), colors=colors, autopct='%1.1f%%', shadow=False, startangle=90) # Aspect ratio of the pie chart, 'equal' indicates tht we # want it to be a circle plt.axis('equal') plt.show()
pie_chart.py
file that's already provided to you. If you run this code, you will see the following figure:If you change the explode array to (0, 0.2, 0, 0, 0
), then it will highlight the Strawberry section. You will see the following figure:
18.118.140.108