Page 303 - Ai_V3.0_c11_flipbook
P. 303
• ‘games_played’ extracts the column named ‘Games Played’ from the DataFrame, creating a series that contains the
number of games played.
• ‘total_scores’ extracts the column named ‘Total Scores’ from the DataFrame, creating a series that contains the total scores.
• These extracted series (games_played and total_scores) are then used to create a scatter plot.
Pie Chart
A pie chart is a circular graph divided into slices to show the relative proportions of different categories within a
whole. Each slice's size is proportional to the quantity it represents. Pie charts are mostly used to visualise facts from
a small dataset. There should be not more than seven categories in a pie chart. Another limitation of a Pie chart is
that zero values can't be displayed in pie chart. However, these graphs are hard to interpret and compare facts with
another pie chart.
Pie charts are used to examine elements of a whole. They do not display changes over time. For example, pie charts
may be used to suggest the achievement or failure of a product or service, display the time-period allotted to each
subject in a class, or depict monthly spending on numerous items and services in a household. For example:
No. of books read in a month
Story books No. of books read in a month
Self-help books
12%
Fiction 30
Fiction Fiction
35%
Murder Mystery 20 Murder Mystery
Comic Books
29% Comic Books
Comic Books 25 Self-help books
Murder Mystery
24%
Self-help books 10
The function pie() is used to plot a pie chart.
Program 10: Create a Pie chart for different category of books read in a month using Python.
Book Category Fiction Murder Mystery Comic Books Self-help books
Read in a Month 30 20 25 10
import matplotlib.pyplot as plt
# Data
labels = ['Fiction', 'Murder Mystery', 'Comic Books', 'Self-help books']
sizes = [30, 20, 25, 10]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0, 0, 0) # explode the first slice (Fiction)
# Create pie chart using pie() function.
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
autopct='%1.0f%%', shadow=True, startangle=140)
# Add title
plt.title('No. of Books Read in a Month')
Data Literacy—Data Collection to Data Analysis 301

