In [1]:
from pathlib import Path
import pandas as pd
import plotnine as p9
import plotly as pl
import plotly.offline as po
import plotly.graph_objs as pg
%load_ext autoreload
%autoreload 2
po.init_notebook_mode(connected=True)
DATA_DIR = Path('..')/'data'
OUT_DIR = Path('..')/'output'
In [2]:
# Auckland bicycle counts
path = DATA_DIR/'bicycle_counts_2017.csv'
f = (
pd.read_csv(path, parse_dates=['datetime'])
.assign(datetime=lambda x: x.datetime.dt.strftime('%Y-%m'))
)
f
Out[2]:
In [3]:
# Use Plotnine to plot time series of the monthly bicycle counts.
# First need to put data in long format
counters = [c for c in f.columns if c != 'datetime']
g = (
pd.melt(f, id_vars=['datetime'], value_vars=counters,
var_name='counter', value_name='count')
)
print(g.head())
# Create plot
plot = (
p9.ggplot(g, p9.aes(x='datetime', y='count', color='counter', group='counter'))
+ p9.geom_line()
+ p9.labs(x='', y='Count', color='Counter', title='Auckland bicycle counts by month')
+ p9.theme(axis_text_x=p9.element_text(angle=45, hjust=1))
)
# Show plot and save to SVG
plot.save(str(OUT_DIR/'bicycle_counts_plotnine.svg'))
plot
Out[3]:
In [4]:
# Use Plotly to plot time series of the monthly bicycle counts.
# Create plot
counters = [c for c in f.columns if c != 'datetime']
data = [pg.Scatter(
x=f.datetime,
y=f[counter],
name=counter,
) for counter in counters
]
layout = pg.Layout(
title='Auckland bicycle counts by month',
yaxis=dict(title='Count'),
# Adding a legend title is hard work:
annotations=[dict(
x=1.17,
y=1.07,
align='right',
valign='top',
text='Counters',
showarrow=False,
xref='paper',
yref='paper',
)]
)
fig = dict(data=data, layout=layout)
# Show plot and save to SVG
po.iplot(fig, filename='bicycle_counts_plotly', image='svg')
In [5]:
# Alternatively, save the plot to HTML to preserve interactivity
path = OUT_DIR/'bicycle_counts_plotly.html'
po.plot(fig, filename=str(path), auto_open=False)
Out[5]: