# import * is a personal choice
from ggplot import *
# our trusty old friends
import pandas as pd
import numpy as np
%matplotlib inline
meat.head()
date | beef | veal | pork | lamb_and_mutton | broilers | other_chicken | turkey | |
---|---|---|---|---|---|---|---|---|
0 | 1944-01-01 | 751 | 85 | 1280 | 89 | NaN | NaN | NaN |
1 | 1944-02-01 | 713 | 77 | 1169 | 72 | NaN | NaN | NaN |
2 | 1944-03-01 | 741 | 90 | 1128 | 75 | NaN | NaN | NaN |
3 | 1944-04-01 | 650 | 89 | 978 | 66 | NaN | NaN | NaN |
4 | 1944-05-01 | 681 | 106 | 1029 | 78 | NaN | NaN | NaN |
5 rows × 8 columns
diamonds.head()
carat | cut | color | clarity | depth | table | price | x | y | z | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 0.23 | Ideal | E | SI2 | 61.5 | 55 | 326 | 3.95 | 3.98 | 2.43 |
1 | 0.21 | Premium | E | SI1 | 59.8 | 61 | 326 | 3.89 | 3.84 | 2.31 |
2 | 0.23 | Good | E | VS1 | 56.9 | 65 | 327 | 4.05 | 4.07 | 2.31 |
3 | 0.29 | Premium | I | VS2 | 62.4 | 58 | 334 | 4.20 | 4.23 | 2.63 |
4 | 0.31 | Good | J | SI2 | 63.3 | 58 | 335 | 4.34 | 4.35 | 2.75 |
5 rows × 10 columns
mtcars.head()
name | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
1 | Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
2 | Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
3 | Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
4 | Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
5 rows × 12 columns
pageviews.head()
date_hour | pageviews | |
---|---|---|
0 | 2013-02-11 21:00:00 | 8860.982383 |
1 | 2013-02-11 22:00:00 | 8637.474753 |
2 | 2013-02-11 23:00:00 | 9020.593099 |
3 | 2013-02-12 00:00:00 | 8437.500380 |
4 | 2013-02-12 01:00:00 | 9157.399672 |
?ggplot
ggplots take 2 arguments: a data frame and accompanying "aesthetics" or aes
. These are equivalent.
p = ggplot(aes(x='wt'), data=mtcars)
p = ggplot(mtcars, aes(x='wt'))
A ggplot is a "base layer". It won't create any aesthetics but think of it as a canvas. Watch what happens when you render it.
p
<ggplot: (281151189)>
aes
¶Aesthetics or aes
define how ggplot with extract data from your data frame and render it. Think of it as the instructions for creating x, y, color, etc. components.
aes
is just a dictionary with keys being an aesthetic property and values being strings or formulas--for more on formulas read this--relating to data in your data frame.
aes(x='date', y='price')
{'y': 'price', 'x': 'date'}
# shorthand
aes('date', 'price')
{u'y': 'price', u'x': 'date'}
# shorthand
aes('date', 'price', 'name')
{u'y': 'price', u'x': 'date', u'color': 'name'}
# formula
aes(x='date', y='price', color='date * price', shape='factor(name)')
{'color': 'date * price', 'y': 'price', 'shape': 'factor(name)', 'x': 'date'}
ggplot
¶So taking everything that we've learned, let's use the mtcars
dataset to plot the relationship between car weight (wt
) and miles per gallon (mpg
). First create a ggplot
object with the proper aes
and name it p
.
p = ggplot(aes(x='wt', y='mpg'), data=mtcars)
p
<ggplot: (274847889)>
Now let's (quite literally) add a scatterplot (geom_point
) to our plot. We'll get into more detail on how this works later.
p + geom_point()
<ggplot: (275439357)>