Now that you have made a bar chart and a histogram, let's try making another simple statistical plot. A pie chart! As you are aware, pie charts are a simple way of showing the relative sizes of data in a given set. Pie charts are easy to make in matplotlib, and only require one function. Most functions in matplotlib use similar, if not identical, arguments to do certain things, like 'color' in the bar chart example. This can be used to decipher which arguments a function might take!
Although there are no examples of pie charts in this page, it is relatively simple and can be found in the matplotlib docs here. The function is matplotlib.pylot.pie(...)
Use the data below and:
BONUS 7. 'explode' the small wedge, and give it shadow.
HINT
Start simple, and build up. REMEMBER: The online docs will give the function with ALL its possible arguments. However only those without an equals sign (e.g. plt.xxx(X,label=...)) are mandatory (X in this case). Those with an equals sign (label) are not required for the function to work, but allow for more customisation should you wish it.
Most of the tricks you will use here are applicable on other plots and figures throughout this course, do not forget them as they can make your life easier later.
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
# This is your data to plot
data = np.array([['Pacman','not Pacman'],[270, 90]])
CATEGORIES = data[0]
SIZES = data[1]
# Separate the data into two arrays, one of the sizes and one of the categories
# I use capitals to distinguish them from the function's argument labels
COLORS = ['y','w']
# 'COLORS' is the list of colours we provide and 'colors' is the argument in the
# function. Our colours here are yellow 'y' and white 'w'.
EXPLODE = (0,0.1)
# Part of the bonus answer, EXPLODE is a tuple with each element denoting how
# 'Exploded' the corresponding segment will be.
plt.figure()
# Start the figure. This is not strictly necessary in ipython notebooks, but is
# a good habit to adopt as it is useful when differentiating between multiple
# figures in the same code.
plt.pie(SIZES, labels = CATEGORIES, colors = COLORS, startangle = 45,
explode = EXPLODE, shadow = True)
# Pie chart function, fist argument contains the values and is the only
# mandatory one. All the rest have defaults. Set the labels to be our
# CATEGORIES, colors to be our COLORS, the startangle to be 45 degrees
# anti-clockwise, explode to be the EXPLODE tuple and shadow to True.
plt.title('Fraction of a pie chart that looks like Pacman')
# Add a title explaining the plot
plt.axes().set_aspect('equal')
# Make the aspect ratio equal. I pinched this line of code from the
# aspect ratio demo script in the matplotlib docs.
plt.show()
# Show the plot. Again, this is not strictly necessary in ipython notebook
# but is a good practice to get into.