Pie Charts

In this section we will be looking at Pie charts. Let’s see a small example. Suppose that we want to chart the distribution of active hours for a standard day.

Let’s suppose that 70% of the active hours are taken by work and than the rest of the day is equally split between Programming, Reading and Sport (that is how much I can at the moment).

Let’s have a look at the code below:

import matplotlib.pyplot as plt
l=["Work","Sport","Programming","Reading"]
mytime =[70,10,10,10]
e=(0,0,0.2,0)
fig1,ax1=plt.subplots()
ax1.pie(mytime, explode=e,labels=l,autopct="%1.1f%%",shadow=True,startangle=90)
ax1.axis('equal')
plt.title("Day Distribution")
plt.show()

We have a variable for the Label (l) and a variable with the hours distribution.

The variable “e” is used to indicate MatPlot which slice of the Pie we want to detach from the Pie (in this case we are selecting the Programming element (the third in the list)

The other important part of this small program is the ax1.pie where we insert the :

  • values to plot (mytime variable)
  • we set the explode flag so that we can detach the 3rd element of our list
  • the label (variable l)
  • the format of the data (autopct)
  • shadow paremeter is set to True
  • startangle set to 90

Here is our Pie Chart