@extends('template')
@section('title')
Lab 7: Pie chart tips
@stop
@section('content')
# Reference Notes: How to make a pie chart
#### Reference
[matplotlib.pyplot.pie](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.pie)
#### Preface
For all of the examples below, `pyplot` has been imported:
```py
import matplotlib.pyplot as plt
```
## Pie Charts
To create a [pyplot pie chart](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.pie), you'll need two lists:
+ (required) A list of numbers indicating the size of each pie slice.
+ (optional) A list of labels for those slices
For example, if we wanted to chart ice cream preferences, you might have these two lists:
```py
icecreamPreferences = [30,50,10,6] # fyi: does not have to sum to 100
iceCreamLabels = ['Strawberry', 'Chocolate', 'Vanilla','Banana']
```
Those two lists can then be passed to the `pie()` function:
```py
# Setting the figure size is important-
# without it, you'll get a squished pie chart (no one likes squished pie)
plt.figure('Pie Chart Example', figsize=(4,4), facecolor='white')
plt.pie(icecreamPreferences,labels=iceCreamLabels)
plt.show()
```
## Colors
To customize the colors of your slices, add the `colors` parameter when invoking `pie()` and pass it a list of colors:
```py
sliceColors = ['pink', 'chocolate', 'ivory','yellow']
plt.pie(icecreamPreferences,labels=iceCreamLabels,colors=sliceColors)
```
In our ice cream flavor examples, the colors we choose are important because they match the flavors.
In some pie charts though, the color won't matter, just as long as there is a variety. In these cases, you can create a list of colors using [pyplot's colormap](http://scipy.github.io/old-wiki/pages/Cookbook/Matplotlib/Show_colormaps) feature.
Example:
```py
import numpy as np
colormap = plt.cm.Set2
numberOfSlices = len(icecreamPreferences)
sliceColors = colormap(np.linspace(0., 1., numberOfSlices))
# Example resulting sliceColors list:
# [[ 0.40000001 0.76078433 0.64705884 1. ]
# [ 0.67058825 0.59869283 0.78562093 1. ]
# [ 0.88366014 0.84967321 0.23267974 1. ]
# [ 0.7019608 0.7019608 0.7019608 1. ]]
```
## Start angle
By default, your pie chart starts at 0 degrees (red lines added to show degrees):
To customize this angle, utilize the `startangle` parameter when invoking `pie()`:
```py
plt.pie(
icecreamPreferences,
labels=iceCreamLabels,
startangle=90
)
```
## Displaying percentages on slices
Our pie chart is created using these slices:
```py
icecreamPreferences = [30,50,10,6]
```
The sum of these 4 values is 96.
So each slice size is determined by a fraction of that:
+ Slice 1: 30/96 = 31.25%
+ Slice 2: 50/96 = 52.08%
+ Slice 3: 10/96 = 10.42%
+ Slice 4: 6/96 = 6.25%
To label each slice with its percentage, add the `autopct` parameter when invoking `pie()`.
To customize how the percentage values are displayed, set `autopct` to a __format string__ such as as `%.2f` which will display the percentages as a floating point number (`f`) with 2 decimals (`.2`).
```py
plt.pie(
icecreamPreferences,
labels=iceCreamLabels,
colors=sliceColors,
startangle=90,
autopct='%.2f'
)
```
## Full example
```py
import matplotlib.pyplot as plt
# Set up data for the pie chart...
icecreamPreferences = [30,50,10,6] # does not have to sum to 100
iceCreamLabels = ['Strawberry', 'Chocolate', 'Vanilla','Banana']
# Make the pie chart...
# facecolor is the background color of the canvas
# figsize controls the (x, y) dimensions of the plot
plt.figure('Pie Chart Example', figsize=(4,4), facecolor='white')
plt.title("Ice Cream Preferences")
sliceColors = ['pink', 'chocolate', 'ivory','yellow']
plt.pie(
icecreamPreferences,
labels=iceCreamLabels,
colors=sliceColors,
startangle=90,
autopct='%.2f'
)
plt.show()
```
---
@include('/labs/lab07/_toc')
@stop