Creating a basic pie chart using Matplotlib | by Mubarak Ganiyu | Towards Data Science

Pie charts are used to present categorical data in a format that highlights how each data point contributes to a whole, that is 100%. The chart is shaped in a circular form like a pie and each data point is represented by a certain percentage while taking a part of the pie that is shaped like a slice. The bigger its slice in the pie, the more proportion of the pie the datapoint has.
The dataset was gotten from the English premier league website and scraped to include the top 15 highest goal scorers in the English premier league. Then, they were manipulated to find out how the premier league teams are represented in this table.
After obtaining the dataset, the next step would be to import the data visualization package for analysis.
The data visualization package that was imported to build pie charts is called Matplotlib and it was imported using the sets of codes below:
## Import data visualization packagesimport matplotlib.pyplot as plt%matplotlib inline
The goal is to build a pie chart representing the top five teams that have produced the most goals among the top 15 highest goal scorers in the premier league. Firstly, a very basic pie chart will be built. The pie chart is made using the following codes below:
labels = df_score_2.indexcolors = ['lightskyblue', 'red', 'blue', 'green', 'gold']plt.pie(df_score_2['Stat'], labels= labels, colors=colors, startangle=90, autopct='%.1f%%')plt.show()
The labels = df_score_2.index was used to store the index of the table as labels for the pie chart. The labels are the names of the five teams. The colors = [‘lightskyblue’, ‘red’, ‘blue’, ‘green’, ‘gold’] will be used to represent each slice of the five premier league teams in the pie chart as their colors. The plt.pie(df_score_2[‘Stat’], labels= labels, colors=colors, startangle=90, autopct=’%.1f%%’) shall be used to create a pie chart. The df_score_2[‘Stat’] contains the numerical values of each team which shall be divided by the total and multiplied by 100. The sizes of their slices are relative to their proportion. The labels = labels is the label for each slice in the pie chart. The colors = colors is for highlighting the color of each slice. The startangle=90 is to represent the angle from which the pie chart start getting built. The autopct=’%.1f%%’ is for formatting how the percentages appear on the pie chart. Finally, plt.show() is used to display the pie chart.
The pie chart above is a draft pie chart which can be improved by adding more codes. Assuming Liverpool was to play Tottenham hotspur and both teams want to be made more visually prominent. This can be done using a set of codes written below:
## for exploding the pie chart labels = df_score_2.index colors = ['lightskyblue', 'red', 'blue', 'green', 'gold']explode =(0,0.1,0,0.1,0) plt.pie(df_score_2['Stat'], explode = explode,labels= labels, colors=colors, startangle=90, autopct='%.1f%%') plt.show()
Nothing really changed in the code above except for the fact that a new line of code, explode =(0,0.1,0,0.1,0), was added. Adding this line of code and specifically signifying that the second and fourth member of the bracketed values are 0.1 will cut out Liverpool and Tottenham Hotspur from the pie. Then, a new command, explode = explode, is added to the plt.pie function.
The pie chart can be further improved by including a title for more details and adding shadows to make it look more solid using the codes below.
## Adding a title to the pie chart and a shadow to the chart labels = df_score_2.index colors = ['lightskyblue', 'red', 'blue', 'green', 'gold'] explode =(0,0.1,0,0.1,0)plt.pie(df_score_2['Stat'], explode = explode,labels= labels, colors=colors, startangle=90, autopct='%.1f%%', shadow = True) plt.title("Top five teams with the most goals among the top 15 highest goal scorers \nin the premier league 2019/20 mid-season", fontsize = 18) plt.show()
The shadow = True command was added to the function plt.pie to make the pie chart look more elegant. The plt.title(“Top five teams with the most goals among the top 15 highest goal scorers \nin the premier league 2019/20 mid-season”, fontsize = 18) code was also used to add a title to the pie chart.
More improvements can be made to the pie chart to make it more structured and presentable. A legend can be added, the pie chart can be enlarged and the structure of the picture can be enhanced.
## Adding a legend to the chart and increasing its size labels = df_score_2.index colors = [‘lightskyblue’, ‘red’, ‘blue’, ‘green’, ‘gold’] explode =(0,0.1,0,0.1,0) fig, ax1 = plt.subplots(figsize = (24,12)) ax1.pie(df_score_2[‘Stat’], explode = explode, colors=colors, startangle=90, autopct=’%.1f%%’, shadow = True) plt.title(“Top five teams with the most goals among the top 15 highest goal scorers \nin the premier league 2019/20 mid-season”, fontsize = 24) ax1.legend(labels, loc = “upper right”) plt.tight_layout() plt.show()
The new additions include adding fig, ax1 = plt.subplots(figsize = (24,12)) which is the code that is used to enlarge the pie chart while making the pie chart’s plotting function assigned to ax1. The plt.pie becomes ax1.pie. Then, the ax1.legend(labels, loc = “upper right”) is used to add a legend to the pie chart with the labels as the legends and the legend located in the upper right corner of the figure. plt.tight_layout() is used to fix the pie chart better into the figure.
The pie chart above is the beautiful presentable form of the pie chart and an example of how to put the pie in pie chart.
Pie charts are very useful in categorizing data that add up to a total value in order to show how much each category in the pie. For example, racial representation on a college campus or vehicle brand ownership in a neighbourhood.