How to create pie chart using python?
Today we are going to take a look on how to plot your data in a pie chart. I know there are lots of software out there. But I will focus on using python. To do it, we are going to use a python package called 'matplotlib'. If you are using macOS or Linux, you can get it by the following command-
Looks good?. What if you want to slice you pie chart? You just need to add one line-
pip install matplotlibFor the demonstration, I am going to use the data from this paper. Now, open your favorite IDE and write-
import matplotlib.pyplot as plt
# Data to plot
labels = 'Title I', 'General Fund', 'SIG or ARRA', 'Other disctrict funds', 'Wallace', '21CCLC', 'City funds', 'Other private'
sizes = [51, 9, 6, 9, 17, 4, 2, 2]
colors = ['Grey','gold', 'yellowgreen', 'lightcoral', 'lightskyblue','green','blue','red']
# Plot
plt.pie(sizes, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)
plt.axis('equal')
plt.show()
And this will produce -
explode = (0.1, 0, 0, 0)
Boom. You are done. The full code will look like this-
import matplotlib.pyplot as plt
# Data to plot
labels = 'Title I', 'General Fund', 'SIG or ARRA', 'Other disctrict funds', 'Wallace', '21CCLC', 'City funds', 'Other private'
sizes = [51, 9, 6, 9, 17, 4, 2, 2]
colors = ['Grey','gold', 'yellowgreen', 'lightcoral', 'lightskyblue','green','blue','red']
explode = (0.1, 0, 0, 0) # explode 1st slice
# Plot
plt.pie(sizes, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)
plt.axis('equal')
plt.show()