In [ ]:
Copied!
import pandas as pd
from plotnine import *
df = pd.read_csv('lumber-prices.csv')
df.head(2)
import pandas as pd
from plotnine import *
df = pd.read_csv('lumber-prices.csv')
df.head(2)
In this dataset, we can use df.dtypes
to see it's an object
, which generally means a normal boring string.
In [2]:
Copied!
df.dtypes
df.dtypes
Out[2]:
open float64 high float64 low float64 close float64 date object dtype: object
It's simple to convert the column to a datetime using pd.to_datetime
.
In [3]:
Copied!
df['date'] = pd.to_datetime(df.date)
df.head(2)
df['date'] = pd.to_datetime(df.date)
df.head(2)
Out[3]:
open | high | low | close | date | |
---|---|---|---|---|---|
0 | 407.0 | 424.7 | 377.0 | 424.7 | 1996-12-09 |
1 | 426.0 | 450.5 | 395.0 | 411.4 | 1997-01-02 |
Making your chart¶
Now you just use geom_line()
after setting your date column as the x axis.
In [6]:
Copied!
(
ggplot(df)
+ aes(x='date', y='open')
+ geom_line()
)
(
ggplot(df)
+ aes(x='date', y='open')
+ geom_line()
)
Out[6]:
<ggplot: (310604532)>
Adding points to a line chart¶
In [20]:
Copied!
(
ggplot(df)
+ aes(x='date', y='open')
+ geom_line()
+ geom_point(size=1)
)
(
ggplot(df)
+ aes(x='date', y='open')
+ geom_line()
+ geom_point(size=1)
)
Out[20]:
<ggplot: (311520443)>
In [ ]:
Copied!