Getting set up
Installing pyWaffle
We’ll be using the pyWaffle
library,
which you can install with pip
.
pip install pywaffle
Importing into your Jupyter Notebook
You’ll need to import all of these to make it work!
Type 1: Using columns for drawing
Sometimes you’ll just want to use columns right out of your dataframe.
crime_type | number_of_cases | |
---|---|---|
0 | felony | 54 |
1 | misdemeanor | 12 |
2 | violation | 8 |
/Users/jonathansoma/.pyenv/versions/3.6.5/lib/python3.6/site-packages/matplotlib/figure.py:2267: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
warnings.warn("This figure includes Axes that are not compatible "
Type 2: Using pandas calculations for a waffle chart
First we’ll read in/create our dataset…
District | party | |
---|---|---|
0 | District 12 | Republican |
1 | District 23 | Republican |
2 | District 32 | Republican |
3 | District 45 | Republican |
4 | District 65 | Democrat |
5 | District 67 | Democrat |
6 | District 33 | Democrat |
We can’t use this raw data for our waffle chart, because we need a list of numbers for it! In order to chart the number of Democrats and Republicans, we need to first get a value counts…
Republican 4
Democrat 3
Name: party, dtype: int64
…then you’ll feed the index
and values
into your waffle chart separately.
The index
(on the left) will be your labels, the values (on the right) will be
the number of each square to display. If you don’t use list()
with the labels
part it won’t work.
/Users/jonathansoma/.pyenv/versions/3.6.5/lib/python3.6/site-packages/matplotlib/figure.py:2267: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
warnings.warn("This figure includes Axes that are not compatible "
Hot tips: Customizing your graphic
You can use figsize=
, legend=
, and other
options, too.
crime_type | number_of_cases | |
---|---|---|
0 | felony | 54 |
1 | misdemeanor | 12 |
2 | violation | 8 |
/Users/jonathansoma/.pyenv/versions/3.6.5/lib/python3.6/site-packages/matplotlib/figure.py:2267: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
warnings.warn("This figure includes Axes that are not compatible "