In [2]:
# import * is a personal choice
from ggplot import *
# our trusty old friends
import pandas as pd
import numpy as np
In [1]:
%matplotlib inline
In [3]:
meat.head()
Out[3]:
In [4]:
diamonds.head()
Out[4]:
In [5]:
mtcars.head()
Out[5]:
In [7]:
pageviews.head()
Out[7]:
In [9]:
?ggplot
ggplots take 2 arguments: a data frame and accompanying "aesthetics" or aes
. These are equivalent.
In [6]:
p = ggplot(aes(x='wt'), data=mtcars)
In [7]:
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.
In [8]:
p
Out[8]:
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.
In [9]:
aes(x='date', y='price')
Out[9]:
In [10]:
# shorthand
aes('date', 'price')
Out[10]:
In [11]:
# shorthand
aes('date', 'price', 'name')
Out[11]:
In [12]:
# formula
aes(x='date', y='price', color='date * price', shape='factor(name)')
Out[12]:
In [30]:
p = ggplot(aes(x='wt', y='mpg'), data=mtcars)
p
Out[30]:
Now let's (quite literally) add a scatterplot (geom_point
) to our plot. We'll get into more detail on how this works later.
In [31]:
p + geom_point()
Out[31]:
In [ ]: